Python操做MySql --Python3

Python版本:v3.7

模塊:pymysql

一、鏈接數據庫

connectDB.py:mysql

# coding:utf-8
import pymysql

host = 'localhost' # 主機
username = 'root' # 用戶名
pwd = 'nxl123' # 密碼
dbName = 'testdb' # 數據庫名
# 打開數據庫鏈接
db = pymysql.connect(host, username, pwd, dbName)
# 經過cursor方法獲取操做遊標
cursor = db.cursor()
# sql語句
sql = 'SELECT VERSION()'
# 執行sql語句
cursor.execute(sql)
# 使用fetchone方法獲取一個查詢結果集
data = cursor.fetchone()
# 輸出結果集
print('db version:%s' % data)
# 關閉數據庫鏈接
db.close()

 

二、建立表

createTables.py:

# coding:utf-8
import pymysql

host = 'localhost' # 主機
username = 'root' # 用戶名
pwd = 'nxl123' # 密碼
dbName = 'testdb' # 數據庫名
# 打開數據庫鏈接
db = pymysql.connect(host, username, pwd, dbName)
# 經過cursor方法獲取操做遊標
cursor = db.cursor()
# 執行sql語句,若是存在stusex表,就將其刪除
cursor.execute('DROP TABLE IF EXISTS stu')
cursor.execute('DROP TABLE IF EXISTS sex')
# sql語句建立stu
sql = '''CREATE TABLE stu(
stuId SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
stuNo VARCHAR(20) NOT NULL UNIQUE KEY,
stuName VARCHAR(10) NOT NULL,
age SMALLINT UNSIGNED NOT NULL,
sexId SMALLINT UNSIGNED NOT NULL)'''
sql2 = '''CREATE TABLE sex(
id SMALLINT UNSIGNED PRIMARY KEY,
sex ENUM('','') NOT NULL)'''
# 執行sql、sql2語句
cursor.execute(sql)
cursor.execute(sql2)
# 關閉數據庫鏈接
db.close()

 

三、插入數據

insertData.py:sql

# coding:utf-8
import pymysql

host = 'localhost' # 主機
username = 'root' # 用戶名
pwd = 'nxl123' # 密碼
dbName = 'testdb' # 數據庫名
db = pymysql.connect(host, username, pwd, dbName)
# 經過cursor方法獲取操做遊標
cursor = db.cursor()
# sql語句
# sql = '''INSERT stu(stuNo,stuName,age,sexId) VALUES('2015011070','Thanlon',22,1) '''
# sql = '''INSERT stu(stuNo,stuName,age,sexId) VALUES('2015011071','Maria',20,2) '''
# sql = '''INSERT sex(id,sex) VALUES(1,'')'''
sql = '''INSERT sex(id,sex) VALUES(2,'')'''
try:
# 執行sql語句
cursor.execute(sql)
# 提交到數據庫執行
db.commit()
except:
# 發生錯誤時回滾
db.rollback()
# 關閉數據庫鏈接
db.close()

stu 表:

sex 表:

四、刪除數據

deleteData.py:

# coding:utf-8
import pymysql

host = 'localhost' # 主機
username = 'root' # 用戶名
pwd = 'nxl123' # 密碼
dbName = 'testdb' # 數據庫名
# 打開數據庫鏈接
db = pymysql.connect('localhost', username, pwd, dbName)
# 經過cursor方法獲取操做遊標
cursor = db.cursor()
sql = "DELETE FROM stu WHERE age>'%d'" % (20)
try:
# 執行sql語句
cursor.execute(sql)
# 提交到數據庫執行
db.commit()
except:
# 語句產生異常時打印提示信息
print('更新數據時出現異常!')
finally:
# 關閉數據庫鏈接
db.close()

stu表:(刪除數據後的stu表)

五、查詢數據

selectData.py:

# coding:utf-8
import pymysql

host = 'localhost' # 主機
username = 'root' # 用戶名
pwd = 'nxl123' # 密碼
dbName = 'testdb' # 數據庫名
# 打開數據庫鏈接
db = pymysql.connect(host, username, pwd, dbName)
# 經過cursor方法獲取操做遊標
cursor = db.cursor()
# sql = '''SELECT *FROM stu'''
sql = '''SELECT stuID,stuNo,stuName,age,x.sex FROM sex AS x INNER JOIN stu AS s ON s.sexId = x.id'''
try:
# 執行sql語句
cursor.execute(sql)
# results接收所有的返回結果行
results = cursor.fetchall()
# print(results)
# 返回執行execute方法後影響的行數
results_count = cursor.rowcount
# 打印輸出影響的行數
print('execute()方法執行後影響的行數:%d' % results_count)
# 遍歷結果集
for row in results:
stuID = row[0]
stuNo = row[1]
stuName = row[2]
age = row[3]
sex = row[4]
# 打印查詢結果
print(stuID, stuNo, stuName, age, sex)
except:
print('獲取數據出現異常!')
finally:
# 關閉數據庫鏈接
db.close()

六、修改數據

updateData.py:

# coding:utf-8
import pymysql

host = 'localhost' # 主機
username = 'root' # 用戶名
pwd = 'nxl123' # 密碼
dbName = 'testdb' # 數據庫名
# 打開數據庫鏈接
db = pymysql.connect(host, username, pwd, dbName)
# 經過cursor方法獲取操做遊標
cursor = db.cursor()
# 將性別爲男的學生年齡加1
sql = '''UPDATE stu SET age=age+1 WHERE sexId=1'''
try:
# 執行語句
cursor.execute(sql)
# 提交到數據庫執行
db.commit()
except:
print('更新數據時出現異常!')
finally:
# 關閉數據庫鏈接
db.close()

執行語句前stu表信息:

執行語句後stu表信息:

相關文章
相關標籤/搜索