摘要:一安裝模塊二導(dǎo)入需要的模塊三構(gòu)建數(shù)據(jù)庫屬性四寫語句五使用連接數(shù)據(jù)庫六執(zhí)行語句插入數(shù)據(jù)刪除刪除數(shù)據(jù)時如果沒有提交數(shù)據(jù)庫中數(shù)據(jù)不變但是查詢到的東西沒有要刪除的那條數(shù)據(jù)修改數(shù)據(jù)庫如果修改沒有提交在代碼查看到的數(shù)據(jù)已經(jīng)修改單數(shù)數(shù)據(jù)庫中的數(shù)據(jù)沒有修改
一.安裝pyMySQL模塊
pip install pymysql
二,導(dǎo)入需要的模塊
import pymysql
三.構(gòu)建數(shù)據(jù)庫屬性
host = "localhost" username = "root" password = "root" db_name = "test"
四.寫sql語句
insert_table_sql = """insert into user values (null ,"lw","555222000")""" find_table_sql = """select * from user""" delete_table_sql = """delete from user where user_id={user_id}"""
五.使用pymysql連接數(shù)據(jù)庫
conn = pymysql.connect(host=host, user=username, password=password, db=db_name)
六.執(zhí)行SQL語句
try: with conn.cursor() as cursor: # 插入數(shù)據(jù) # cursor.execute(insert_table_sql.format(username="ll", password="123")) # conn.commit() # 刪除 # cursor.execute(delete_table_sql.format(user_id="3")) # pymysql刪除數(shù)據(jù)時,如果,沒有提交,數(shù)據(jù)庫中數(shù)據(jù)不變,但是查詢到的東西沒有 # 要刪除的那條數(shù)據(jù) # conn.commit() # 修改數(shù)據(jù)庫 cursor.execute(update_table_sql.format(user_id=5)) # 如果修改沒有提交,在代碼查看到的數(shù)據(jù)已經(jīng)修改,單數(shù)數(shù)據(jù)庫中的數(shù)據(jù)沒有修改 conn.commit() # 查詢?nèi)繑?shù)據(jù) cursor.execute(find_table_sql) result = cursor.fetchall() print(result) finally: conn.close()
七.防sql注入
修改插入數(shù)據(jù)sql語句為:
insert_table_sql = """insert into user(user_id,user_name,password) values (%S ,%S,%S)"""
執(zhí)行代碼修改為
cursor.execute(insert_table_sql, (1, "ll", "123")) conn.commit()
運(yùn)行后報錯
Traceback (most recent call last): File "D:/creator/pythonProject/0002.py", line 55, insave_code() File "D:/creator/pythonProject/0002.py", line 33, in save_code cursor.execute(insert_table_sql, (1, "ll", "123")) File "D:creatorpythonProjectvenvlibsite-packagespymysqlcursors.py", line 168, in execute query = self.mogrify(query, args) File "D:creatorpythonProjectvenvlibsite-packagespymysqlcursors.py", line 147, in mogrify query = query % self._escape_args(args, conn) ValueError: unsupported format character "S" (0x53) at index 54
錯誤原因:字符占位符寫錯的應(yīng)該是%s 而不是$S s應(yīng)該小寫
修改語句為
insert_table_sql = """insert into user(user_id,user_name,password) values (%s ,%s,%s)"""
運(yùn)行正常
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/42941.html
摘要:模塊什么是是在版本中用于連接服務(wù)器的一個庫,中則使用。遵循數(shù)據(jù)庫規(guī)范,并包含了客戶端庫。 【Python3】pymysql模塊 1. 什么是 PyMySQL? PyMySQL 是在 Python3.x 版本中用于連接 MySQL 服務(wù)器的一個庫,Python2中則使用mysqldb。 PyMySQL 遵循 Python 數(shù)據(jù)庫 API v2.0 規(guī)范,并包含了 pure-Python ...
摘要:簡述是中操作的模塊,其使用方法和幾乎相同。但目前支持而后者不支持版本。因此要避免這種情況需使用提供的參數(shù)化查詢。使用存儲過程動態(tài)執(zhí)行防注入使用存儲過程自動提供防注入,動態(tài)傳入到存儲過程執(zhí)行語句。 簡述 pymsql是Python中操作MySQL的模塊,其使用方法和MySQLdb幾乎相同。但目前pymysql支持python3.x而后者不支持3.x版本。本文測試python版本:3.5....
摘要:一介紹是在版本中用于連接和操作服務(wù)器的一個庫引入方式二連接數(shù)據(jù)庫的完整流程引入模塊引入第三方庫創(chuàng)建連接對象用戶名密碼端口號默認(rèn)為且此處為整數(shù)類型數(shù)據(jù)庫名連接地址使用連接對象創(chuàng)建游標(biāo)對象游標(biāo)對象是通過鏈接對象進(jìn)行創(chuàng) ...
閱讀 1502·2021-11-24 11:16
閱讀 2713·2021-07-28 12:32
閱讀 2315·2019-08-30 11:22
閱讀 1455·2019-08-30 11:01
閱讀 611·2019-08-29 16:24
閱讀 3559·2019-08-29 12:52
閱讀 1635·2019-08-29 12:15
閱讀 1346·2019-08-29 11:18