2017-08-03 11:39:35   Visit  137

sql语句中包含汉字的时候会有以下警告,并且保存的数据为乱码

    sql="insert into zhilian (name ,salary,location,time,company) values('JAVA','8001-10000','北京','今天','北京') "
    execute(sql)
L:/python/spider/database/mysql.py:35: Warning: Incorrect string value: '\xE5\x8C\x97\xE4\xBA\xAC' for column 'location' at row 1
  cursor.execute(sql)
L:/python/spider/database/mysql.py:35: Warning: Incorrect string value: '\xE4\xBB\x8A\xE5\xA4\xA9' for column 'time' at row 1
  cursor.execute(sql)
L:/python/spider/database/mysql.py:35: Warning: Incorrect string value: '\xE5\x8C\x97\xE4\xBA\xAC' for column 'company' at row 1
  cursor.execute(sql)

解决方法

在连接数据库的时候添加字符集参数

    conn= MySQLdb.connect(
        host='127.0.0.1',
        port = 3306,
        user='root',
        passwd='123',
        db ='test',
        charset = 'utf8'
        )
©2017 Leechg.com