Python與數(shù)據(jù)庫交互:入門指南
"""測(cè)試連接數(shù)據(jù)庫"""
1.第一步導(dǎo)包
from pymysql import Connection
2.獲取連接對(duì)象
connection = Connection( host="localhost", #主機(jī)名(IP) port=3306, #端口號(hào) user="root", #用戶名 password="123456", #密碼 autocommit=True #(自動(dòng)確認(rèn),用于增刪改) )
3.獲取游標(biāo)
cursor = connection.cursor()
4.使用數(shù)據(jù)庫
connection.select_db("test")
5.執(zhí)行sql語句
cursor.execute("select *from user")
6.獲取數(shù)據(jù)(數(shù)據(jù)為元組類型)
fetchall = cursor.fetchall()
7.循環(huán)遍歷數(shù)據(jù)
for e in fetchall: print(e)
8.關(guān)閉對(duì)象
connection.close()
以下是一個(gè)使用Python連接數(shù)據(jù)庫的示例代碼:
import mysql.connector # 連接數(shù)據(jù)庫 def connect_to_database(): try: conn = mysql.connector.connect( host="your_host", user="your_user", password="your_password", database="your_database" ) print("成功連接到數(shù)據(jù)庫") return conn except mysql.connector.Error as err: print("數(shù)據(jù)庫連接失?。?, err) # 查詢數(shù)據(jù) def query_data(conn, query): try: cursor = conn.cursor() cursor.execute(query) results = cursor.fetchall() for row in results: print(row) except mysql.connector.Error as err: print("查詢數(shù)據(jù)失?。?, err) # 插入數(shù)據(jù) def insert_data(conn, query, values): try: cursor = conn.cursor() cursor.execute(query, values) conn.commit() print("數(shù)據(jù)插入成功") except mysql.connector.Error as err: print("插入數(shù)據(jù)失?。?, err) # 更新數(shù)據(jù) def update_data(conn, query): try: cursor = conn.cursor() cursor.execute(query) conn.commit() print("數(shù)據(jù)更新成功") except mysql.connector.Error as err: print("更新數(shù)據(jù)失?。?, err) # 刪除數(shù)據(jù) def delete_data(conn, query): try: cursor = conn.cursor() cursor.execute(query) conn.commit() print("數(shù)據(jù)刪除成功") except mysql.connector.Error as err: print("刪除數(shù)據(jù)失敗:", err) # 關(guān)閉數(shù)據(jù)庫連接 def close_connection(conn): conn.close() print("數(shù)據(jù)庫連接已關(guān)閉") # 測(cè)試連接和查詢 conn = connect_to_database() query = "SELECT * FROM your_table" query_data(conn, query) close_connection(conn)
到此這篇關(guān)于Python與數(shù)據(jù)庫交互:入門指南的文章就介紹到這了,更多相關(guān)Python連接數(shù)據(jù)庫內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
pytorch中nn.Flatten()函數(shù)詳解及示例
nn.Flatten是一個(gè)類,而torch.flatten()則是一個(gè)函數(shù),下面這篇文章主要給大家介紹了關(guān)于pytorch中nn.Flatten()函數(shù)詳解及示例的相關(guān)資料,需要的朋友可以參考下2023-01-01Python 實(shí)現(xiàn)輸入任意多個(gè)數(shù),并計(jì)算其平均值的例子
今天小編就為大家分享一篇Python 實(shí)現(xiàn)輸入任意多個(gè)數(shù),并計(jì)算其平均值的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-07-07Python實(shí)戰(zhàn)之生成有關(guān)聯(lián)單選問卷
這篇文章主要為大家分享了一個(gè)Python實(shí)戰(zhàn)小案例——生成有關(guān)聯(lián)單選問卷,并且能根據(jù)問卷總分?jǐn)?shù)生成對(duì)應(yīng)判斷文案結(jié)果,感興趣的可以了解一下2023-04-04python實(shí)現(xiàn)超時(shí)退出的三種方式總結(jié)
這篇文章主要介紹了python實(shí)現(xiàn)超時(shí)退出的三種方式總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11Python機(jī)器學(xué)習(xí)之決策樹和隨機(jī)森林
本文主要介紹了機(jī)器學(xué)習(xí)之決策樹和隨機(jī)森林,詳細(xì)的介紹了實(shí)現(xiàn) 原理機(jī)器實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07python 請(qǐng)求服務(wù)器的實(shí)現(xiàn)代碼(http請(qǐng)求和https請(qǐng)求)
本篇文章主要介紹了python 請(qǐng)求服務(wù)器的實(shí)現(xiàn)代碼(http請(qǐng)求和https請(qǐng)求),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05基于python編寫一個(gè)簡單的壓力測(cè)試(DDoS)腳本
這篇文章主要為大家詳細(xì)介紹了如何基于python編寫一個(gè)簡單的壓力測(cè)試(DDoS)腳本,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2024-12-12Python?Celery動(dòng)態(tài)添加定時(shí)任務(wù)生產(chǎn)實(shí)踐指南
elery是一種異步任務(wù)隊(duì)列,如果還不熟悉這個(gè)開源軟件的請(qǐng)先看看官方文檔,快速入門,下面這篇文章主要給大家介紹了關(guān)于Python?Celery動(dòng)態(tài)添加定時(shí)任務(wù)生產(chǎn)實(shí)踐的相關(guān)資料,需要的朋友可以參考下2022-08-08