国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Python連接Mysql進(jìn)行增刪改查的示例代碼

瀏覽:170日期:2022-07-15 11:39:02

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.connectorconnect = mysql.connector.connect( host='127.0.0.1', # 數(shù)據(jù)庫(kù)主機(jī)地址 user='root', # 數(shù)據(jù)庫(kù)用戶(hù)名 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ò)提示

Python連接Mysql進(jìn)行增刪改查的示例代碼

說(shuō)明連接失敗,請(qǐng)檢查賬戶(hù)、密碼以及數(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ù)用戶(hù)名 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的表

Python連接Mysql進(jìn)行增刪改查的示例代碼

3.2插入數(shù)據(jù)

Python連接Mysql進(jìn)行增刪改查的示例代碼

import mysql.connectorconnect = mysql.connector.connect( host='127.0.0.1', # 數(shù)據(jù)庫(kù)主機(jī)地址 user='root', # 數(shù)據(jù)庫(kù)用戶(hù)名 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ù)

Python連接Mysql進(jìn)行增刪改查的示例代碼

3.3查詢(xún)語(yǔ)句

import mysql.connectorconnect = mysql.connector.connect( host='127.0.0.1', # 數(shù)據(jù)庫(kù)主機(jī)地址 user='root', # 數(shù)據(jù)庫(kù)用戶(hù)名 passwd='root', # 數(shù)據(jù)庫(kù)密碼 database='test' # 要連接的數(shù)據(jù)庫(kù))# 數(shù)據(jù)庫(kù)查詢(xún)指令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('查詢(xún)失敗')connect.close()

Python連接Mysql進(jìn)行增刪改查的示例代碼

3.4更新數(shù)據(jù)

import mysql.connectorconnect = mysql.connector.connect( host='127.0.0.1', # 數(shù)據(jù)庫(kù)主機(jī)地址 user='root', # 數(shù)據(jù)庫(kù)用戶(hù)名 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()

Python連接Mysql進(jìn)行增刪改查的示例代碼

3.5刪除數(shù)據(jù)

import mysql.connectorconnect = mysql.connector.connect( host='127.0.0.1', # 數(shù)據(jù)庫(kù)主機(jī)地址 user='root', # 數(shù)據(jù)庫(kù)用戶(hù)名 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()

Python連接Mysql進(jìn)行增刪改查的示例代碼

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)!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 麻阳| 宿州市| 开平市| 龙南县| 奈曼旗| 兴和县| 文水县| 延寿县| 贵州省| 西林县| 永清县| 东乡族自治县| 遵义县| 东山县| 化德县| 麻城市| 拜城县| 贺州市| 泽州县| 吉水县| 明溪县| 临高县| 犍为县| 承德县| 淅川县| 曲麻莱县| 麟游县| 南川市| 饶平县| 夏邑县| 宝应县| 元朗区| 蓬溪县| 松阳县| 华坪县| 皋兰县| 乌兰浩特市| 瓮安县| 益阳市| 阆中市| 贵州省|