Python處理MySQL與SQLite數(shù)據(jù)庫詳解
在數(shù)據(jù)處理和存儲方面,數(shù)據(jù)庫扮演著至關(guān)重要的角色。Python提供了多種與數(shù)據(jù)庫交互的方式,其中pymysql庫用于連接和操作MySQL數(shù)據(jù)庫,而SQLite則是一種輕量級的嵌入式數(shù)據(jù)庫,Python標(biāo)準(zhǔn)庫中的sqlite3模塊即可滿足操作需求。本文將為入門者介紹MySQL和SQLite,并分別展示如何使用Python進行增刪改查操作。
1. MySQL 簡介
MySQL是一種開源的關(guān)系型數(shù)據(jù)庫管理系統(tǒng)(RDBMS),廣泛應(yīng)用于各種Web應(yīng)用。它支持標(biāo)準(zhǔn)的SQL語言,提供了高性能、高可靠性和可擴展性。
安裝pymysql
在Python中操作MySQL數(shù)據(jù)庫,需要先安裝pymysql庫。可以使用pip進行安裝:
pip install pymysql
1.1 連接MySQL數(shù)據(jù)庫
import pymysql
# 創(chuàng)建數(shù)據(jù)庫連接
connection = pymysql.connect(
host='localhost', # 數(shù)據(jù)庫主機地址
user='your_username', # 數(shù)據(jù)庫用戶名
password='your_password', # 數(shù)據(jù)庫密碼
database='your_database' # 數(shù)據(jù)庫名稱
)
# 創(chuàng)建游標(biāo)對象
cursor = connection.cursor()
1.2 增加數(shù)據(jù)(Insert)
try:
# SQL插入語句
sql = "INSERT INTO your_table (column1, column2) VALUES (%s, %s)"
val = ("value1", "value2")
# 執(zhí)行SQL語句
cursor.execute(sql, val)
# 提交事務(wù)
connection.commit()
print("插入成功")
except Exception as e:
print("插入失?。?, e)
# 回滾事務(wù)
connection.rollback()
finally:
# 關(guān)閉游標(biāo)和連接
cursor.close()
connection.close()1.3 查詢數(shù)據(jù)(Select)
try:
# SQL查詢語句
sql = "SELECT * FROM your_table"
# 執(zhí)行SQL語句
cursor.execute(sql)
# 獲取所有記錄
results = cursor.fetchall()
for row in results:
print(row)
except Exception as e:
print("查詢失?。?, e)
finally:
# 關(guān)閉游標(biāo)和連接
cursor.close()
connection.close()1.4 更新數(shù)據(jù)(Update)
try:
# SQL更新語句
sql = "UPDATE your_table SET column1 = %s WHERE column2 = %s"
val = ("new_value1", "value2")
# 執(zhí)行SQL語句
cursor.execute(sql, val)
# 提交事務(wù)
connection.commit()
print("更新成功")
except Exception as e:
print("更新失敗:", e)
# 回滾事務(wù)
connection.rollback()
finally:
# 關(guān)閉游標(biāo)和連接
cursor.close()
connection.close()1.5 刪除數(shù)據(jù)(Delete)
try:
# SQL刪除語句
sql = "DELETE FROM your_table WHERE column1 = %s"
val = ("value1",)
# 執(zhí)行SQL語句
cursor.execute(sql, val)
# 提交事務(wù)
connection.commit()
print("刪除成功")
except Exception as e:
print("刪除失?。?, e)
# 回滾事務(wù)
connection.rollback()
finally:
# 關(guān)閉游標(biāo)和連接
cursor.close()
connection.close()2. SQLite 簡介
SQLite是一種輕量級的嵌入式數(shù)據(jù)庫,它不需要單獨的服務(wù)器進程,非常適合在本地存儲數(shù)據(jù)。SQLite支持標(biāo)準(zhǔn)的SQL語法,并且Python標(biāo)準(zhǔn)庫中的sqlite3模塊可以直接操作SQLite數(shù)據(jù)庫。
2.1 連接SQLite數(shù)據(jù)庫
import sqlite3
# 創(chuàng)建數(shù)據(jù)庫連接(如果數(shù)據(jù)庫文件不存在會自動創(chuàng)建)
connection = sqlite3.connect('your_database.db')
# 創(chuàng)建游標(biāo)對象
cursor = connection.cursor()
2.2 增加數(shù)據(jù)(Insert)
try:
# SQL插入語句
sql = "INSERT INTO your_table (column1, column2) VALUES (?, ?)"
val = ("value1", "value2")
# 執(zhí)行SQL語句
cursor.execute(sql, val)
# 提交事務(wù)
connection.commit()
print("插入成功")
except sqlite3.Error as e:
print("插入失敗:", e)
finally:
# 關(guān)閉游標(biāo)和連接
cursor.close()
connection.close()2.3 查詢數(shù)據(jù)(Select)
try:
# SQL查詢語句
sql = "SELECT * FROM your_table"
# 執(zhí)行SQL語句
cursor.execute(sql)
# 獲取所有記錄
results = cursor.fetchall()
for row in results:
print(row)
except sqlite3.Error as e:
print("查詢失?。?, e)
finally:
# 關(guān)閉游標(biāo)和連接
cursor.close()
connection.close()2.4 更新數(shù)據(jù)(Update)
try:
# SQL更新語句
sql = "UPDATE your_table SET column1 = ? WHERE column2 = ?"
val = ("new_value1", "value2")
# 執(zhí)行SQL語句
cursor.execute(sql, val)
# 提交事務(wù)
connection.commit()
print("更新成功")
except sqlite3.Error as e:
print("更新失敗:", e)
finally:
# 關(guān)閉游標(biāo)和連接
cursor.close()
connection.close()2.5 刪除數(shù)據(jù)(Delete)
try:
# SQL刪除語句
sql = "DELETE FROM your_table WHERE column1 = ?"
val = ("value1",)
# 執(zhí)行SQL語句
cursor.execute(sql, val)
# 提交事務(wù)
connection.commit()
print("刪除成功")
except sqlite3.Error as e:
print("刪除失?。?, e)
finally:
# 關(guān)閉游標(biāo)和連接
cursor.close()
connection.close()通過以上步驟,你可以輕松地使用Python對MySQL和SQLite數(shù)據(jù)庫進行增刪改查操作。
以上就是Python處理MySQL與SQLite數(shù)據(jù)庫詳解的詳細(xì)內(nèi)容,更多關(guān)于Python處理MySQL與SQLite的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python使用win32com.client模塊實現(xiàn)xls轉(zhuǎn)xlsx自動化的方法
在日常辦公和數(shù)據(jù)處理中,我們經(jīng)常會遇到老式的.xls格式Excel文件,這類文件兼容性差、容量有限,且無法支持Excel新版本的部分功能,手動逐個轉(zhuǎn)換格式費時費力,今天就給大家分享一個實用技巧,使用Python的win32com.client模塊,需要的朋友可以參考下2026-05-05
Python import與from import使用及區(qū)別介紹
Python程序可以調(diào)用一組基本的函數(shù)(即內(nèi)建函數(shù)),比如print()、input()和len()等函數(shù)。接下來通過本文給大家介紹Python import與from import使用及區(qū)別介紹,感興趣的朋友一起看看吧2018-09-09
PyTorch高級教程之自定義模型、數(shù)據(jù)加載及設(shè)備間數(shù)據(jù)移動
在深入理解了PyTorch的核心組件之后,我們將進一步學(xué)習(xí)一些高級主題,包括如何自定義模型、加載自定義數(shù)據(jù)集,以及如何在設(shè)備(例如CPU和GPU)之間移動數(shù)據(jù),需要的朋友可以參考下2023-07-07
Python實現(xiàn)圖像尺寸和格式轉(zhuǎn)換處理的示例詳解
這篇文章主要為大家詳細(xì)介紹了如何利用Python實現(xiàn)圖像尺寸獲取和格式轉(zhuǎn)換處理的功能,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2023-04-04

