pymsql

Posted duhong0520

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pymsql相关的知识,希望对你有一定的参考价值。


pymysql模块:
pip3 install pymysql -i https://pypi.douban.com/simple
Python模块:对数据库进行操作(SQL语句)

1. Python实现用户登录
2. MySQL保存数据


- 连接、关闭(游标)
- execute() -- SQL注入
- 增删改: conn.commit()
- fetchone fetchall
- 获取插入数据自增ID

# 增加,删,该
# conn = pymysql.connect(host="localhost",user=‘root‘,password=‘‘,database="db666")
# cursor = conn.cursor()
# sql = "insert into userinfo(username,password) values(‘root‘,‘123123‘)"
# 受影响的行数
# r = cursor.execute(sql)
# # ******
# conn.commit()
# cursor.close()
# conn.close()

# conn = pymysql.connect(host="localhost",user=‘root‘,password=‘‘,database="db666")
# cursor = conn.cursor()
# # sql = "insert into userinfo(username,password) values(%s,%s)"
# # cursor.execute(sql,(user,pwd,))
#
# sql = "insert into userinfo(username,password) values(%s,%s)"
# # 受影响的行数
# r = cursor.executemany(sql,[(‘egon‘,‘sb‘),(‘laoyao‘,‘BS‘)])
# # ******
# conn.commit()
# cursor.close()
# conn.close()

# 查
# conn = pymysql.connect(host="localhost",user=‘root‘,password=‘‘,database="db666")
# cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
# sql = "select * from userinfo"
# cursor.execute(sql)
# cursor.scroll(1,mode=‘relative‘) # 相对当前位置移动
# cursor.scroll(2,mode=‘absolute‘) # 相对绝对位置移动
# result = cursor.fetchone()
# print(result)
# result = cursor.fetchone()
# print(result)
# result = cursor.fetchone()
# print(result)
# result = cursor.fetchall()
# print(result)
# result = cursor.fetchmany(4)
# print(result)
# cursor.close()
# conn.close()

# 新插入数据的自增ID: cursor.lastrowid
# import pymysql
# conn = pymysql.connect(host="localhost",user=‘root‘,password=‘‘,database="db666")
# cursor = conn.cursor()
# sql = "insert into userinfo(username,password) values(‘asdfasdf‘,‘123123‘)"
# cursor.execute(sql)
# conn.commit()
# print(cursor.lastrowid)
# cursor.close()
# conn.close()

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