欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

python連mysql數(shù)據(jù)庫(kù)的方法

小編給大家分享一下python連MySQL數(shù)據(jù)庫(kù)的方法,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

成都創(chuàng)新互聯(lián)專(zhuān)業(yè)為企業(yè)提供宜章網(wǎng)站建設(shè)、宜章做網(wǎng)站、宜章網(wǎng)站設(shè)計(jì)、宜章網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、宜章企業(yè)網(wǎng)站模板建站服務(wù),10年宜章做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。

一、Python連接MySQL數(shù)據(jù)庫(kù)

1、導(dǎo)入模塊

#導(dǎo)入模塊
import pymysql

相關(guān)課程推薦:Python基礎(chǔ)視頻教程(python語(yǔ)言基礎(chǔ))

2、打開(kāi)數(shù)據(jù)庫(kù)連接

#打開(kāi)數(shù)據(jù)庫(kù)連接
#注意:這里已經(jīng)假定存在數(shù)據(jù)庫(kù)testdb,db指定了連接的數(shù)據(jù)庫(kù),當(dāng)然這個(gè)參數(shù)也可以沒(méi)有
db = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='1234', db='testdb', charset='utf8')

3、創(chuàng)建游標(biāo)對(duì)象cursor

#使用cursor方法創(chuàng)建一個(gè)游標(biāo)
cursor = db.cursor()

二、數(shù)據(jù)庫(kù)基本操作

使用execute()方法來(lái)實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)的基本操作。

1、查詢數(shù)據(jù)庫(kù)版本

#查詢數(shù)據(jù)庫(kù)版本
cursor.execute("select version()")
data = cursor.fetchone()
print(" Database Version:%s" % data)

2、創(chuàng)建數(shù)據(jù)庫(kù)

#創(chuàng)建數(shù)據(jù)庫(kù)test
cursor.execute("drop database if exists test")  #如果數(shù)據(jù)庫(kù)已經(jīng)存在,那么刪除后重新創(chuàng)建
sql = "create database test"
cursor.execute(sql)

3、創(chuàng)建數(shù)據(jù)表

#創(chuàng)建數(shù)據(jù)庫(kù)表
cursor.execute("drop table if exists employee")  #如果數(shù)據(jù)表已經(jīng)存在,那么刪除后重新創(chuàng)建
sql = """
CREATE TABLE EMPLOYEE (
FIRST_NAME CHAR(20) NOT NULL,
LAST_NAME CHAR(20),
AGE INT,
SEX CHAR(1),
INCOME FLOAT )
"""
cursor.execute(sql)

4、查詢操作

#查詢數(shù)據(jù)表數(shù)據(jù)
sql = "select * from employee"
cursor.execute(sql)
data = cursor.fetchone()
print(data)

5、插入操作

#插入數(shù)據(jù)
sql = "insert into employee values ('李','梅',20,'W',5000)"
cursor.execute(sql)
db.commit()
#查看插入后的結(jié)果
sql = "select * from employee"
cursor.execute(sql)
data = cursor.fetchone()
print(data)

6、指定條件查詢數(shù)據(jù)

#指定條件查詢數(shù)據(jù)表數(shù)據(jù)
sql = " select * from employee where income > '%d' " % (1000)
cursor.execute(sql)
data = cursor.fetchone()
print(data)

7、更新操作

#更新數(shù)據(jù)庫(kù)
sql = " update employee set age = age+1 where sex = '%c' " % ('W')
cursor.execute(sql)
db.commit()
#查看更新后的結(jié)果
sql = "select * from employee"
cursor.execute(sql)
data = cursor.fetchone()
print(data)

8、刪除操作

#刪除數(shù)據(jù)
sql = " delete from employee where age > '%d' " % (30)
cursor.execute(sql)
db.commit()
#查看更新后的結(jié)果
sql = "select * from employee"
cursor.execute(sql)
data = cursor.fetchone()
print(data)

三、關(guān)閉數(shù)據(jù)庫(kù)連接

db.close()

四、其他

1、說(shuō)明

·上例中"sql=..."語(yǔ)句,是經(jīng)典的MySQL語(yǔ)句的形式,將數(shù)據(jù)庫(kù)語(yǔ)句寫(xiě)在雙引號(hào)內(nèi),形成類(lèi)似字符串的形式;

·使用cursor對(duì)象的execute()方法具體執(zhí)行數(shù)據(jù)庫(kù)的操作;

·對(duì)于插入、更新、刪除等操作,需要使用db.commit()來(lái)提交到數(shù)據(jù)庫(kù)執(zhí)行,對(duì)于查詢、創(chuàng)建數(shù)據(jù)庫(kù)和數(shù)據(jù)表的操作不需要此語(yǔ)句。

2、為有效避免因?yàn)殄e(cuò)誤導(dǎo)致的后果,使用以下方式來(lái)執(zhí)行數(shù)據(jù)庫(kù)的操作:

try:
  # 執(zhí)行 SQL 語(yǔ)句
  cursor.execute(sql)
  # 提交修改
  db.commit()
except:
  # 發(fā)生錯(cuò)誤時(shí)回滾
  db.rollback()

以上是python連mysql數(shù)據(jù)庫(kù)的方法的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

當(dāng)前題目:python連mysql數(shù)據(jù)庫(kù)的方法
轉(zhuǎn)載源于:http://chinadenli.net/article2/gsjhoc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作電子商務(wù)面包屑導(dǎo)航品牌網(wǎng)站建設(shè)App設(shè)計(jì)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司