pymysql使用和安装

pymysql使用
1.下载pymysql
1.pip3 install pymysql
2.pycharm–>file–>settings–>Project Interpreter,将pymysql加进去

2.导入pymysql模块
import pymysql

3.创建链接及小例

1.打开数据库连接

conn=pymysql.connect(host="127.0.0.1",port=3306,user=user,password=pwd,database="seu",charset="utf8")

host:数据库主机地址
post:mysql默认端口3306
user、password:mysql用户
database:打开的数据库名字
charset:以utf-8形式打开,注意写utf8
#创建游标,相当于cmd的光标

2.使用cursor()创建一个游标对象cursor

cursor=conn.cursor()

3.使用execute()执行一条mysql语句,并返回受影响的行数

effect_row=cursor.execute("select * from student")
print(effect_row)

effect_row打印出来是6,因为student此时一共有6条数据,都被查询;

# 使用 execute() 方法执行 SQL,如果表存在则删除
cursor.execute("DROP TABLE IF EXISTS EMPLOYEE")
 
# 使用预处理语句创建表
sql = """CREATE TABLE EMPLOYEE (
         FIRST_NAME  CHAR(20) NOT NULL,
         LAST_NAME  CHAR(20),
         AGE INT,  
         SEX CHAR(1),
         INCOME FLOAT )"""
 
cursor.execute(sql)

以上是execute()。

还可以使用executemany()执行多条语句并执行,并返回受影响行数

effect_row = cursor.executemany("insert into hosts(host,color_id)values(%s,%s)", [
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值