pyMysql模块

Posted 嗯姆嗯姆嗯姆

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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(33[45m登录成功33[0m)
else:
    print(33[46m用户或密码错误33[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(33[45m登陆成功33[0m)
else:
    print(33[46m用户名或密码错误33[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()

 

以上是关于pyMysql模块的主要内容,如果未能解决你的问题,请参考以下文章

MySQL5.7-pymysql模块和SQL注入

数据库可视化和pymysql模块

pymysql 模块简单使用

pymysql模块初识

pymysql模块的简单操作

Python pymysql模块