pymysql
python链接mysql数据库
pyMysql介绍
pyMsql是在py3中连接mysql服务器的一个库,但是在py2中则使用mysqldb
在django中也可以使用pymysql来连接mysql数据库
pymysql安装
pip install pymysql(打开cmd输入这句话即可)
连接数据库
注意
要保证有一个mysql数据库,并且已经启动
有一个可以连接该数据库的用户名和密码
有操作权限的database
具体使用如下图所示
具体连接操作
第一步
.
第二步
第三步
第四步
基本使用
import pymysql
# 连接数据库
conn=pymysql.connect(host="localhost",user="root",password="123",database="db6",charset="utf8")
# 创建游标
cursor=conn.cursor()
# 写sql语句
# sql="""
# create table userinfo2(
# id INT auto_increment PRIMARY KEY , #创建表
# NAME VARCHAR(32),
# password VARCHAR (32)
# )
# """
# 执行sql语句
# cursor.execute(sql)
# cursor.executemany("insert into userinfo2 values(%s,%s,%s)",[(1,"desheng","1234"), #往表中插入数据
# (2,"qinxiao","1234"),
# (3,"xiaomei","1234")
#
# ])
ret=cursor.execute("select * from userinfo2 where name ='dengsheng' and password='1234';") #当一切准备就绪以后我们就可以开始查询了
# 提交
print(ret)
conn.commit()
# 关闭指针对象
cursor.close()
# 关闭连接对象
conn.close()
增删改查操作
增操作
import pymysql # 连接 conn=pymysql.connect(host="localhost",user="root",password="123",database="db6",charset="utf8") cursor=conn.cursor() # 写sql语句 id=20 name="anan" password="123" sql="insert into userinfo (id,name,password) value(%s,%s,%s);" try: cursor.execute(sql,(id,name,password)) conn.commit() except Exception as e: conn.rollback() print(e) cursor.close() conn.close()
import pymysql conn=pymysql.connect(host="localhost",database="db6",user="root",password="123",charset="utf8") cursor=conn.cursor() sql="insert into userinfo(id,name,password) VALUES(%s,%s,%s)" id1=15 user1="tingting" pwd1="12333" id2=16 user2="junjun" pwd2="123444" data=((id1,user1,pwd2),(id2,user2,pwd2)) try: cursor.executemany(sql,data) conn.commit() except Exception as e: print(e) conn.rollback() cursor.close() conn.close()
import pymysql conn=pymysql.connect(host="localhost",database="db6",user="root",password="123",charset="utf8") cursor=conn.cursor() sql="insert into userinfo(name,password) VALUES(%s,%s)" id1=23 user1="tingting" pwd1="12333" try: cursor.execute(sql,(user1,pwd1)) # 拿到插入的这条的ID print(cursor.lastrowid) last_id=cursor.lastrowid conn.commit() print("ID为:", last_id) except Exception as e: print(e) conn.rollback() cursor.close() conn.close()
删操作
import pymysql conn=pymysql.connect(host="localhost",database="db6",user="root",password="123",charset="utf8") cursor=conn.cursor() sql="delete from userinfo where name=%s" name="lanbo" try: cursor.execute(sql,(name)) conn.commit() except Exception as e: conn.rollback() cursor.close() conn.close()
改操作
import pymysql # 获取用户输入 # name=input("用户名>>>:") # pwd=input("密码>>>:") # 连接数据库 conn=pymysql.connect(host="localhost",database="db6",user="root",password="123",charset="utf8") # 获取游标 cursor=conn.cursor() # 执行sql语句 sql= "update userinfo set password=%s where name=%s;" name="deshen" print(sql) pwd="1233333" # 让pymysql拼接字符串 try: cursor.execute(sql,(pwd,name)) conn.commit() except Exception as e: print(e) conn.rollback() cursor.close() conn.close()
查操作(包括单条多条进阶用法)
import pymysql conn=pymysql.connect(host="localhost",database="db6",user="root",password="123",charset="utf8") cursor=conn.cursor() # 写sql语句 sql="select * from userinfo" # 执行sql语句 ret=cursor.execute(sql) print(ret) # 打印此时又多少条数据 # 一次取一条 print(cursor.fetchone()) print(cursor.fetchone()) #一次取所有 # print(cursor.fetchall()) # 一次取3条 print(cursor.fetchmany(3)) # 进阶用法 # 移动取数据的光标 cursor.scroll(-2) #默认是相对移动的 print(cursor.fetchone()) # 按照绝对位置去移动 cursor.scroll(4,mode="absolute") print(cursor.fetchone()) cursor.close() conn.close()