Python操作MySQL數(shù)據(jù)庫的示例代碼
1. MySQL Connector
1.1 創(chuàng)建連接
import mysql.connector
config={
"host":"localhost","port":"3306",
"user":"root","password":"password",
"database":"demo"
}
con=mysql.connector.connect(**config)
import mysql.connector
config={
"host":"localhost","port":"3306",
"user":"root","password":"password",
"database":"demo"
}
con=mysql.connector.connect(**config)
1.2 Cursor
import mysql.connector
con=mysql.connector.connect(
host="localhost",port="3306",
user="root",password="password",
database="demo"
)
cursor=con.cursor()
sql="SELECT empno,job,sal FROM t_bonus;"
cursor.execute(sql)
print(type(cursor))
for i in cursor:
print(i)
con.close()
Result:
<class 'mysql.connector.cursor_cext.CMySQLCursor'>
(7369, 'CLERK', Decimal('8000.00'))
(7499, 'SALESMAN', Decimal('1600.00'))
(7521, 'SALESMAN', Decimal('1250.00'))
(7566, 'MANAGER', Decimal('2975.00'))
(7654, 'SALESMAN', Decimal('1250.00'))
(7698, 'MANAGER', Decimal('2850.00'))
(7782, 'MANAGER', Decimal('2450.00'))
(7788, 'ANALYST', Decimal('3000.00'))
(7839, 'PRESIDENT', Decimal('5000.00'))
(7844, 'SALESMAN', Decimal('1500.00'))
(7900, 'CLERK', Decimal('950.00'))
(7902, 'ANALYST', Decimal('3000.00'))
(7934, 'CLERK', Decimal('1300.00'))
1.3 SQL注入攻擊
- username=1 OR 1=1 password=1 OR 1=1
- 在使用字符串直接拼接時OR之前不管對錯,與OR結(jié)合都為true
- 解決方法——預(yù)編譯(也可以提高速度)
1.4 事務(wù)管理和異常處理
sql連接和使用異常處理異常
import mysql.connector
try:
con=mysql.connector.connect(
host="localhost",port="3306",
user="root",password="password",
database="demo"
)
con.start_transaction()
cursor=con.cursor()
sql="INSERT INTO t_dept(deptno,dname,loc) VALUES(%s,%s,%s);"
cursor.execute(sql,(60,"SALES","HUBAI"))
con.commit()
except Exception as e:
if "con" in dir():
con.rollback()
print(e)
finally:
if "con" in dir():
con.close()
1.5 刪除數(shù)據(jù)
import mysql.connector,mysql.connector.pooling
config={
"host": "localhost", "port": "3306",
"user": "root", "password": "password",
"database": "demo"
}
try:
pool=mysql.connector.pooling.MySQLConnectionPool(**config,pool_size=5)
con=pool.get_connection()
con.start_transaction()
cursor = con.cursor()
sql = "DELETE FROM t_dept WHERE deptno=%s"
cursor.execute(sql, (70,))
con.commit()
except Exception as e:
if "con" in dir():
con.rollback()
print(e)
# do not need to close con
executemany() 反復(fù)執(zhí)行一條SQL語句
import mysql.connector,mysql.connector.pooling
config={
"host": "localhost", "port": "3306",
"user": "root", "password": "password",
"database": "demo"
}
try:
pool=mysql.connector.pooling.MySQLConnectionPool(**config,pool_size=5)
con=pool.get_connection()
con.start_transaction()
cursor = con.cursor()
sql = "INSERT INTO t_dept(deptno,dname,loc) VALUES(%s,%s,%s);"
date=[[70,"SALES","BEIJING"],[80,"ACTOR","SHANGHAI"]]
cursor.executemany(sql, date)
con.commit()
except Exception as e:
if "con" in dir():
con.rollback()
print(e)
# do not need to close con
2. 數(shù)據(jù)庫連接池
- 數(shù)據(jù)庫的連接是昂貴的,一個連接要經(jīng)過TCP三次握手,四次揮手,而且一臺計算機的最大線程數(shù)也是有限的
- 數(shù)據(jù)庫連接池技術(shù)就是先創(chuàng)建好連接,再直接拿出來使用
import mysql.connector,mysql.connector.pooling
config={
"host": "localhost", "port": "3306",
"user": "root", "password": "password",
"database": "demo"
}
try:
pool=mysql.connector.pooling.MySQLConnectionPool(**config,pool_size=5)
con=pool.get_connection()
con.start_transaction()
cursor = con.cursor()
sql = "INSERT INTO t_dept(deptno,dname,loc) VALUES(%s,%s,%s);"
cursor.execute(sql, (70, "SALES", "HUBAI"))
con.commit()
except Exception as e:
if "con" in dir():
con.rollback()
print(e)
# do not need to close con
以上就是Python操作MySQL數(shù)據(jù)庫的示例代碼的詳細內(nèi)容,更多關(guān)于Python操作MySQL數(shù)據(jù)庫的資料請關(guān)注腳本之家其它相關(guān)文章!
- Python基礎(chǔ)之操作MySQL數(shù)據(jù)庫
- Python操作MySQL數(shù)據(jù)庫的簡單步驟分享
- Python 操作 MySQL數(shù)據(jù)庫
- Python連接mysql數(shù)據(jù)庫及簡單增刪改查操作示例代碼
- Python實現(xiàn)的連接mssql數(shù)據(jù)庫操作示例
- python詳解如何通過sshtunnel pymssql實現(xiàn)遠程連接數(shù)據(jù)庫
- Python基于Pymssql模塊實現(xiàn)連接SQL Server數(shù)據(jù)庫的方法詳解
- Python連接mssql數(shù)據(jù)庫編碼問題解決方法
- 使用Python操作MySql數(shù)據(jù)庫和MsSql數(shù)據(jù)庫
相關(guān)文章
Python在Windows環(huán)境下的文件路徑問題及解決辦法
在Python中處理Windows路徑時,經(jīng)常會遇到一些特殊的問題,在Windows中,路徑使用反斜杠(\)作為分隔符,而在其他操作系統(tǒng)中,路徑使用正斜杠(/)作為分隔符,本文給大家介紹了Python在Windows環(huán)境下的文件路徑問題及解決辦法,需要的朋友可以參考下2024-06-06
Python數(shù)據(jù)結(jié)構(gòu)之優(yōu)先級隊列queue用法詳解
queue庫提供了一個適用于多線程編程的先進先出(FIFO)數(shù)據(jù)結(jié)構(gòu),可以用來在生產(chǎn)者與消費者線程之間安全地傳遞消息或其他數(shù)據(jù),它會為調(diào)用者處理鎖定,使多個線程可以安全而更容易地處理同一個Queue實例.Queue的大小可能受限,以限制內(nèi)存使用或處理,需要的朋友可以參考下2021-05-05
Python進程multiprocessing.Process()的使用解讀
這篇文章主要介紹了Python進程multiprocessing.Process()的使用,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02
六個Python3中使用最廣泛的內(nèi)置函數(shù)總結(jié)
這篇文章主要為大家詳細介紹了六個Python3中使用最廣泛的內(nèi)置函數(shù):Lamdba?函數(shù)、Map?函數(shù)、Filter?函數(shù)、Reduce?函數(shù)、Enumerate?函數(shù)和Zip?函數(shù),需要的可以參考一下2022-08-08
Python使用plotly繪制數(shù)據(jù)圖表的方法
本篇文章主要介紹了Python使用plotly繪制數(shù)據(jù)圖表的方法,實例分析了plotly繪制的技巧,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07
Python tornado隊列示例-一個并發(fā)web爬蟲代碼分享
這篇文章主要介紹了Python tornado隊列示例-一個并發(fā)web爬蟲代碼分享,具有一定借鑒價值,需要的朋友可以參考下2018-01-01
Python操作PostgreSql數(shù)據(jù)庫的方法(基本的增刪改查)
這篇文章主要介紹了Python操作PostgreSql數(shù)據(jù)庫(基本的增刪改查),本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12

