pythonmysql亂碼
發布時間: 2023-07-11 10:59:45
Ⅰ python2.7中如果腳本文件放在中文目錄下,pymysql連接資料庫會報錯,怎麼解決(不要修改中文目錄名為英文)
解決方法為:
創建數據表時指定charset=utf8,如:
usehongxiudb;
createtableifnotexistshongxiu(
nametext,
authortext,
introtext
)engine=InnoDBdefaultcharset=utf8;
Ⅱ python在網頁上爬取數據然後輸入mysql,python中直接輸出是中文,在mysql的表中打開變成亂碼了。
你如果是用 MySQLdb 操作的資料庫,那麼執行完sql語句後需要commit。例:
conn = MySQLdb.connect(user='xxx', db='xxx', passwd='xxx', host='127.0.0.1', use_unicode=True, charset='utf8')
cur = conn.cursor()
cur.execute('update table set xxx=xxx')
conn.commit()
cur.close()
conn.close()
熱點內容