Python連接Mysql進(jìn)行增刪改查的示例代碼
Python連接Mysql
1.安裝對(duì)應(yīng)的庫(kù)
使用Python連接Mysql數(shù)據(jù)庫(kù)需要安裝相應(yīng)的庫(kù)
以管理員身份運(yùn)行cmd,輸入命令
pip install mysql.connector
安裝完成后建立test.py寫入import mysql.connector保存后運(yùn)行python test.py用以測(cè)試模塊庫(kù)是否安裝完成,如果不報(bào)錯(cuò),說(shuō)明安裝完成
2.進(jìn)行連接測(cè)試
編寫connectTest.py文件文件內(nèi)容:
import mysql.connectorconnect = mysql.connector.connect( host='127.0.0.1', # 數(shù)據(jù)庫(kù)主機(jī)地址 user='root', # 數(shù)據(jù)庫(kù)用戶名 passwd='root', # 數(shù)據(jù)庫(kù)密碼 database='mysql' # 要連接的數(shù)據(jù)庫(kù))#關(guān)閉連接connect.close()
運(yùn)行文件python connectTest.py如果沒(méi)有報(bào)錯(cuò)提示說(shuō)明連接成功,如果報(bào)錯(cuò)提示
說(shuō)明連接失敗,請(qǐng)檢查賬戶、密碼以及數(shù)據(jù)庫(kù)是否正確,查看數(shù)據(jù)庫(kù)是否開(kāi)機(jī)
3.執(zhí)行sql命令
3.1創(chuàng)建表
import mysql.connectorconnect = mysql.connector.connect( host='127.0.0.1', # 數(shù)據(jù)庫(kù)主機(jī)地址 user='root', # 數(shù)據(jù)庫(kù)用戶名 passwd='root', # 數(shù)據(jù)庫(kù)密碼 database='test' # 要連接的數(shù)據(jù)庫(kù))#數(shù)據(jù)庫(kù)建表指令sql = '''CREATE TABLE `test`.`testtable` ( `id` int NOT NULL, `name` varchar(255) NULL, `age` int NULL, `address` varchar(255) NULL, PRIMARY KEY (`id`) );'''#獲取數(shù)據(jù)庫(kù)操作游標(biāo)myCursor=connect.cursor()#執(zhí)行sql語(yǔ)句myCursor.execute(sql)#提交給數(shù)據(jù)庫(kù)執(zhí)行命令connect.commit()connect.close()
執(zhí)行后會(huì)創(chuàng)建一個(gè)名為testtabe的表
3.2插入數(shù)據(jù)
import mysql.connectorconnect = mysql.connector.connect( host='127.0.0.1', # 數(shù)據(jù)庫(kù)主機(jī)地址 user='root', # 數(shù)據(jù)庫(kù)用戶名 passwd='root', # 數(shù)據(jù)庫(kù)密碼 database='test' # 要連接的數(shù)據(jù)庫(kù))# 數(shù)據(jù)庫(kù)插入指令,待定字符無(wú)論是數(shù)值還是文字,都需要用%ssql = 'INSERT INTO `test`.`testtable`(`id`, `name`, `age`, `address`) VALUES (%s,%s,%s,%s)'var = (1, ’windSnowLi’, 20, ’中國(guó)’)# 獲取數(shù)據(jù)庫(kù)操作游標(biāo)myCursor = connect.cursor()try: # 執(zhí)行sql語(yǔ)句 myCursor.execute(sql, var) # 提交給數(shù)據(jù)庫(kù)執(zhí)行命令 connect.commit()except : #回滾,以防出現(xiàn)錯(cuò)誤 connect.rollback()connect.close()
隨后檢查數(shù)據(jù)庫(kù)
3.3查詢語(yǔ)句
import mysql.connectorconnect = mysql.connector.connect( host='127.0.0.1', # 數(shù)據(jù)庫(kù)主機(jī)地址 user='root', # 數(shù)據(jù)庫(kù)用戶名 passwd='root', # 數(shù)據(jù)庫(kù)密碼 database='test' # 要連接的數(shù)據(jù)庫(kù))# 數(shù)據(jù)庫(kù)查詢指令sql = 'select * from testtable'# 獲取數(shù)據(jù)庫(kù)操作游標(biāo)myCursor = connect.cursor()try: # 執(zhí)行sql語(yǔ)句 myCursor.execute(sql) results = myCursor.fetchall() print(results)except : print('查詢失敗')connect.close()
3.4更新數(shù)據(jù)
import mysql.connectorconnect = mysql.connector.connect( host='127.0.0.1', # 數(shù)據(jù)庫(kù)主機(jī)地址 user='root', # 數(shù)據(jù)庫(kù)用戶名 passwd='root', # 數(shù)據(jù)庫(kù)密碼 database='test' # 要連接的數(shù)據(jù)庫(kù))# 數(shù)據(jù)庫(kù)更新指令sql = 'UPDATE `test`.`testtable` SET `id` = 2, `name` = ’mirror’, `age` = 19, `address` = ’祖國(guó)’ WHERE `id` = 1'# 獲取數(shù)據(jù)庫(kù)操作游標(biāo)myCursor = connect.cursor()try: # 執(zhí)行sql語(yǔ)句 myCursor.execute(sql) # 提交給數(shù)據(jù)庫(kù)執(zhí)行命令 connect.commit()except : #回滾,以防出現(xiàn)錯(cuò)誤 connect.rollback()connect.close()
3.5刪除數(shù)據(jù)
import mysql.connectorconnect = mysql.connector.connect( host='127.0.0.1', # 數(shù)據(jù)庫(kù)主機(jī)地址 user='root', # 數(shù)據(jù)庫(kù)用戶名 passwd='root', # 數(shù)據(jù)庫(kù)密碼 database='test' # 要連接的數(shù)據(jù)庫(kù))# 數(shù)據(jù)庫(kù)刪除指令sql = 'DELETE FROM `test`.`testtable` WHERE `id` = 1'# 獲取數(shù)據(jù)庫(kù)操作游標(biāo)myCursor = connect.cursor()try: # 執(zhí)行sql語(yǔ)句 myCursor.execute(sql) # 提交給數(shù)據(jù)庫(kù)執(zhí)行命令 connect.commit()except : #回滾,以防出現(xiàn)錯(cuò)誤 connect.rollback()connect.close()
4.說(shuō)明
sql語(yǔ)句中如果有待定字符,則都可以通過(guò)
sql = 'INSERT INTO `test`.`testtable`(`id`, `name`, `age`, `address`) VALUES (%s,%s,%s,%s)'var = (1, ’windSnowLi’, 20, ’中國(guó)’)
這種方式拼接,不過(guò)執(zhí)行時(shí)需要myCursor.execute(sql, var)將參數(shù)也同步傳入
到此這篇關(guān)于Python連接Mysql進(jìn)行增刪改查的示例代碼的文章就介紹到這了,更多相關(guān)Python連接Mysql增刪改查內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 存儲(chǔ)于xml中需要的HTML轉(zhuǎn)義代碼2. 完美解決vue 中多個(gè)echarts圖表自適應(yīng)的問(wèn)題3. python 浮點(diǎn)數(shù)四舍五入需要注意的地方4. JAMon(Java Application Monitor)備忘記5. Springboot 全局日期格式化處理的實(shí)現(xiàn)6. SpringBoot+TestNG單元測(cè)試的實(shí)現(xiàn)7. python開(kāi)發(fā)一款翻譯工具8. 利用CSS制作3D動(dòng)畫9. Java GZip 基于內(nèi)存實(shí)現(xiàn)壓縮和解壓的方法10. jsp+servlet簡(jiǎn)單實(shí)現(xiàn)上傳文件功能(保存目錄改進(jìn))
