pyMysql模块

# pip3 install pymysql
import pymysql

client=pymysql.connect(
    host='127.0.0.1',
    port=3306,
    user='root',
    password='',
    database='db5',
    charset='utf8'
)

cursor=client.cursor()

sql='insert into t1 values(1,"egon");  '
try:
    res=cursor.execute(sql)
    # res=cursor.execute(sql)
    # res=cursor.execute(sql)
    # res=cursor.execute(sql)
    print(res)

    client.commit()
except Exception:
    client.rollback()

cursor.close()
client.close()



# 基于pymysql模块的增删改

# pip3 install pymysql
import pymysql

client=pymysql.connect(
    host='127.0.0.1',
    port=3306,
    user='root',
    password='',
    database='db5',
    charset='utf8'
)

cursor=client.cursor()

#
# userinfo=[
#     (3,"alex"),
#     (4,"lxx"),
#     (5,"xxx")
# ]
#
# for user in userinfo:
#     sql='insert into t1 values(%s,"%s");  '%(user[0],user[1])
#     print(sql)
#     cursor.execute(sql)


# 增简便方法
# userinfo=[
#     (3,"alex"),
#     (4,"lxx"),
#     (5,"xxx")
# ]
# sql='insert into t1 values(%s,%s);'
# cursor.executemany(sql,userinfo)


#
# cursor.execute('delete from t1 where id =3;')


#
# sql='alter table t1 add age int; '
sql='alter table t1 modify name varchar(20); '

cursor.execute(sql)

client.commit()

cursor.close()
client.close()






# 基于mysql模块的查询

import pymysql

client=pymysql.connect(
    host='127.0.0.1',
    port=3306,
    user='root',
    password='',
    database='db5',
    charset='utf8'
)

cursor=client.cursor()


inp_user=input('输入账号:').strip()
inp_pwd=input('输入密码:').strip()

# sql='select id from user where name="%s" and pwd=password("%s");'%(inp_user,inp_pwd)
sql='select id from user where name="%s" and pwd="%s";'%(inp_user,inp_pwd)
print(sql)
rows=cursor.execute(sql)        #rows行数 如果有证明存在
if rows:
    print('\033[45m登录成功\033[0m')
else:
    print('\033[46m用户或密码错误\033[0m')


cursor.execute(sql)

client.commit()

cursor.close()
client.close()


# 输入:egon" -- xxxxx
# xxx" or 1=1 -- hellosb  可以登录




# 解决sql 注入问题

import pymysql

client=pymysql.connect(
    host='127.0.0.1',
    port=3306,
    user='root',
    password='',
    database='db5',
    charset='utf8'
)

cursor=client.cursor()
#查询
inp_user=input('输入账号名: ').strip()
inp_pwd=input('输入密码: ').strip()

sql='select id from user where name = %s and pwd = %s;'
rows=cursor.execute(sql,(inp_user,inp_pwd))              #不进行字符串拼接,放在execute执行
if rows:
    print('\033[45m登陆成功\033[0m')
else:
    print('\033[46m用户名或密码错误\033[0m')

cursor.close()
client.close()




# 提交查询语句并且拿到查询结果
import pymysql

client=pymysql.connect(
    host='127.0.0.1',
    port=3306,
    user='root',
    password='egon123',
    database='db6',
    charset='utf8'
)

cursor=client.cursor(pymysql.cursors.DictCursor)
#查询

sql='select * from user where id > 3'
rows=cursor.execute(sql)
# print(rows)
# print(cursor.fetchall())
# print(cursor.fetchall())

# print(cursor.fetchone())
# print(cursor.fetchone())
# print(cursor.fetchone())

# print(cursor.fetchmany(2))
# print(cursor.fetchone())



# print(cursor.fetchall())
# # cursor.scroll(0,mode='absolute') # 绝对位置移动
# # cursor.scroll(1,mode='absolute') # 绝对位置移动
# print(cursor.fetchall())


# print(cursor.fetchone())
# cursor.scroll(2,mode='relative') # 相对当前位置移动
# print(cursor.fetchone())

cursor.close()
client.close()





# 在Python3中调用存储过程:
import pymysql

client=pymysql.connect(
    host='127.0.0.1',
    port=3306,
    user='root',
    password='',
    database='db5',
    charset='utf8'
)

cursor=client.cursor(pymysql.cursors.DictCursor)
res=cursor.callproc('p4',(3,111)) # set @_p4_0 = 3 ;set @_p4_1 = 111

print(cursor.fetchall())

cursor.execute('select @_p4_1;')
print(cursor.fetchone())

cursor.close()
client.close()

 

posted @ 2018-08-11 16:21  Shinonon  阅读(128)  评论(0编辑  收藏  举报