日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

python3 mysql模块_8.6.1 python3的mysql模块pymysql

發(fā)布時間:2023/12/16 数据库 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python3 mysql模块_8.6.1 python3的mysql模块pymysql 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

之前我們都是通過MySQL自帶的命令行客戶端工具mysql來操作數(shù)據(jù)庫,那如何在python程序中操作數(shù)據(jù)庫呢?這就用到了pymysql模塊,該模塊本質(zhì)就是一個套接字客戶端軟件,使用前需要事先安裝

二?鏈接、執(zhí)行sql、關(guān)閉(游標(biāo))

import pymysql

login_user=input("user>>").strip()

login_pwd= input("passwor>>").strip()

# 建立連接

conn=pymysql.connect(

host="127.0.0.1",

port=3306,

user="cmz",

passwd="cmz",

db="leco",

charset="utf8")

# 拿到游標(biāo)

cursor=conn.cursor()

sql= 'select * from userinfo where user ="%s" and pwd="%s"' %(login_user, login_pwd)

row=cursor.execute(sql)

cursor.close()

conn.close()

# 判斷ifrow:

print("登錄成功")else:

print("登錄失敗")

C:\Python35\python.exe D:/MySQL/mysql基本使用.py

user>>cmz

passwor>> 123登錄成功

View Code

三?execute()之sql注入

注意:符號--會注釋掉它之后的sql,正確的語法:--后至少有一個任意字符

根本原理:就根據(jù)程序的字符串拼接name='%s',我們輸入一個xxx' -- haha,用我們輸入的xxx加'在程序中拼接成一個判斷條件name='xxx' -- haha'

最后那一個空格,在一條sql語句中如果遇到select * from t1 where id > 3 -- and name='cmz';則--之后的條件被注釋掉了

#1、sql注入之:用戶存在,繞過密碼

cmz'-- 任意字符 # --在MySQL中表示注釋

#2、sql注入之:用戶不存在,繞過用戶與密碼

xxx'or 1=1 -- 任意字符

user>> xxx"or 1=1 -- jaa

passwor>>

select * from userinfo where user ="xxx" or 1=1 -- jaa"and pwd=""登錄成功

解決方法:

# 原來是我們對sql進(jìn)行字符串拼接

# sql="select * from userinfo where name='%s' and password='%s'" %(login_user,login_pwd)

# print(sql)

# res=cursor.execute(sql)

#改寫為(execute幫我們做字符串拼接,我們無需且一定不能再為%s加引號了)

sql="select * from userinfo where name=%s and password=%s" #!!!注意%s需要去掉引號,因?yàn)閜ymysql會自動為我們加上

res=cursor.execute(sql,[login_user,login_pwd]) #[login_user,login_pwd] 和 (login_user,login_pwd) 都有可以

#pymysql模塊自動幫我們解決sql注入的問題,只要我們按照pymysql的規(guī)矩來。

四?增、刪、改:conn.commit()

增刪改

import pymysql

# 建立連接

conn=pymysql.connect(

host="127.0.0.1",

port=3306,

user="cmz",

passwd="cmz",

db="leco",

charset="utf8")

# 拿到游標(biāo)

cursor=conn.cursor()

# 增刪改

sql= 'insert into userinfo(user,pwd) values(%s, %s)'# row= cursor.execute(sql,("xxx",'123'))

row= cursor.executemany(sql,[('yxx','123'),('cmz1','111'),('cmz2','2222')]) # #執(zhí)行sql語句,返回sql影響成功的行數(shù)

print(row)

conn.commit() #提交后才發(fā)現(xiàn)表中插入記錄成功

cursor.close()

conn.close()

五?查:fetchone,fetchmany,fetchall

# 查詢

import pymysql

# 建立連接

conn=pymysql.connect(

host="127.0.0.1",

port=3306,

user="cmz",

passwd="cmz",

db="leco",

charset="utf8")

# 拿到游標(biāo)

# cursor=conn.cursor(pymysql.cursors.DictCursor)

cursor=conn.cursor(pymysql.cursors.DictCursor)

# 查詢

row= cursor.execute('select * from userinfo;') #執(zhí)行sql語句,返回sql影響成功的行數(shù)rows,將結(jié)果放入一個集合,等待被查詢

print(row)

# print(cursor.fetchall()) # 一次取全部

# print(cursor.fetchmany(2)) # 一次取N(2)條

# print(cursor.fetchone()) # 一次取一條

# print(cursor.fetchone()) # 一次取一條

# print(cursor.fetchone()) # 一次取一條

#

# cursor.scroll(3,mode='absolute') # 相對絕對位置移動

# cursor.scroll(3,mode='relative') # 相對當(dāng)前位置移動

# cursor.scroll(3,mode='absolute') # 相對絕對位置移動

print(cursor.fetchone())

print(cursor.fetchone())

cursor.scroll(2,mode='relative') # 向后移動兩條數(shù)據(jù)

print(cursor.fetchone())

conn.commit()

cursor.close()

conn.close()

結(jié)果:

12{'user': 'cmz', 'id': 1, 'pwd': '123'}

{'user': 'leco', 'id': 2, 'pwd': '456'}

{'user': 'cmz1', 'id': 5, 'pwd': '111'}18

五?獲取插入的最后一條數(shù)據(jù)的自增ID

在插入之前查看數(shù)據(jù)庫

id在15

import pymysql

# 建立連接

conn=pymysql.connect(

host="127.0.0.1",

port=3306,

user="cmz",

passwd="cmz",

db="leco",

charset="utf8")

# 拿到游標(biāo)

cursor=conn.cursor()

# 插入

sql= 'insert into userinfo(user,pwd) values(%s, %s)'# row= cursor.execute(sql,("xxx",'123'))

row= cursor.executemany(sql,[('cc1','123'),('cc2','111'),('cc3','2222')]) # 一次性插入多條

print(cursor.lastrowid) #獲取插入的最后一條數(shù)據(jù)的自增ID

conn.commit()

cursor.close()

conn.close()

結(jié)果是

C:\Python35\python.exe D:MySQL/mysql模塊之增刪改查.py21

此時查看數(shù)據(jù)庫自增的ID

總結(jié)

以上是生活随笔為你收集整理的python3 mysql模块_8.6.1 python3的mysql模块pymysql的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。