Python常用數(shù)據(jù)庫接口sqlite3和MySQLdb學(xué)習(xí)指南
sqlite3 - SQLite 數(shù)據(jù)庫
SQLite 是一款輕量級(jí)、無需安裝、零配置的嵌入式關(guān)系數(shù)據(jù)庫。Python 自帶 sqlite3
庫,無需額外安裝。以下是一個(gè)簡單的 sqlite3
使用示例。
創(chuàng)建數(shù)據(jù)庫和表
import sqlite3 # 連接數(shù)據(jù)庫(如果不存在,將自動(dòng)創(chuàng)建) conn = sqlite3.connect('sqlite_example.db') # 創(chuàng)建游標(biāo) cursor = conn.cursor() # 創(chuàng)建 users 表 cursor.execute(''' CREATE TABLE IF NOT EXISTS users ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, age INTEGER NOT NULL, email TEXT ) ''') # 提交更改并關(guān)閉連接 conn.commit() conn.close()
插入數(shù)據(jù)
conn = sqlite3.connect('sqlite_example.db') cursor = conn.cursor() # 插入數(shù)據(jù) cursor.execute("INSERT INTO users (name, age, email) VALUES ('Alice', 30, 'alice@example.com')") # 提交更改 conn.commit() # 關(guān)閉連接 conn.close()
查詢數(shù)據(jù)
conn = sqlite3.connect('sqlite_example.db') cursor = conn.cursor() # 查詢數(shù)據(jù) cursor.execute('SELECT * FROM users') # 獲取查詢結(jié)果 result = cursor.fetchall() for row in result: print(row) # 關(guān)閉連接 conn.close()
更新和刪除數(shù)據(jù)
conn = sqlite3.connect('sqlite_example.db') cursor = conn.cursor() # 更新數(shù)據(jù) cursor.execute("UPDATE users SET age = 31 WHERE name = 'Alice'") conn.commit() # 刪除數(shù)據(jù) cursor.execute("DELETE FROM users WHERE id = 1") conn.commit() # 關(guān)閉連接 conn.close()
MySQLdb - MySQL 數(shù)據(jù)庫
MySQLdb
是一個(gè)用于連接 MySQL 數(shù)據(jù)庫的 Python 庫。在使用之前,需要先安裝:
pip install mysqlclient
注:MySQLdb
僅支持 Python 2.x 版本,如果你使用的是 Python 3.x,可以使用 mysqlclient
,它是 MySQLdb
的 Python 3.x 分支。
以下是一個(gè)簡單的 MySQLdb
使用示例。
創(chuàng)建數(shù)據(jù)庫和表
import MySQLdb # 連接數(shù)據(jù)庫 conn = MySQLdb.connect(host='localhost', user='your_username', passwd='your_password', db='mydb') # 創(chuàng)建游標(biāo) cursor = conn.cursor() # 創(chuàng)建 users 表 cursor.execute(''' CREATE TABLE IF NOT EXISTS users ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50) NOT NULL, age INT NOT NULL, email VARCHAR(100) ) ''') # 提交更改并關(guān)閉連接 conn.commit() conn.close()
插入數(shù)據(jù)
conn = MySQLdb.connect(host='localhost', user='your_username', passwd='your_password', db='mydb') cursor = conn.cursor() # 插入數(shù)據(jù) cursor.execute("INSERT INTO users (name, age, email) VALUES ('Alice', 30, 'alice@example.com')") # 提交更改 conn.commit() # 關(guān)閉連接 conn.close()
查詢數(shù)據(jù)
conn = MySQLdb.connect(host='localhost', user='your_username', passwd='your_password', db='mydb') cursor = conn.cursor() # 查詢數(shù)據(jù) cursor.execute('SELECT * FROM users') # 獲取查詢結(jié)果 result = cursor.fetchall() for row in result: print(row) # 關(guān)閉連接 conn.close()
更新和刪除數(shù)據(jù)
conn = MySQLdb.connect(host='localhost', user='your_username', passwd='your_password', db='mydb') cursor = conn.cursor() # 更新數(shù)據(jù) cursor.execute("UPDATE users SET age = 31 WHERE name = 'Alice'") conn.commit() # 刪除數(shù)據(jù) cursor.execute("DELETE FROM users WHERE id = 1") conn.commit() # 關(guān)閉連接 conn.close()
以上就是 Python 中常用的數(shù)據(jù)庫接口 sqlite3
和 MySQLdb
的使用示例。學(xué)習(xí)這些知識(shí)點(diǎn)時(shí),建議你親自動(dòng)手操作,逐漸積累經(jīng)驗(yàn)。在實(shí)際項(xiàng)目中,你可能會(huì)遇到更復(fù)雜的場景,需要根據(jù)實(shí)際情況選擇適當(dāng)?shù)臄?shù)據(jù)庫操作和優(yōu)化策略。同時(shí),編寫易于維護(hù)和擴(kuò)展的代碼也很重要,如使用 ORM(Object-Relational Mapping,對象關(guān)系映射)庫,例如 SQLAlchemy 和 Django ORM,可以簡化數(shù)據(jù)庫操作,提高代碼可讀性和可維護(hù)性
更多關(guān)于Python數(shù)據(jù)庫接口的資料請關(guān)注腳本之家其它相關(guān)文章!
- Python使用MySQL8.2讀寫分離實(shí)現(xiàn)示例詳解
- Python中執(zhí)行MySQL結(jié)果限制和分頁查詢示例詳解
- python實(shí)現(xiàn)MySQL?數(shù)據(jù)庫表格創(chuàng)建?數(shù)據(jù)插入及獲取插入ID操作教程
- Python數(shù)據(jù)庫安裝及MySQL?Connector應(yīng)用教程
- Python使用Pandas庫實(shí)現(xiàn)MySQL數(shù)據(jù)庫讀寫
- python pymysql peewee關(guān)于時(shí)區(qū)問題分析
- Python pymysql連接數(shù)據(jù)庫并將查詢結(jié)果轉(zhuǎn)化為Pandas dataframe
- python導(dǎo)出mysql指定binlog文件實(shí)現(xiàn)demo
相關(guān)文章
使用darknet框架的imagenet數(shù)據(jù)分類預(yù)訓(xùn)練操作
這篇文章主要介紹了使用darknet框架的imagenet數(shù)據(jù)分類預(yù)訓(xùn)練操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07python報(bào)錯(cuò)TypeError: ‘NoneType‘ object is not subscriptable的解決
這篇文章主要給大家介紹了關(guān)于python報(bào)錯(cuò)TypeError: ‘NoneType‘ object is not subscriptable的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11Python監(jiān)控主機(jī)是否存活并以郵件報(bào)警
本文是利用python腳本寫的簡單測試主機(jī)是否存活,此腳本有個(gè)缺點(diǎn)不適用線上,由于網(wǎng)絡(luò)延遲、丟包現(xiàn)象會(huì)造成誤報(bào)郵件,感興趣的朋友一起看看Python監(jiān)控主機(jī)是否存活并以郵件報(bào)警吧2015-09-09python中字符串比較使用is、==和cmp()總結(jié)
在Python中比較字符串最好是使用簡單邏輯操作符,今天為大家講解一下is、==和cmp()使用總結(jié)2018-03-03使用Python PIL庫讀取文件批量處理圖片大小實(shí)現(xiàn)
這篇文章主要為大家介紹了使用Python PIL庫讀取文件批量處理圖片大小實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07Python OpenCV 使用滑動(dòng)條來調(diào)整函數(shù)參數(shù)的方法
這篇文章主要介紹了Python OpenCV 使用滑動(dòng)條來調(diào)整函數(shù)參數(shù)的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07使用Python實(shí)現(xiàn)圖像標(biāo)記點(diǎn)的坐標(biāo)輸出功能
這篇文章主要介紹了使用Python實(shí)現(xiàn)圖像標(biāo)記點(diǎn)的坐標(biāo)輸出功能,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2019-08-08