python 数据库实践经验

MySQL

ubuntu下是按照网上搜来的进行安装,python部分按照廖雪峰的教材来安装,refer to:https://www.liaoxuefeng.com/wiki/1016959663602400/1017802264972000

pip install mysql-connector
mysql -u root -p

执行下面的脚本,提示出错。

import mysql.connector

# change root password to yours:
# conn = mysql.connector.connect(user=‘root‘, password=‘password‘, database=‘test‘)
conn = mysql.connector.connect(user=root, password=‘‘, database=test)

cursor = conn.cursor()
# 创建user表:
# cursor.execute(‘create table user (id varchar(20) primary key, name varchar(20))‘)
# 插入一行记录,注意MySQL的占位符是%s:
cursor.execute(insert into user (id, name) values (%s, %s), (3, Michael))
print(rowcount =, cursor.rowcount)
# 提交事务:
conn.commit()
cursor.close()

# 运行查询:
cursor = conn.cursor()
cursor.execute(select * from user where id = %s, (2,))
values = cursor.fetchall()
print(values)
# 关闭Cursor和Connection:
cursor.close()
conn.close()

摸索解决方法

GRANT ALL PRIVILEGES ON * . * TO root@localhost;
ALTER USER root@localhost IDENTIFIED WITH mysql_native_password BY ‘‘;

可能是仅仅第二行解决了问题。感觉“set password for root@localhost = password(‘’);”这个是没有效果的。

 

python 数据库实践经验

上一篇:mysql主从同步


下一篇:Eclipse 安装对 Java 8 的支持