1、pymysql连接
import pymysql #导入pymysql模块 conn=pymysql.connect( #连接mysql数据库 host="localhost", port=3306, #端口号是数据类型 database="db1", #本地数据库名字 user="root", password="123456", charset="utf8" #没有- ) cursor=conn.cursor() #获取输入sql语句的光标对象 sql="select * from info " #sql语句 ret=cursor.execute(sql) #执行sql语句 print(ret) #打印 cursor.close() conn.close()
2、pymysql连接验证登陆
user=input("请输入姓名:") pwd=input("请输入密码:") import pymysql conn=pymysql.connect( host="localhost", port=3306, database="db1", user="root", password="123456", charset="utf8", ) cursor=conn.cursor() sql = "select * from userinfo where username = %s and password = %s" ret=cursor.execute(sql,[user,pwd]) if ret: print("登陆成功") else: print("登陆失败") cursor.close() conn.close()
3、pymysql增操作
import pymysql conn=pymysql.connect( host="localhost", port=3306, database="db1", user="root", password="123456", charset="utf8" ) cursor=conn.cursor() sql="insert into userinfo(username,password) VALUES (%s,%s)" cursor.execute(sql,['liuxing','lll']) conn.commit() cursor.close() conn.close()
4、获取插入数据的id:lastrowid
import pymysql #导入MySQL模块 conn=pymysql.connect( #连接 host="localhost", port=3306, database="db1", user='root', password='123456', charset="utf8", ) cursor=conn.cursor() #获取输入光标 sql1="insert into class(name) VALUES (%s)" #sql语句 sql2="insert into student(name,cid) VALUES (%s,%s)" #sql语句 cursor.execute(sql1,"全栈九期") #执行sql语句 new_id=cursor.lastrowid #获取最后插入数据的id并关联到另一个表 cursor.execute(sql2,["小东北",new_id]) #执行sql语句,把获取的id传入光标并执行 conn.commit() #提交 cursor.close() #关闭光标 conn.close() #关闭链接
5、异常回滚
# 导入pymysql模块 import pymysql # 连接database conn = pymysql.connect(host=“你的数据库地址”, user=“用户名”,password=“密码”,database=“数据库名”,charset=“utf8”) # 得到一个可以执行SQL语句的光标对象 cursor = conn.cursor() sql = "INSERT INTO USER1(name, age) VALUES (%s, %s);" username = "Alex" age = 18 try: # 执行SQL语句 cursor.execute(sql, [username, age]) # 提交事务 conn.commit() except Exception as e: # 有异常,回滚事务 conn.rollback() cursor.close() conn.close()
6、批量增操作
#批量插入操作 import pymysql conn=pymysql.connect( host="localhost", port=3306, database="db1", user="root", password="123456", charset="utf8" ) cursor=conn.cursor() sql="insert into userinfo(username,password) VALUES (%s,%s)" data=[("alex","dashabi"),("egon","xiaoshabi")] cursor.executemany(sql,data) conn.commit() cursor.close() conn.close()
7、删除
# 导入pymysql模块 import pymysql # 连接database conn = pymysql.connect(host=“你的数据库地址”, user=“用户名”,password=“密码”,database=“数据库名”,charset=“utf8”) # 得到一个可以执行SQL语句的光标对象 cursor = conn.cursor() sql = "DELETE FROM USER1 WHERE id=%s;" try: cursor.execute(sql, [4]) # 提交事务 conn.commit() except Exception as e: # 有异常,回滚事务 conn.rollback() cursor.close() conn.close()
8、改
# 导入pymysql模块 import pymysql # 连接database conn = pymysql.connect(host=“你的数据库地址”, user=“用户名”,password=“密码”,database=“数据库名”,charset=“utf8”) # 得到一个可以执行SQL语句的光标对象 cursor = conn.cursor() # 修改数据的SQL语句 sql = "UPDATE USER1 SET age=%s WHERE name=%s;" username = "Alex" age = 80 try: # 执行SQL语句 cursor.execute(sql, [age, username]) # 提交事务 conn.commit() except Exception as e: # 有异常,回滚事务 conn.rollback() cursor.close() conn.close()
9、查
# 导入pymysql模块 import pymysql # 连接database conn = pymysql.connect(host=“你的数据库地址”, user=“用户名”,password=“密码”,database=“数据库名”,charset=“utf8”) # 得到一个可以执行SQL语句的光标对象 cursor = conn.cursor() # 查询数据的SQL语句 sql = "SELECT id,name,age from USER1 WHERE id=1;" # 执行SQL语句 cursor.execute(sql) # 获取单条查询数据 ret = cursor.fetchone() cursor.close() conn.close() # 打印下查询结果 print(ret)
10、查询多条语句
# 导入pymysql模块 import pymysql # 连接database conn = pymysql.connect(host=“你的数据库地址”, user=“用户名”,password=“密码”,database=“数据库名”,charset=“utf8”) # 得到一个可以执行SQL语句的光标对象 cursor = conn.cursor() # 查询数据的SQL语句 sql = "SELECT id,name,age from USER1;" # 执行SQL语句 cursor.execute(sql) # 获取多条查询数据 ret = cursor.fetchall() cursor.close() conn.close() # 打印下查询结果 print(ret)
11、补充
# 可以获取指定数量的数据 cursor.fetchmany(3) # 光标按绝对位置移动1 cursor.scroll(1, mode="absolute") # 光标按照相对位置(当前位置)移动1 cursor.scroll(1, mode="relative")