1、基本规则及其input命令

import pymysql

conn=pymysql.connect(host='127.0.0.1',port=3306,user='root',password='123',database='db4',charset='utf8mb4')
#游标
cursor=conn.cursor() #执行完毕返回的结果集默认以元组显示

# cursor.execute("insert into user(name,pwd) values('egon','123'),('tom','456'),('jack','111');")

# sql="insert into user(name,pwd) values('%s','%s');" %('lili','123')
# cursor.execute(sql)

# %s不要加引号
# cursor.execute("insert into user(name,pwd) values(%s,%s);",('kkk','123'))


username = input("username>>>: ").strip()
password = input("password>>>: ").strip()
# sql = "select * from user where name='%s' and pwd='%s'" %(username,password)
# select * from user where name='egon' -- hello' and pwd='%s'
# select * from user where name='xxx' or 1=1 -- hello' and pwd='%s';
# rows=cursor.execute(sql)

rows=cursor.execute("select * from user where name=%s and pwd=%s",(username,password))

if rows:
    print('ok')
else:
    print('no')

conn.commit()
cursor.close()
conn.close()

2、用法

import pymysql  # pip3 install pymysql

conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", password="123", db="db13", charset="utf8mb4")
#游标 cursor
= conn.cursor(cursor=pymysql.cursors.DictCursor)#返回字典 # 记录=》增删改 # cursor.execute("insert into student values(%s,%s)",[5,'kkk']) # cursor.executemany("insert into student values(%s,%s)",[(6,'xxx'),(7,'yyy'),(8,'zzzz')]) cursor.execute("insert into student(name) values(%s)",['zzz']) print(cursor.lastrowid) # cursor.execute("delete from student where id > 6;") # 记录=》查 # rows = cursor.execute('select * from student;') # print(rows) # line = cursor.fetchone() # print(line) # line = cursor.fetchone() # print(line) # # lines = cursor.fetchmany(2) # print(lines) # lines = cursor.fetchall() # print(lines) # for line in cursor: # print(line) # cursor.scroll(3,mode='absolute') # 相对绝对位置移动 # print(cursor.fetchone()) # print(cursor.fetchall()) # cursor.scroll(0,mode='absolute') # 相对绝对位置移动 # print(cursor.fetchall()) # cursor.fetchone() # cursor.scroll(1,mode='relative') # 相对当前位置移动 # cursor.scroll(0,mode='absolute') # 相对当前位置移动 # print(cursor.fetchone()) conn.commit() cursor.close() conn.close()

3、在pymysql里调用存储过程

import pymysql  # pip3 install pymysql

conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", password="123", db="db4", charset="utf8mb4")
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)


cursor.callproc('p2',(3,0))  # @_p2_0=3,@_p2_1=0
'''
set @_p2_0=3
set @_p2_1=0

call p2(@_p2_0,@_p2_1);

'''

print(cursor.fetchall())

cursor.execute("select @_p2_1;")
print(cursor.fetchall())

cursor.execute("select @_p2_0;")
print(cursor.fetchall())

cursor.close()
conn.close()

 

----46---