Python基础44(PyMySQL模块)
生活随笔
收集整理的這篇文章主要介紹了
Python基础44(PyMySQL模块)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
PyMySQL安裝方法
pip install pymysql連接數(shù)據(jù)庫(kù)
注意事項(xiàng)
在進(jìn)行本文以下內(nèi)容之前需要注意:
- 你有一個(gè)MySQL數(shù)據(jù)庫(kù),并且已經(jīng)啟動(dòng)。
- 你有可以連接該數(shù)據(jù)庫(kù)的用戶名和密碼
- 你有一個(gè)有權(quán)限操作的database
基本使用
# 導(dǎo)入pymysql模塊import pymysql# 鏈接庫(kù)(database) conn = pymysql.connect(host="127.0.0.1", # host后面填寫(xiě)需要鏈接的數(shù)據(jù)庫(kù)地址user="root", # 用戶名password="123456", # 密碼database="human", # 數(shù)據(jù)庫(kù)名charset="utf8" # 編碼方式 )# 得到一個(gè)可以執(zhí)行的SQL語(yǔ)句的光標(biāo) cursor = conn.cursor()# 定義要執(zhí)行的SQL語(yǔ)句 sql = 'select * from info;'# 執(zhí)行SQL語(yǔ)句 cursor.execute(sql)# 關(guān)閉光標(biāo)對(duì)象 cursor.close()# 關(guān)閉數(shù)據(jù)庫(kù)連接 conn.close()返回字典格式的數(shù)據(jù)
import pymysqlconn = pymysql.connect(host="127.0.0.1",user="root",password="123456",database="human",charset="utf8" )# 得到一個(gè)可以執(zhí)行SQL語(yǔ)句并且將結(jié)果作為字典返回的游標(biāo) cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)sql = 'select name from class;'# 執(zhí)行sql語(yǔ)句 cursor.execute(sql)# 返回查詢到的所有數(shù)據(jù) ret = cursor.fetchall()# 返回查詢到的指定條數(shù)數(shù)據(jù) # ret = cursor.fetchmany(5)print(ret)# 關(guān)閉鏈接 cursor.close() conn.close()注意:
charset=“utf8”,編碼不要寫(xiě)成"utf-8"
增刪改查操作
增
import pymysqlconn = pymysql.connect(host="127.0.0.1",user="root",password="123456",database="human",charset="utf8" )cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # 注意此處傳值全部使用%s做占位符即可 sql = "insert into class(name, age, sex, salary) VALUES(%s,%s,%s,%s)" name = 'abcd' age = '33' sex = '男' salary = '120.02'# 執(zhí)行sql語(yǔ)句 cursor.execute(sql, [name, age, sex, salary])# 提交事務(wù) conn.commit()cursor.close() conn.close()插入數(shù)據(jù)失敗回滾
import pymysql conn = pymysql.connect(host="127.0.0.1",user="root",password="123456",database="human",charset="utf8" )cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)sql = "INSERT into class(name, age, sex, salary) values(%s,%s,%s,%s);"name = "ss" age = "22" sex = "男22" salary="1234.2" try:cursor.execute(sql, [name, age, sex, salary])# 提交事務(wù) conn.commit() except Exception:print('出錯(cuò)')# 有異常回滾事務(wù) conn.rollback()cursor.close() conn.close()獲取插入數(shù)據(jù)的ID(關(guān)聯(lián)操作時(shí)會(huì)用到)
# 導(dǎo)入pymysql模塊 import pymysql # 連接database conn = pymysql.connect(host=“你的數(shù)據(jù)庫(kù)地址”, user=“用戶名”,password=“密碼”,database=“數(shù)據(jù)庫(kù)名”,charset=“utf8”) # 得到一個(gè)可以執(zhí)行SQL語(yǔ)句的光標(biāo)對(duì)象 cursor = conn.cursor() sql = "INSERT INTO USER1(name, age) VALUES (%s, %s);" username = "Alex" age = 18 try:# 執(zhí)行SQL語(yǔ)句 cursor.execute(sql, [username, age])# 提交事務(wù) conn.commit()# 提交之后,獲取剛插入的數(shù)據(jù)的IDlast_id = cursor.lastrowid except Exception as e:# 有異常,回滾事務(wù) conn.rollback() cursor.close() conn.close()批量執(zhí)行
# 導(dǎo)入pymysql模塊 import pymysql # 連接database conn = pymysql.connect(host=“你的數(shù)據(jù)庫(kù)地址”, user=“用戶名”,password=“密碼”,database=“數(shù)據(jù)庫(kù)名”,charset=“utf8”) # 得到一個(gè)可以執(zhí)行SQL語(yǔ)句的光標(biāo)對(duì)象 cursor = conn.cursor() sql = "INSERT INTO USER1(name, age) VALUES (%s, %s);" data = [("Alex", 18), ("Egon", 20), ("Yuan", 21)] try:# 批量執(zhí)行多條插入SQL語(yǔ)句 cursor.executemany(sql, data)# 提交事務(wù) conn.commit() except Exception as e:# 有異常,回滾事務(wù) conn.rollback() cursor.close() conn.close()刪
# 導(dǎo)入pymysql模塊 import pymysql # 連接database conn = pymysql.connect(host=“你的數(shù)據(jù)庫(kù)地址”, user=“用戶名”,password=“密碼”,database=“數(shù)據(jù)庫(kù)名”,charset=“utf8”) # 得到一個(gè)可以執(zhí)行SQL語(yǔ)句的光標(biāo)對(duì)象 cursor = conn.cursor() sql = "DELETE FROM USER1 WHERE id=%s;" try:cursor.execute(sql, [4])# 提交事務(wù) conn.commit() except Exception as e:# 有異常,回滾事務(wù) conn.rollback() cursor.close() conn.close()改
# 導(dǎo)入pymysql模塊 import pymysql # 連接database conn = pymysql.connect(host=“你的數(shù)據(jù)庫(kù)地址”, user=“用戶名”,password=“密碼”,database=“數(shù)據(jù)庫(kù)名”,charset=“utf8”) # 得到一個(gè)可以執(zhí)行SQL語(yǔ)句的光標(biāo)對(duì)象 cursor = conn.cursor() # 修改數(shù)據(jù)的SQL語(yǔ)句 sql = "UPDATE USER1 SET age=%s WHERE name=%s;" username = "Alex" age = 80 try:# 執(zhí)行SQL語(yǔ)句 cursor.execute(sql, [age, username])# 提交事務(wù) conn.commit() except Exception as e:# 有異常,回滾事務(wù) conn.rollback() cursor.close() conn.close()查
查詢單條數(shù)據(jù)
# 導(dǎo)入pymysql模塊 import pymysql # 連接database conn = pymysql.connect(host=“你的數(shù)據(jù)庫(kù)地址”, user=“用戶名”,password=“密碼”,database=“數(shù)據(jù)庫(kù)名”,charset=“utf8”) # 得到一個(gè)可以執(zhí)行SQL語(yǔ)句的光標(biāo)對(duì)象 cursor = conn.cursor() # 查詢數(shù)據(jù)的SQL語(yǔ)句 sql = "SELECT id,name,age from USER1 WHERE id=1;" # 執(zhí)行SQL語(yǔ)句 cursor.execute(sql) # 獲取單條查詢數(shù)據(jù) ret = cursor.fetchone() cursor.close() conn.close() # 打印下查詢結(jié)果 print(ret)查詢多條數(shù)據(jù)
# 導(dǎo)入pymysql模塊 import pymysql # 連接database conn = pymysql.connect(host=“你的數(shù)據(jù)庫(kù)地址”, user=“用戶名”,password=“密碼”,database=“數(shù)據(jù)庫(kù)名”,charset=“utf8”) # 得到一個(gè)可以執(zhí)行SQL語(yǔ)句的光標(biāo)對(duì)象 cursor = conn.cursor() # 查詢數(shù)據(jù)的SQL語(yǔ)句 sql = "SELECT id,name,age from USER1;" # 執(zhí)行SQL語(yǔ)句 cursor.execute(sql) # 獲取多條查詢數(shù)據(jù) ret = cursor.fetchall() cursor.close() conn.close() # 打印下查詢結(jié)果 print(ret)進(jìn)階用法
# 可以獲取指定數(shù)量的數(shù)據(jù) cursor.fetchmany(3) # 光標(biāo)按絕對(duì)位置移動(dòng)1 cursor.scroll(1, mode="absolute") # 光標(biāo)按照相對(duì)位置(當(dāng)前位置)向后移動(dòng)1 cursor.scroll(1, mode="relative") # 光標(biāo)按照相對(duì)位置(當(dāng)前位置)向前移動(dòng)1 cursor.scroll(-1, mode="relative")轉(zhuǎn)載于:https://www.cnblogs.com/L5251/articles/8617440.html
總結(jié)
以上是生活随笔為你收集整理的Python基础44(PyMySQL模块)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【BZOJ1048】分割矩阵(记忆化搜索
- 下一篇: 什么是垃圾