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
寫(xiě)入
import mysql.connector
保存后運(yùn)行
python test.py
用以測(cè)試模塊庫(kù)是否安裝完成,如果不報(bào)錯(cuò),說(shuō)明安裝完成
2.進(jìn)行連接測(cè)試
編寫(xiě)connectTest.py
文件
文件內(nèi)容:
import mysql.connector connect = 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.connector connect = 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.connector connect = 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ù)值還是文字,都需要用%s sql = "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.connector connect = 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.connector connect = 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.connector connect = 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)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python實(shí)現(xiàn)mysql的讀寫(xiě)分離及負(fù)載均衡
- Python實(shí)現(xiàn)自定義讀寫(xiě)分離代碼實(shí)例
- Python暴力破解Mysql數(shù)據(jù)的示例
- python查詢MySQL將數(shù)據(jù)寫(xiě)入Excel
- Python操控mysql批量插入數(shù)據(jù)的實(shí)現(xiàn)方法
- python對(duì) MySQL 數(shù)據(jù)庫(kù)進(jìn)行增刪改查的腳本
- Python連接mysql數(shù)據(jù)庫(kù)及簡(jiǎn)單增刪改查操作示例代碼
- Python操作MySQL數(shù)據(jù)庫(kù)的示例代碼
- Python web框架(django,flask)實(shí)現(xiàn)mysql數(shù)據(jù)庫(kù)讀寫(xiě)分離的示例
相關(guān)文章
Python unittest 簡(jiǎn)單實(shí)現(xiàn)參數(shù)化的方法
今天小編就為大家分享一篇Python unittest 簡(jiǎn)單實(shí)現(xiàn)參數(shù)化的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-11-11python基礎(chǔ)--除法(/,//,%)的應(yīng)用說(shuō)明
這篇文章主要介紹了python基礎(chǔ)--除法(/,//,%)的應(yīng)用說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03解決CentOS下ImportError: No module named &a
這篇文章主要介紹了解決CentOS下ImportError: No module named '_sqlite3'的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12pytorch: tensor類(lèi)型的構(gòu)建與相互轉(zhuǎn)換實(shí)例
今天小編就為大家分享一篇pytorch: tensor類(lèi)型的構(gòu)建與相互轉(zhuǎn)換實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07Python 操作 PostgreSQL 數(shù)據(jù)庫(kù)示例【連接、增刪改查等】
這篇文章主要介紹了Python 操作 PostgreSQL 數(shù)據(jù)庫(kù)的方法,結(jié)合實(shí)例形式分析了Python 連接PostgreSQL及增刪改查等相關(guān)操作技巧,需要的朋友可以參考下2020-04-04python實(shí)現(xiàn)庫(kù)存商品管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)庫(kù)存商品管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02Python實(shí)現(xiàn)學(xué)校管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)學(xué)校管理系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01python實(shí)現(xiàn)在windows服務(wù)中新建進(jìn)程的方法
這篇文章主要介紹了python實(shí)現(xiàn)在windows服務(wù)中新建進(jìn)程的方法,涉及Python針對(duì)Windows服務(wù)與進(jìn)程操作的相關(guān)技巧,需要的朋友可以參考下2015-06-06