Python基礎 Python連線MySQL

2021-08-15 09:15:01 字數 3259 閱讀 4858

什麼是 pymysql?

pymysql 是在 python3.x 版本中用於連線 mysql 伺服器的乙個庫,python2中則使用mysqldb。

pymysql 遵循 python 資料庫 api v2.0 規範,幷包含了 pure-python mysql 客戶端庫。

pymysql 安裝

在使用 pymysql 之前,我們需要確保 pymysql 已安裝。

如果還未安裝,我們可以使用以下命令安裝最新版的 pymysql:

$ pip3 install pymysql

資料庫連線

通過如下**測試資料庫連線

#!/usr/bin/python3

import pymysql

# 開啟資料庫連線

db = pymysql.connect("localhost","root","123456","mydb" )

# 使用 cursor() 方法建立乙個游標物件 cursor

cursor = db.cursor()

# 使用 execute() 方法執行 sql 查詢

cursor.execute("select version()")

# 使用 fetchone() 方法獲取單條資料.

data = cursor.fetchone()

print ("database version : %s " % data)

# 關閉資料庫連線

db.close()

執行資料新增

#!/usr/bin/python3

import pymysql

# 開啟資料庫連線

db = pymysql.connect("localhost","root","","mydemo" )

# 使用 cursor() 方法建立乙個游標物件 cursor

cursor = db.cursor()

# sql 插入語句

sql = "insert into stu(name,***,age,classid) values('%s','%c','%d','%s')" % ('uu142','m',22,'lamp180')

try:

# 執行sql語句

cursor.execute(sql)

# 執行sql語句

db.commit()

print("ok: %d " % (cursor.rowcount))

except:

# 發生錯誤時回滾

db.rollback()

# 關閉資料庫連線

db.close()

執行刪除操作

#!/usr/bin/python3

import pymysql

# 開啟資料庫連線

db = pymysql.connect("localhost","root","","mydemo" )

# 使用 cursor() 方法建立乙個游標物件 cursor

cursor = db.cursor()

# sql 刪除語句

sql = "delete from stu where id = '%d'" % (13)

try:

# 執行sql語句

cursor.execute(sql)

# 提交修改

db.commit()

except:

# 發生錯誤時回滾

db.rollback()

# 關閉資料庫連線

db.close()

執行資料修改|更新

#!/usr/bin/python3

import pymysql

# 開啟資料庫連線

db = pymysql.connect("localhost","testuser","test123","testdb" )

# 使用cursor()方法獲取操作游標

cursor = db.cursor()

# sql 更新語句

sql = "update employee set age = age + 1

where *** = '%c'" % ('m')

try:

# 執行sql語句

cursor.execute(sql)

# 提交到資料庫執行

db.commit()

except:

# 發生錯誤時回滾

db.rollback()

# 關閉資料庫連線

db.close()

執行資料查詢

資料庫查詢操作:

python查詢mysql使用 fetchone() 方法獲取單條資料, 使用fetchall() 方法獲取多條資料。

fetchone(): 該方法獲取下乙個查詢結果集。結果集是乙個物件

fetchall(): 接收全部的返回結果行.

rowcount: 這是乙個唯讀屬性,並返回執行execute()方法後影響的行數。

#!/usr/bin/python3

import pymysql

# 開啟資料庫連線

db = pymysql.connect("localhost","root","","mydemo" )

# 使用 cursor() 方法建立乙個游標物件 cursor

cursor = db.cursor()

# sql 查詢語句

sql = "select * from stu limit %d" % (3)

#sql = "select * from stu"

try:

# 執行sql語句

cursor.execute(sql)

# 獲取所有記錄列表

results = cursor.fetchall()

for row in results:

id = row[0]

name = row[1]

*** = row[2]

age = row[3]

classid = row[4]

# 列印結果

print ("id=%d,name=%s,***=%s,age=%d,classid=%s" % (id,name,***,age,classid))

except:

print ("error: unable to fetch data")

# 關閉資料庫連線

db.close()

python學習交流、資源共享群:563626388 qq

Python基礎 Python語法基礎

關鍵字是python語言的關鍵組成部分,不可隨便作為其他物件的識別符號 andas assert break class continue defdel elif else except exec finally forfrom global ifimport inis lambda notor p...

Python程式設計基礎之Python基礎

1.只能是乙個詞 2.包含字母,數字和下劃線 3.不能以數字開頭 this program syas hello and asks for your name print hello world1 print what is your name?ask for their name myname i...

Python基礎 介紹python基礎語法

型別可以混合,不必是單一的資料型別 操作和字串一樣,號和 號與字串操作也相同 成員運算子 in 在 not in 不在 用來判斷乙個元素是否在一組元素中,返回bool型別。身份運算子 is 是 is not 不是 位運算子 按位與 按位或 按位異或 按位取反 左移 右移 遍歷序列 list a 1,...