pythonmysql报错
Python连接mysql数据库报错
这里的意思是:数据库连不上啊。
可能是网络问题,可能是防火墙问题,可能是3306端口没开。你先排除这些问题吧。用一些mysql工具连接测试看,比如SQLyog 测试。
⑵ python向mysql中插入数据提示错误
答:数据库本身是不支持字典类型的(已知的绝大多)
方法有两种:
1、把字典或者json格式数据作为字符串(string)直接插入、读取的时候再转为字典。
2、为了避免某些字符、引号等转义问题发生、可以使用base64编码后导入数据库、读取时候再用base64解码。
也可以试试这个:
sql="insertintoaccount·userdata(user、passwd)values(%s、%s)"cursor.execute(sql、(name、passwd-md5))试试这个,
或者:sql="insertintoaccunt.userdata(user.passwd)values(%s%s)"%(name、passwd-md5)
oursor.execute(sql)。
⑶ python 连接mysql数据库报错
编辑mysql配置文件my.ini(在MySQLServer的安装目录),在[mysqld]这个条目下加入 skip-grant-tables
保存退出后重启mysql
1.点击“开始”->“运行”(快捷键Win+R)。
2.启动:输入 net stop mysql
3.停止:输入 net start mysql
这时候在cmd里面输入mysql -u root -p就可以不用密码登录了,出现password:的时候直接回车可以进入,不会出现ERROR 1045 (28000),但很多操作都会受限制,因为我们不能grant(没有权限)。按下面的流程走(红色部分为输入部分,绿色的是执行后显示的代码不用输入):
1.进入mysql数据库:
mysql> use mysql; Database changed
2.给root用户设置新密码,蓝色部分自己输入: mysql> update user set password=password("新密码") where user="root"; Query OK, 1 rows affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0
3.刷新数据库 mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
4.退出mysql: mysql> quit Bye
改好之后,再修改一下my.ini这个文件,把我们刚才加入的"skip-grant-tables"这行删除,保存退出再重启mysql就可以了。
总结:猜测根本原因就是简单的密码输入错误,通过以上方法我们可以不验证密码来连接上mysql,然后设置新密码。
⑷ python3.6执行pymysql报错
折腾好半天的数据库连接,由于之前未安装 pip ,而且自己用的Python 版本为3.6. 只能用 pymysql 来连接数据库,下边 简单介绍一下 连接的过程,以及简单的增删改查操作。
1.通过 pip 安装 pymysql
进入 cmd 输入 pip install pymysql
回车等待安装完成;
安装完成后出现如图相关信息,表示安装成功。
2.测试连接
import pymysql #导入 pymysql ,如果编译未出错,即表示 pymysql 安装成功
简单的增删改查操作
示例表结构
2.1查询操作
[python] view plain
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
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
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
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()