欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python中pymysql 模塊的使用詳解

 更新時間:2019年08月12日 08:58:53   作者:與鹿逐秋  
pymsql是Python中操作MySQL的模塊,其使用方法和MySQLdb幾乎相同。但目前pymysql支持python3.x而后者不支持3.x版本。

pymysql 模塊的使用

一、pymysql的下載和使用

(1)pymysql模塊的下載

pip3 install pymysql

(2)pymysql的使用

# 實現(xiàn):使用Python實現(xiàn)用戶登錄,如果用戶存在則登錄成功(假設(shè)該用戶已在數(shù)據(jù)庫中)
import pymysql
user = input('請輸入用戶名:')
pwd = input('請輸入密碼:')

# 1.連接
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='', db='db8', charset='utf8')

# 2.創(chuàng)建游標(biāo)
cursor = conn.cursor()

#注意%s需要加引號
sql = "select * from userinfo where username='%s' and pwd='%s'" %(user, pwd)
print(sql)

# 3.執(zhí)行sql語句
cursor.execute(sql)

result=cursor.execute(sql) #執(zhí)行sql語句,返回sql查詢成功的記錄數(shù)目
print(result)

# 關(guān)閉連接,游標(biāo)和連接都要關(guān)閉
cursor.close()
conn.close()

if result:
  print('登陸成功')
else:
  print('登錄失敗')

二、execute()之sql注入

最后那一個空格,在一條sql語句中如果遇到select * from userinfo where username='mjj' -- asadasdas' and pwd='' 則--之后的條件被注釋掉了(注意--后面還有一個空格)

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

mjj' -- 任意字符

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

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

解決方法:

# 原來是我們對sql進(jìn)行字符串拼接
# sql="select * from userinfo where name='%s' and password='%s'" %(username,pwd)
# print(sql)
# result=cursor.execute(sql)

#改寫為(execute幫我們做字符串拼接,我們無需且一定不能再為%s加引號了)
sql="select * from userinfo where name=%s and password=%s" #?。?!注意%s需要去掉引號,因為pymysql會自動為我們加上
result=cursor.execute(sql,[user,pwd]) #pymysql模塊自動幫我們解決sql注入的問題,只要我們按照pymysql的規(guī)矩來。

三、增、刪、改:conn.commit()

commit()方法:在數(shù)據(jù)庫里增、刪、改的時候,必須要進(jìn)行提交,否則插入的數(shù)據(jù)不生效。

import pymysql
username = input('請輸入用戶名:')
pwd = input('請輸入密碼:')

# 1.連接
conn = pymysql.connect(host='localhost', port=3306, user='root', password='', db='db8', charset='utf8')

# 2.創(chuàng)建游標(biāo)
cursor = conn.cursor()

# 操作
# 增
# sql = "insert into userinfo(username,pwd) values (%s,%s)"

# effect_row = cursor.execute(sql,(username,pwd))
#同時插入多條數(shù)據(jù)
#cursor.executemany(sql,[('李四','110'),('王五','119')]) 

# print(effect_row)#

# 改
# sql = "update userinfo set username = %s where id = 2"
# effect_row = cursor.execute(sql,username)
# print(effect_row)

# 刪
sql = "delete from userinfo where id = 2"
effect_row = cursor.execute(sql)
print(effect_row)

#一定記得commit
conn.commit()

# 4.關(guān)閉游標(biāo)
cursor.close()

# 5.關(guān)閉連接
conn.close()

四、查:fetchone、fetchmany、fetchall

fetchone():獲取下一行數(shù)據(jù),第一次為首行;

fetchall():獲取所有行數(shù)據(jù)源

fetchmany(4):獲取4行數(shù)據(jù)

查看一下表內(nèi)容:

mysql> select * from userinfo;
+----+----------+-----+
| id | username | pwd |
+----+----------+-----+
| 1 | mjj   | 123 |
| 3 | 張三   | 110 |
| 4 | 李四   | 119 |
+----+----------+-----+
3 rows in set (0.00 sec)

使用fetchone():

import pymysql

# 1.連接
conn = pymysql.connect(host='localhost', port=3306, user='root', password='', db='db8', charset='utf8')

# 2.創(chuàng)建游標(biāo)
cursor = conn.cursor()

