1.pymysql的安装
安装步骤的原文老张的博客
2. python中实现对mysql的操作
2.1 python实现mysql的用户登陆,数据存储到mysql中
import pymysqluser = input(\"username:\")pwd = input(\"password:\")conn = pymysql.connect(host=\"localhost\", user=\'root\', password=\'\', database=\"db1\")cursor = conn.cursor()#连接数据库成功sql = \"select * from userinfo where username=\'%s\' and password=\'%s\' \" %(user, pwd)#此处有风险cursor.execute(sql)result = cursor.fetchone()cursor.close()#关闭连接conn.close()#关闭连接if result:print(\'登陆成功\')else:print(\'登陆失败\')#有SQL注入的风险!!!!!!!!!
**
import pymysqluser = input(\"username:\")#输入用户名pwd = input(\"password:\")#输入密码conn = pymysql.connect(host=\"localhost\", user=\'root\', password=\'\', database=\"db1\")#conn=在localhost的mysql用密码为 的root用户连接数据库db1cursor = conn.cursor()#连接数据库成功sql = \"select * from userinfo where username=%s and password=%s \"#sql = \"select * from userinfo where username=%(u)s and password=%(p)s \"cursor.execute(sql,user,pwd)#将输入的用户名和密码与上面的sql语句拼接到一起#cursor.execute(sql,[user,pwd])#可以是一个数组#cursor.execute(sql,{\'u\':user,\'p\':pwd})#可以是一个集合,使用上面第二条sql语句result = cursor.fetchone()#返回一个结果cursor.close()#关闭连接conn.close()#关闭连接if result:print(\'登陆成功\')else:print(\'登陆失败\')
2.2 python中对mysql数据库进行增加/删除/修改操作(修改其中的sql语句就可以)
import pymysql#user = \"eric\"#pwd = \'123123\'conn = pymysql.connect(host=\"localhost\", user=\'root\', password=\'\', database=\"db1\")#conn=在localhost的mysql用密码为 的root用户连接数据库db1cursor = conn.cursor()#连接数据库成功sql = \"insert into userinfo (username,password) values(\'root\',\'123123\')\"#sql = \"insert into userinfo(username,password) values(%S,%s)\"cursor.execute(sql)#cursor.execute(sql,user,pwd)conn.commit()#将数据提交到数据库!!!要修改里面的值的时候,必须要commitcursor.close()#关闭连接conn.close()#关闭连接
批量增加
conn = pymysql.connect(host=\"localhost\", user=\'root\', password=\'\', database=\"db1\")#conn=在localhost的mysql用密码为 的root用户连接数据库db1cursor = conn.cursor()#连接数据库成功sql = \"insert into userinfo (username,password) values(%s,%s)\"cursor.executemany(sql, [(\'egon\',\'sb\'),(\'laoyao\',\'Bs\')])#executemany适用于insert的时候#r = cursor.executemany(sql, [(\'egon\',\'sb\'),(\'laoyao\',\'Bs\')])#表示受影响的行数conn.commit()cursor.close()conn.close()
2.3 python中对mysql数据库进行查看操作
import pymysqlconn = pymysql.connect(host=\"localhost\", user=\'root\', password=\'\', database=\"db1\")cursor = conn.cursor()#连接数据库成功(输出时默认元组形式)#cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)#(输出为字典,更加方便)sql = \"select * from userinfo\"#sql = \"selete * from userinfo limit 10\"#限制之后不论查看多少一次性都不能超过十条cursor.execute(sql)result = cursor.fetchone()result = cursor.fetchmany(4)#可以一次性查看四条result = cursor.fetchall()#一次性查看全部print(result)cursor.close()#关闭连接conn.close()#关闭连接
2.4 新插入数据的自增ID
conn = pymysql.connect(host=\"localhost\", user=\'root\', password=\'\', database=\"db1\")#conn=在localhost的mysql用密码为 的root用户连接数据库db1cursor = conn.cursor()#连接数据库成功sql = \"insert into userinfo(username,password) values(\'sarfaca\',\'123123\')\"cursor.execute(sql)conn.commit()cursor.lastrowid#新插入数据的自增IDprint(cursro.lastrowid)conn.commit()cursor.close()conn.close()
- execute() 防止SQL注入
- 增删改: conn.commit()
- fetchone fetchall
- 获取插入数据自增ID