from sqlalchemy import create_engine
from consts import DB_URI
eng = create_engine(DB_URI)
with eng.connect() as con:
con.execute('drop table if exists users')
con.execute('create table users(Id INT PRIMARY KEY AUTO_INCREMENT, '
'Name VARCHAR(25))')
con.execute("insert into users(name) values('xiaoming')")
con.execute("insert into users(name) values('wanglang')")
rs = con.execute('select * from users')
for row in rs:
print row
consts.py的代碼為(用python驅動可以正常連接數據庫並操作):
HOSTNAME = 'localhost'
DATABASE = 'john'
USERNAME = 'root'
PASSWORD = '123'
DB_URI = 'mysql://{}:{}@{}/{}'.format(USERNAME, PASSWORD, HOSTNAME, DATABASE)
出現如下警告:
Python27\lib\site-packages\sqlalchemy\engine\default.py:469: Warning: Incorrect string value: '\xD6\xD0\xB9\xFA\xB1\xEA...' for column 'VARIABLE_VALUE' at row 479
cursor.execute(statement, parameters)
(1L, 'xiaoming')
(2L, 'wanglang')
Process finished with exit code 0
數據庫為mysql5.7版本
不明白為什麼會出現警告,求大神解答!!
編碼不一致導致的問題,Flask 或者是Django 默認都是UTF-8的,有可能你的數據庫服務器的編碼格式不一致導致的,
查看服務器的/etc/my.cnf 文件
修改:
1. 修改 /etc/my.cnf 配置文件,然後重啟mysqld。
在[client]下配置:default-character-set=utf8
在[mysqld]下配置:
default-character-set=utf8
init_connect=’SET NAMES utf8′