亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時(shí)間:2020年08月03日 14:22:21   作者:windSnowLi  
這篇文章主要介紹了Python連接Mysql進(jìn)行增刪改查的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論