sql = 'select * from userinfo'
cursor.execute(sql)

# 查詢第一行的數(shù)據(jù)
row = cursor.fetchone()
print(row) # (1, 'mjj', '123')

# 查詢第二行數(shù)據(jù)
row = cursor.fetchone()
print(row) # (3, '張三', '110')

# 4.關(guān)閉游標(biāo)
cursor.close()

# 5.關(guān)閉連接
conn.close()

使用fetchall():

import pymysql

# 1.連接
conn = pymysql.connect(host='localhost', port=3306, user='root', password='', db='db8', charset='utf8')

# 2.創(chuàng)建游標(biāo)
cursor = conn.cursor()

sql = 'select * from userinfo'
cursor.execute(sql)

# 獲取所有的數(shù)據(jù)
rows = cursor.fetchall()
print(rows)

# 4.關(guān)閉游標(biāo)
cursor.close()

# 5.關(guān)閉連接
conn.close()

#運行結(jié)果
((1, 'mjj', '123'), (3, '張三', '110'), (4, '李四', '119'))

默認(rèn)情況下,我們獲取到的返回值是元組,只能看到每行的數(shù)據(jù),卻不知道每一列代表的是什么,這個時候可以使用以下方式來返回字典,每一行的數(shù)據(jù)都會生成一個字典:

cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)  #在實例化的時候,將屬性cursor設(shè)置為pymysql.cursors.DictCursor

在fetchone示例中,在獲取行數(shù)據(jù)的時候,可以理解開始的時候,有一個行指針指著第一行的上方,獲取一行,它就向下移動一行,所以當(dāng)行指針到最后一行的時候,就不能再獲取到行的內(nèi)容,所以我們可以使用如下方法來移動行指針:

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

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

第一個值為移動的行數(shù),整數(shù)為向下移動,負(fù)數(shù)為向上移動,mode指定了是相對當(dāng)前位置移動,還是相對于首行移動

# 1.Python實現(xiàn)用戶登錄
# 2.Mysql保存數(shù)據(jù)

import pymysql

# 1.連接
conn = pymysql.connect(host='localhost', port=3306, user='root', password='', db='db8', charset='utf8')

# 2.創(chuàng)建游標(biāo)
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)

sql = 'select * from userinfo'
cursor.execute(sql)

# 查詢第一行的數(shù)據(jù)
row = cursor.fetchone()
print(row) # (1, 'mjj', '123')

# 查詢第二行數(shù)據(jù)
row = cursor.fetchone() # (3, '張三', '110')
print(row)

cursor.scroll(-1,mode='relative') #設(shè)置之后,光標(biāo)相對于當(dāng)前位置往前移動了一行,所以打印的結(jié)果為第二行的數(shù)據(jù)
row = cursor.fetchone() 
print(row)

cursor.scroll(0,mode='absolute') #設(shè)置之后,光標(biāo)相對于首行沒有任何變化,所以打印的結(jié)果為第一行數(shù)據(jù)
row = cursor.fetchone() 
print(row)

# 4.關(guān)閉游標(biāo)
cursor.close()

# 5.關(guān)閉連接
conn.close()

#結(jié)果如下

{'id': 1, 'username': 'mjj', 'pwd': '123'}
{'id': 3, 'username': '張三', 'pwd': '110'}
{'id': 3, 'username': '張三', 'pwd': '110'}
{'id': 1, 'username': 'mjj', 'pwd': '123'}

fetchmany():

import pymysql

# 1.連接
conn = pymysql.connect(host='localhost', port=3306, user='root', password='', db='db8', charset='utf8')


# 2.創(chuàng)建游標(biāo)
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)

sql = 'select * from userinfo'
cursor.execute(sql)

# 獲取2條數(shù)據(jù)
rows = cursor.fetchmany(2)
print(rows)

# 4.關(guān)閉游標(biāo)

# rows = cursor.fetchall()
# print(rows)
cursor.close()

# 5.關(guān)閉連接
conn.close()

#結(jié)果如下:
[{'id': 1, 'username': 'mjj', 'pwd': '123'}, {'id': 3, 'username': '張三', 'pwd': '110'}]

相關(guān)文章

最新評論