07.21自我總結 pymysql模塊 一.創建連接庫 二.建立游標 三.提交sql語句 四.查看內容 五.移動游標 相對位置 cursor.scroll(1, "relative") cursor.scroll() 預設是相對位置 絕對位置 cursor.scroll(0, "absolute") ...
07.21自我總結
pymysql模塊
一.創建連接庫
conn = pymysql.connect(host="127.0.0.1",#預設是本機
port=3306, #預設3306
user="root",#必填
password='密碼',#必填
db="庫名")#必填
#如果沒有庫會報pymysql.err.InternalError: (1049, "Unknown database '庫名'")
所有我們編輯可以這樣
try:
conn = pymysql.connect(host="127.0.0.1",#預設是本機
port=3306, #預設3306
user="root",#必填
password='16745',#必填
db="asds",)#必填
except pymysql.err.InternalError:
print('沒有庫')
二.建立游標
cursor = conn.cursor(pymysql.cursors.DictCursor) #自定義游標類型為字典
cursor = conn.cursor()#預設是元祖
三.提交sql語句
普通提交
count = cursor.execute('show tables') #返回值為受到影響的數據條數
防註入提交
table_name = input('table name :')
count = cursor.execute('select table %s',(name,))
四.查看內容
預設顯示之前那一次顯示的內容,只顯示查看相關語法的內容,為返回值
cursor.fetchall() #查看全部
cursor.fetchone() #查看當前游標位置的一個值
cursor.fetchmay(N) #查看當前游標位置的n值
五.移動游標
相對位置
cursor.scroll(1, "relative")
cursor.scroll() #預設是相對位置
絕對位置
cursor.scroll(0, "absolute")