pymysql 連線的基本使用,注入問題。

2022-07-31 07:12:08 字數 4152 閱讀 4085

目錄注入問題:

資料的增、刪、改、查。

import pymysql

conn = pymysql.connect(

user = 'root',

password = '1999',

host = '127.0.0.1',

port = 3306,

charset = 'utf8',

database = 'dep2' #庫名

)cursor = conn.cursor(cursor=pymysql.cursors.dictcursor) # 產生乙個游標物件

# cursor=pymysql.cursors.dictcursor 將查詢出來的結果製作成字典的形式返回

sql = "select * from dep2" # dep2 ---> 表名

res = cursor.execute(sql) # 執行sql語句

print(res) #execute返回的時候當前sql所影響的行數

# 結果: dep2因為表中就只有兩行資料 。execute只列印他的行式

2

ret = cursor.fetchone()  # 只獲取查詢結果中的一條資料

print(ret)

#結果:

ret = cursor.fetchall()  # 獲取查詢結果的所有資料

print(ret)

#結果:

[, ]

ret = cursor.fetchmany(4)  # 指定獲取幾條資料  如果數字超了也不會報錯

print(ret)

#結果:

[, ]

cursor = conn.cursor(cursor=pymysql.cursors.dictcursor)  # 產生乙個游標物件

# cursor=pymysql.cursors.dictcursor 將查詢出來的結果製作成字典的形式返回

res = cursor.execute(sql) # 執行sql語句

# execute返回的時候當前sql所影響的行數

# ret = cursor.fetchone() # fetchone:只獲取查詢結果中的一條資料

# ret = cursor.fetchall() # fetchall:獲取查詢結果的所有資料

# ret = cursor.fetchmany(4) # fetchmany指定獲取幾條資料 如果數字超了也不會報錯

# fetchone:只獲取查詢結果中的一條資料,如果超出範圍則返回none

print(cursor.fetchone()) #

print(cursor.fetchone()) #

print(cursor.fetchone()) #:none

conn.commit()  #執行時必須先提交conn.commit()

cursor.scroll(1,'relative') # 基於指標所在的位置 往後偏移

# print(cursor.scroll())

conn.commit()  #執行時必須先提交conn.commit()

cursor.scroll(1,'absolute') # 基於起始位置 往後偏移 就相當於間隔的意思。

print(cursor.fetchall())

# 結果:因為表中只有兩行資料 我傳入的數字是1 間隔1行列印 。

sql注入問題

利用特殊符號和注釋語法 巧妙的繞過真正的sql校驗

關鍵性的資料 不要自己手動去拼接 而是交由execute幫你去做拼接

import pymysql

conn = pymysql.connect(

user = 'root',

passwd = '123456',

db = 'day36',

host = '127.0.0.1',

port = 3306,

charset = 'utf8'

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

# 獲取使用者輸入的使用者名稱和密碼 然後取資料庫中校驗

username = input('username>>>:').strip()

password = input('password>>>:').strip()

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

sql = "select * from userinfo where name=%s and password= %s"

print(sql)

cursor.execute(sql,(username,password))

res = cursor.fetchall()

if res:

print(res)

else:

print('username or password error!')

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

print(sql) #拼接的方法,不需要密碼也可以登入

sql = "select * from userinfo where name=%s and password= %s"

print(sql) #

import pymysql

conn = pymysql.connect(

user = 'root',

passwd = '1999',

db = 'dep2',

host = '127.0.0.1',

port = 3306,

charset = 'utf8',

autocommit = true # 自動提交確認

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

# # 獲取使用者輸入的使用者名稱和密碼 然後取資料庫中校驗

# username = input('username>>>:').strip()

# password = input('password>>>:').strip()

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

# sql = "select * from userinfo where name=%s and password= %s"

# print(sql)

# 刪除

# sql = "delete from userinfo where id= 1"

res = cursor.execute(sql)

# conn.commit() # 確認當前操作 真正的同步到資料庫

print(res)

"""針對增 刪 改操作 執行重要程度偏高

你如果真想操作 必須有一步確認操作(commit)

"""

sql = "insert into dep2(name,password,dep_id) values('jason',789,1)"
sql = "update userinfo set name='egondsb' where id = 6"
# sql = "delete from userinfo where id= 1"

res = cursor.execute(sql)

# conn.commit() # 確認當前操作 真正的同步到資料庫

pymysql 連線的基本使用,注入問題。

目錄 注入問題 資料的增 刪 改 查。import pymysql conn pymysql.connect user root password 1999 host 127.0.0.1 port 3306,charset utf8 database dep2 庫名 cursor conn.curs...

pymysql的基本使用

在學爬蟲的過程中,都要和資料庫打交道,我們常用的資料庫是mysql資料庫 或者redis 一對一 mongodb資料庫等 python3為我們提供了簡單的運算元據庫的方法,如下 我們安裝pymysql就基本上就是在命令列模式下,pip3 install pymysql 能翻牆的人用這個安裝吧 不能翻...

pymysql的基本使用

import pymysql 與c s架構中的client一樣使用connect與資料庫建立連線 conn pymysql.connect user root password 123456 host 127.0.0.1 port 3306,charset utf8 database day36 c...