利用python数据库接口MySQLdb编写操作数据库的小程序时,报错:
MySQLdb._exceptions.OperationalError: (2019, "Can't initialize character set utf-8 (path: C:\Program Files\MySQL\MySQL Server 6.1\share\charsets\)")
后来发现charset的参数有问题,不是"utf-8",而是"utf8"
正确写法如下:
db = MySQLdb.connect("localhost", "db_account", "db_password", "db_name", charset="utf8")
顺便附上一个实例。
import MySQLdb
db = MySQLdb.connect("localhost", "db_account", "db_password", "db_name", charset="utf8")
cursor = db.cursor()
cursor.execute("SELECT VERSION()")
data = cursor.fetchone()
print("Database version:%s " %data)
db.close()