Python連接Mysql進(jìn)行增刪改查的示例代碼
Python連接Mysql
1.安裝對應(yīng)的庫
使用Python連接Mysql數(shù)據(jù)庫需要安裝相應(yīng)的庫
以管理員身份運(yùn)行cmd,輸入命令
pip install mysql.connector
安裝完成后建立test.py寫入import mysql.connector保存后運(yùn)行python test.py用以測試模塊庫是否安裝完成,如果不報(bào)錯,說明安裝完成
2.進(jìn)行連接測試
編寫connectTest.py文件文件內(nèi)容:
import mysql.connectorconnect = mysql.connector.connect( host='127.0.0.1', # 數(shù)據(jù)庫主機(jī)地址 user='root', # 數(shù)據(jù)庫用戶名 passwd='root', # 數(shù)據(jù)庫密碼 database='mysql' # 要連接的數(shù)據(jù)庫)#關(guān)閉連接connect.close()
運(yùn)行文件python connectTest.py如果沒有報(bào)錯提示說明連接成功,如果報(bào)錯提示
說明連接失敗,請檢查賬戶、密碼以及數(shù)據(jù)庫是否正確,查看數(shù)據(jù)庫是否開機(jī)
3.執(zhí)行sql命令
3.1創(chuàng)建表
import mysql.connectorconnect = mysql.connector.connect( host='127.0.0.1', # 數(shù)據(jù)庫主機(jī)地址 user='root', # 數(shù)據(jù)庫用戶名 passwd='root', # 數(shù)據(jù)庫密碼 database='test' # 要連接的數(shù)據(jù)庫)#數(shù)據(jù)庫建表指令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ù)庫操作游標(biāo)myCursor=connect.cursor()#執(zhí)行sql語句myCursor.execute(sql)#提交給數(shù)據(jù)庫執(zhí)行命令connect.commit()connect.close()
執(zhí)行后會創(chuàng)建一個名為testtabe的表
3.2插入數(shù)據(jù)
import mysql.connectorconnect = mysql.connector.connect( host='127.0.0.1', # 數(shù)據(jù)庫主機(jī)地址 user='root', # 數(shù)據(jù)庫用戶名 passwd='root', # 數(shù)據(jù)庫密碼 database='test' # 要連接的數(shù)據(jù)庫)# 數(shù)據(jù)庫插入指令,待定字符無論是數(shù)值還是文字,都需要用%ssql = 'INSERT INTO `test`.`testtable`(`id`, `name`, `age`, `address`) VALUES (%s,%s,%s,%s)'var = (1, ’windSnowLi’, 20, ’中國’)# 獲取數(shù)據(jù)庫操作游標(biāo)myCursor = connect.cursor()try: # 執(zhí)行sql語句 myCursor.execute(sql, var) # 提交給數(shù)據(jù)庫執(zhí)行命令 connect.commit()except : #回滾,以防出現(xiàn)錯誤 connect.rollback()connect.close()
隨后檢查數(shù)據(jù)庫
3.3查詢語句
import mysql.connectorconnect = mysql.connector.connect( host='127.0.0.1', # 數(shù)據(jù)庫主機(jī)地址 user='root', # 數(shù)據(jù)庫用戶名 passwd='root', # 數(shù)據(jù)庫密碼 database='test' # 要連接的數(shù)據(jù)庫)# 數(shù)據(jù)庫查詢指令sql = 'select * from testtable'# 獲取數(shù)據(jù)庫操作游標(biāo)myCursor = connect.cursor()try: # 執(zhí)行sql語句 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ù)庫主機(jī)地址 user='root', # 數(shù)據(jù)庫用戶名 passwd='root', # 數(shù)據(jù)庫密碼 database='test' # 要連接的數(shù)據(jù)庫)# 數(shù)據(jù)庫更新指令sql = 'UPDATE `test`.`testtable` SET `id` = 2, `name` = ’mirror’, `age` = 19, `address` = ’祖國’ WHERE `id` = 1'# 獲取數(shù)據(jù)庫操作游標(biāo)myCursor = connect.cursor()try: # 執(zhí)行sql語句 myCursor.execute(sql) # 提交給數(shù)據(jù)庫執(zhí)行命令 connect.commit()except : #回滾,以防出現(xiàn)錯誤 connect.rollback()connect.close()
3.5刪除數(shù)據(jù)
import mysql.connectorconnect = mysql.connector.connect( host='127.0.0.1', # 數(shù)據(jù)庫主機(jī)地址 user='root', # 數(shù)據(jù)庫用戶名 passwd='root', # 數(shù)據(jù)庫密碼 database='test' # 要連接的數(shù)據(jù)庫)# 數(shù)據(jù)庫刪除指令sql = 'DELETE FROM `test`.`testtable` WHERE `id` = 1'# 獲取數(shù)據(jù)庫操作游標(biāo)myCursor = connect.cursor()try: # 執(zhí)行sql語句 myCursor.execute(sql) # 提交給數(shù)據(jù)庫執(zhí)行命令 connect.commit()except : #回滾,以防出現(xiàn)錯誤 connect.rollback()connect.close()
4.說明
sql語句中如果有待定字符,則都可以通過
sql = 'INSERT INTO `test`.`testtable`(`id`, `name`, `age`, `address`) VALUES (%s,%s,%s,%s)'var = (1, ’windSnowLi’, 20, ’中國’)
這種方式拼接,不過執(zhí)行時需要myCursor.execute(sql, var)將參數(shù)也同步傳入
到此這篇關(guān)于Python連接Mysql進(jìn)行增刪改查的示例代碼的文章就介紹到這了,更多相關(guān)Python連接Mysql增刪改查內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. python中scrapy處理項(xiàng)目數(shù)據(jù)的實(shí)例分析2. 教你在 IntelliJ IDEA 中使用 VIM插件的詳細(xì)教程3. GIT相關(guān)-IDEA/ECLIPSE工具配置的教程詳解4. Django ORM實(shí)現(xiàn)按天獲取數(shù)據(jù)去重求和例子5. Python requests庫參數(shù)提交的注意事項(xiàng)總結(jié)6. 快速搭建Spring Boot+MyBatis的項(xiàng)目IDEA(附源碼下載)7. js抽獎轉(zhuǎn)盤實(shí)現(xiàn)方法分析8. IntelliJ IDEA導(dǎo)入jar包的方法9. 如何基于Python實(shí)現(xiàn)word文檔重新排版10. vue-electron中修改表格內(nèi)容并修改樣式
