最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

pymysql模塊的使用(增刪改查)詳解

 更新時(shí)間:2019年09月09日 09:14:32   作者:信奉上帝的小和尚  
這篇文章主要介紹了pymysql模塊的使用(增刪改查)詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

一、pymysql的下載和使用

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

(1)pymysql模塊的下載

pip3 install pymysql

(2)pymysql的使用

# 實(shí)現(xiàn):使用Python實(shí)現(xiàn)用戶登錄,如果用戶存在則登錄成功(假設(shè)該用戶已在數(shù)據(jù)庫(kù)中)


import pymysql
user = input('請(qǐng)輸入用戶名:')
pwd = input('請(qǐng)輸入密碼:')
# 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需要加引號(hào)
sql = "select * from userinfo where username='%s' and pwd='%s'" %(user, pwd)
print(sql)
# 3.執(zhí)行sql語(yǔ)句
cursor.execute(sql)
result=cursor.execute(sql) #執(zhí)行sql語(yǔ)句,返回sql查詢成功的記錄數(shù)目
print(result)
# 關(guān)閉連接,游標(biāo)和連接都要關(guān)閉
cursor.close()
conn.close()
if result:
  print('登陸成功')
else:
  print('登錄失敗')

二、execute()之sql注入

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

#1、sql注入之:用戶存在,繞過(guò)密碼
mjj' -- 任意字符
#2、sql注入之:用戶不存在,繞過(guò)用戶與密碼
xxx' or 1=1 -- 任意字符

解決方法:

# 原來(lái)是我們對(duì)sql進(jìn)行字符串拼接
# sql="select * from userinfo where name='%s' and password='%s'" %(username,pwd)
# print(sql)
# result=cursor.execute(sql)
#改寫(xiě)為(execute幫我們做字符串拼接,我們無(wú)需且一定不能再為%s加引號(hào)了)
sql="select * from userinfo where name=%s and password=%s" #?。。∽⒁?s需要去掉引號(hào),因?yàn)閜ymysql會(huì)自動(dòng)為我們加上
result=cursor.execute(sql,[user,pwd]) #pymysql模塊自動(dòng)幫我們解決sql注入的問(wèn)題,只要我們按照pymysql的規(guī)矩來(lái)。

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

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

import pymysql
username = input('請(qǐng)輸入用戶名:')
pwd = input('請(qǐng)輸入密碼:')
# 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í)插入多條數(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 |
+----+----------+-----+
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()

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

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

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

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

cursor.scroll(1,mode='relative') # 相對(duì)當(dāng)前位置移動(dòng)
cursor.scroll(2,mode='absolute') # 相對(duì)絕對(duì)位置移動(dòng)

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

# 1.Python實(shí)現(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)相對(duì)于當(dāng)前位置往前移動(dòng)了一行,所以打印的結(jié)果為第二行的數(shù)據(jù)
row = cursor.fetchone() 
print(row)

cursor.scroll(0,mode='absolute') #設(shè)置之后,光標(biāo)相對(duì)于首行沒(méi)有任何變化,所以打印的結(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'}]

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

抚松县| 广元市| 贵南县| 天祝| 德州市| 沾益县| 阿勒泰市| 九龙城区| 安溪县| 茌平县| 安多县| 建水县| 五大连池市| 长寿区| 鄱阳县| 金沙县| 宁城县| 灌云县| 茶陵县| 邵东县| 合江县| 水城县| 修水县| 南汇区| 高邮市| 正镶白旗| 博乐市| 罗平县| 滦南县| 黄浦区| 通山县| 茶陵县| 道孚县| 个旧市| 名山县| 临夏市| 谷城县| 安康市| 乐亭县| 夏邑县| 大理市|