1、pymysql的安装

1.1 conda环境解释器在线安装

以管理员身份运行终端或者使用Anaconda Powershell Prompt并以管理员身份运行

conda install -c conda-forge pymysql

注意:conda安装pymysql后,只有使用conda解释器才可以调用

1.2 Python环境在线安装

1、找到python安装位置,打开终端输入 where python

2、输入cd 进入python位置目录

3、进入Scripts

4、输入pip install pymysql即可

2、写操作流程

1、建立数据库连接 (db = pymysql.connect(...))

connect()参数:host(主机地址,本地localhost)、port(端口号,默认3306)

user(用户名)、password(密码)、database(数据库)、charaset(编码方式,默认utf8)

2、创建游标对象 (c = db.cursor())

3、游标方法 c.execute("insert......")

4、提交到数据库 db.commit() 将写操作提交到数据库(增删改)

5、关闭游标对象 c.close()

6、断开数据库连接 db.close()

3、写操作(插入)案例

3.1 案例1

Python中的sql这个字符串,格式与sql语句保持一致,即可以复制到终端运行

import pymysql

# 连接数据库

db = pymysql.connect(host='localhost',

port=3306,

user='root',

password='1234',

database='student',

charset='utf8')

# 获取游标(操作数据库,执行sql语句,承载结果)

cur = db.cursor()

# 执行SQL语句

sql = "insert into class (name,age,sex,score) values ('王俊龙',19,'w',67),('李伯俊',19,'m',81);"

cur.execute(sql)

# 提交写操作,可将多次写操作一起提交

db.commit()

cur.close()

db.close()

3.2 案例2

sql语句处理方式1:此种方式需要注意字符串占位符加引号’',目的是保持与sql语句格式完全匹配 sql语句:sql = “insert into class (name,age,sex,score) values (‘王俊龙’,19,‘w’,67),(‘李伯俊’,19,‘m’,81);” 故字符串占位符一定要加 引号 sql = “INSERT INTO interest (name,hobby,price,level,comment) VALUES (‘%s’,‘%s’,%f,‘%s’,‘%s’);” % ( name, hobby, price, level, comment)

import pymysql

# 连接数据库

db = pymysql.connect(host='localhost',

port=3306,

user='root',

password='1234',

database='student',

charset='utf8')

# 获取游标(操作数据库、执行sql语句、承载结果)

cur = db.cursor()

name = input("姓名:")

hobby = input("爱好:")

price = int(input("价格:"))

# 不用强转也可以,sql语句中字符串带'',

# 数字没有,而在下方格式化中,数字直接取代占位符,与sql语句一致;而字符串需要在占位符上加''

level = input("水平:")

comment = input("评语:")

# 注意:下列字符串格式化时一定要与sql语句格式一致,字段对应的值如果是字符串加 ''

# 例如:sql = "insert into class (name,age,sex,score) values ('王俊龙',19,'w',67),('李伯俊',19,'m',81);"

sql = "INSERT INTO interest (name,hobby,price,level,comment) VALUES ('%s','%s',%f,'%s','%s');" % (

name, hobby, price, level, comment)

try:

cur.execute(sql)

db.commit()

except Exception as e:

db.rollback()

finally:

cur.close()

db.close()

3.3 案例3

使用pymysql进行读操作,sql语句处理方式2 执行sql语句时,用参数二列表中的值顺次匹配%s sql = “INSERT INTO interest (name,hobby,price,level,comment) VALUES (%s,%s,%s,%s,%s);” cur.execute(sql, [name, hobby, price, level, comment])

import pymysql

db = pymysql.connect(host='localhost',

port=3306,

user='root',

password='1234',

database='student',

charset='utf8')

cur = db.cursor()

name = input("姓名:")

hobby = input("爱好:")

price = input("价格:")

# 不用强转也可以,sql语句中字符串带'',

# 数字没有,而在下方格式化中,数字直接取代占位符,与sql语句一致;而字符串需要在占位符上加''

level = input("水平:")

comment = input("评语:")

sql = "INSERT INTO interest (name,hobby,price,level,comment) VALUES (%s,%s,%s,%s,%s);"

try:

# 执行sql语句时,会自动查找VALUES后面的值,用参数2列表中的值顺次匹配,会自动适应sql语句格式

# (不用管列表中值的数据类型,sql字符串中直接%s),

# 此时sql这个字符串中的%s不用加''

cur.execute(sql, [name, hobby, price, level, comment])

db.commit()

except Exception as e:

db.rollback()

finally:

cur.close()

db.close()

**注意:**写操作除上述的插入操作,还有删除、修改操作,过程与案例1、2相似,更换sql语句就可以,案例3情况,只能传递某些字段的值或参量,字段名与表名不可以传递

4、读操作流程

import pymysql

# 连接数据库

db = pymysql.connect(host='localhost',

port=3306,

user='root',

password='1234',

database='student',

charset='utf8')

# 创建游标对象

cur = db.cursor()

sql = "select * from interest"

# 执行sql语句

cur.execute(sql)

# 打印读取的内容

print(cur.fetchone()) # 返回值是一个元组

print(cur.fetchmany(3)) # 游标输出内容,等待下次输出时会接着上次游标所在位置读取(该语句输出三条记录)

print(cur.fetchall()) # 单独使用,输出所有的记录;但此时会在上述游标操作后的位置输出所有记录(返回值为元组,里面每一条记录也是元组)

cur.close()

db.close()

"""

# (1, 'Tom', 'sing,dance', Decimal('16800.00'), 'B', '表现不错,进步好快')

# ((2, 'kevin', 'draw', Decimal('18800.00'), 'A', '基础扎实,画风突出'), (3, 'lily', 'sing,draw', Decimal('13499.00'), 'C', '进步空间巨大'), (5, 'Mary', 'sing', Decimal('13900.00'), 'B', '未来可期'))

# ((6, 'Jim', 'sing,dance', Decimal('16900.00'), 'C', '再接再厉'),)

"""

参考阅读

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。