python3.6执行pymysql报错

\pymysql\__init__.py",line90,inConnectreturnConnection(*args,**kwargs)File"C:\Users\l... \pymysql\__init__.py", line 90, in Connect
return Connection(*args, **kwargs)
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\connections.py", line 706, in __init__
self.connect()
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\connections.py", line 931, in connect
self._get_server_information()
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\connections.py", line 1245, in _get_server_information
packet = self._read_packet()
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\connections.py", line 987, in _read_packet
packet_header = self._read_bytes(4)
File "C:\Users\lenovo\AppData\Local\Programs\Python\Python36\lib\site-packages\pymysql\connections.py", line 1033, in _read_bytes
CR.CR_SERVER_LOST, "Lost connection to MySQL server during query")
pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query')
展开
 我来答
从空去听8
2017-07-20 · TA获得超过7439个赞
知道大有可为答主
回答量:6907
采纳率:93%
帮助的人:5577万
展开全部
折腾好半天的数据库连接,由于之前未安装 pip ,而且自己用的Python 版本为3.6. 只能用 pymysql 来连接数据库,下边 简单介绍一下 连接的过程,以及简单的增删改查操作。
1.通过 pip 安装 pymysql
进入 cmd 输入 pip install pymysql
回车等待安装完成;

安装完成后出现如图相关信息,表示安装成功。
2.测试连接
import pymysql #导入 pymysql ,如果编译未出错,即表示 pymysql 安装成功

简单的增删改查操作

示例表结构

2.1查询操作

[python] view plain copy
import pymysql #导入 pymysql

#打开数据库连接
db= pymysql.connect(host="localhost",user="root",
password="123456",db="test",port=3307)

# 使用cursor()方法获取操作游标
cur = db.cursor()

#1.查询操作
# 编写sql 查询语句 user 对应我的表名
sql = "select * from user"
try:
cur.execute(sql) #执行sql语句

results = cur.fetchall() #获取查询的所有记录
print("id","name","password")
#遍历结果
for row in results :
id = row[0]
name = row[1]
password = row[2]
print(id,name,password)
except Exception as e:
raise e
finally:
db.close() #关闭连接
2.2插入操作
[python] view plain copy
import pymysql
#2.插入操作
db= pymysql.connect(host="localhost",user="root",
password="123456",db="test",port=3307)

# 使用cursor()方法获取操作游标
cur = db.cursor()

sql_insert ="""insert into user(id,username,password) values(4,'liu','1234')"""

try:
cur.execute(sql_insert)
#提交
db.commit()
except Exception as e:
#错误回滚
db.rollback()
finally:
db.close()
2.3更新操作

[python] view plain copy
import pymysql
#3.更新操作
db= pymysql.connect(host="localhost",user="root",
password="123456",db="test",port=3307)

# 使用cursor()方法获取操作游标
cur = db.cursor()

sql_update ="update user set username = '%s' where id = %d"

try:
cur.execute(sql_update % ("xiongda",3)) #像sql语句传递参数
#提交
db.commit()
except Exception as e:
#错误回滚
db.rollback()
finally:
db.close()
2.4删除操作

[python] view plain copy
import pymysql
#4.删除操作
db= pymysql.connect(host="localhost",user="root",
password="123456",db="test",port=3307)

# 使用cursor()方法获取操作游标
cur = db.cursor()

sql_delete ="delete from user where id = %d"

try:
cur.execute(sql_delete % (3)) #像sql语句传递参数
#提交
db.commit()
except Exception as e:
#错误回滚
db.rollback()
finally:
db.close()
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式