Python操作MySQL數(shù)據(jù)庫(kù)的示例代碼
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
- 在使用字符串直接拼接時(shí)OR之前不管對(duì)錯(cuò),與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語(yǔ)句
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ù)庫(kù)連接池
- 數(shù)據(jù)庫(kù)的連接是昂貴的,一個(gè)連接要經(jīng)過(guò)TCP三次握手,四次揮手,而且一臺(tái)計(jì)算機(jī)的最大線程數(shù)也是有限的
- 數(shù)據(jù)庫(kù)連接池技術(shù)就是先創(chuàng)建好連接,再直接拿出來(lái)使用
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ù)庫(kù)的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于Python操作MySQL數(shù)據(jù)庫(kù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- Python基礎(chǔ)之操作MySQL數(shù)據(jù)庫(kù)
- Python操作MySQL數(shù)據(jù)庫(kù)的簡(jiǎn)單步驟分享
- Python 操作 MySQL數(shù)據(jù)庫(kù)
- Python連接mysql數(shù)據(jù)庫(kù)及簡(jiǎn)單增刪改查操作示例代碼
- Python實(shí)現(xiàn)的連接mssql數(shù)據(jù)庫(kù)操作示例
- python詳解如何通過(guò)sshtunnel pymssql實(shí)現(xiàn)遠(yuǎn)程連接數(shù)據(jù)庫(kù)
- Python基于Pymssql模塊實(shí)現(xiàn)連接SQL Server數(shù)據(jù)庫(kù)的方法詳解
- Python連接mssql數(shù)據(jù)庫(kù)編碼問(wèn)題解決方法
- 使用Python操作MySql數(shù)據(jù)庫(kù)和MsSql數(shù)據(jù)庫(kù)
相關(guān)文章
解決python3捕獲cx_oracle拋出的異常錯(cuò)誤問(wèn)題
今天小編就為大家分享一篇解決python3捕獲cx_oracle拋出的異常錯(cuò)誤問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
Python 數(shù)據(jù)處理更容易的12個(gè)輔助函數(shù)總結(jié)
Python的產(chǎn)生似乎就是專門(mén)用來(lái)處理數(shù)據(jù)的,順理成章的成為大數(shù)據(jù)的主流語(yǔ)言,本文介紹十二個(gè)函數(shù)輔助你更容易更便捷的用Python進(jìn)行數(shù)據(jù)處理2021-11-11
編寫(xiě)python代碼實(shí)現(xiàn)簡(jiǎn)單抽獎(jiǎng)器
這篇文章主要介紹了編寫(xiě)python代碼實(shí)現(xiàn)簡(jiǎn)單抽獎(jiǎng)器,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
Python讀取大型數(shù)據(jù)文件的6種方式匯總
在 Python 中,我們可以使用多種方法讀取大型數(shù)據(jù)文件,本文主要為大家介紹6個(gè)常用的Python讀取大型數(shù)據(jù)文件的方法,希望對(duì)大家有所幫助2023-05-05
python3.4 將16進(jìn)制轉(zhuǎn)成字符串的實(shí)例
今天小編就為大家分享一篇python3.4 將16進(jìn)制轉(zhuǎn)成字符串的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06
Python 最強(qiáng)編輯器詳細(xì)使用指南(PyCharm )
這篇文章主要介紹了Python 最強(qiáng)編輯器詳細(xì)使用指南(PyCharm),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
pytorch模型存儲(chǔ)的2種實(shí)現(xiàn)方法
今天小編就為大家分享一篇pytorch模型存儲(chǔ)的2種實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02
python sleep和wait對(duì)比總結(jié)
在本篇文章里小編給大家整理的是一篇關(guān)于python sleep和wait對(duì)比總結(jié)內(nèi)容,對(duì)此有興趣的朋友們可以學(xué)習(xí)下。2021-02-02

