pymysql html
# 1.先安裝pymysql 模塊 pip3 install pymysql import pymysql # 至關於mysql的客戶端程序 # 前端中獲取的用戶名和密碼 username = input('請輸入用戶名:') pwd = input('請輸入密碼:') # 創建鏈接 conn = pymysql.connect( host = '127.0.0.1', port = 3306, db = 'db20', user = 'root', password='', charset ='utf8' ) # 建立遊標 cur = conn.cursor() # select * from userinfo where name = 'alex' and password = '123'; sql = "select * from userinfo where name = '%s' and password = '%s'" %(username,pwd) print(sql) # 執行sql 返回是查詢的成功的記錄 result = cur.execute(sql) 使用這個能夠防止sql注入 print(result) # 遊標關閉 鏈接關閉 cur.close() conn.close() if result: # 響應 數據 到前端 print('登陸成功') else: print('登陸失敗')
sql = "select * from userinfo where name = %(name)s and password = %(password)s" print(sql) # sql -- # 執行sql 返回是查詢的成功的記錄 result = cur.execute(sql,{"name":username,"password":pwd}) print(result)# 字典
# 1.先安裝pymysql 模塊 pip3 install pymysql import pymysql # 至關於mysql的客戶端程序 # 創建鏈接 conn = pymysql.connect( host = '127.0.0.1', port = 3306, db = 'db20', user = 'root', password='', charset ='utf8' ) # 建立遊標 cur = conn.cursor() # 插入 # select * from userinfo where name = 'alex' and password = '123'; sql = "insert into userinfo(name,password) values (%s,%s)" # 更改 # sql = "update userinfo set name = %s where id = 3" # 更改 # sql = "delete from userinfo where id = 3" # print(sql) # sql -- # 執行sql 插入數據 刪除數據 更改數據 必定記得commit() # 插入一條數據 # result = cur.execute(sql) # 插入多條數據 result = cur.executemany(sql,[('alex2','321'),('alex3','678')]) print(result) # 必定要提交 conn.commit() # 遊標關閉 鏈接關閉 cur.close() conn.close()
查詢前端
:mysql
建立遊標 查詢出來的記錄 是字典的形式 cur = conn.cursor(cursor=pymysql.cursors.DictCursor) # select * from userinfo where name = 'alex' and password = '123'; sql = "select * from userinfo" print(sql) # sql -- # 執行sql 返回是查詢的成功的記錄 result = cur.execute(sql) print(result) # rows = cur.fetchone() # print(rows) # rows = cur.fetchone() # print(rows) # rows = cur.fetchone() # print(rows) # rows = cur.fetchone() # print(rows) # 查詢多條數據 # rows = cur.fetchmany(2) # 查詢全部的數據 rows = cur.fetchall() print(rows)
elect * from userinfo where name = 'alex' and password = '123'; sql = "select * from userinfo" print(sql) # sql -- # 執行sql 返回是查詢的成功的記錄 result = cur.execute(sql) print(result) rows = cur.fetchone() print(rows) rows = cur.fetchone() print(rows) cur.scroll(1,mode='absolute') rows = cur.fetchone() print(rows) # rows = cur.fetchone() # print(rows) # rows = cur.fetchone() # print(rows) # rows = cur.fetchone() # print(rows)
只有一頁 和下一頁sql
1,記錄當前頁的最大id或者最小的idfetch
下一頁:htm
select * from user where id > max_id limit 10,blog
select 8 from user where id< min order by id desc limit 10;ip
2,頁面有頁碼的狀況input
select * from user where id in (it
select id from (select *from user where id > pre_max_id limit (cu)max_id-pre_max_id)*10 ) as A order by A.id desc limit 10);
select * from (select * from user where id > 1500011 limit 30) as A order by id order by desc;