Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python UnicodeDecodeError:&x27;ascii';编解码器可以';t解码第139位的字节0xe2:序号不在范围内(128)_Python_Mysql_Python 2.7_Twitter - Fatal编程技术网

Python UnicodeDecodeError:&x27;ascii';编解码器可以';t解码第139位的字节0xe2:序号不在范围内(128)

Python UnicodeDecodeError:&x27;ascii';编解码器可以';t解码第139位的字节0xe2:序号不在范围内(128),python,mysql,python-2.7,twitter,Python,Mysql,Python 2.7,Twitter,我正在编写一个代码,该代码基于搜索词从twitter获取实时推文,并将其保存到Mysql数据库。但是,当我在插入数据库时运行代码时,它会引发一个错误: UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 139: ordinal not in range(128) 我无法理解这里的问题是要插入数据库的代码 tweet = json.loads(data); #print json.dumps(twee

我正在编写一个代码,该代码基于搜索词从twitter获取实时推文,并将其保存到Mysql数据库。但是,当我在插入数据库时运行代码时,它会引发一个错误:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 139: ordinal not in range(128)
我无法理解这里的问题是要插入数据库的代码

tweet = json.loads(data);
    #print json.dumps(tweet, indent=4, sort_keys=True)
    #print tweet['text']
    tweetid = tweet['id_str']
    userid = tweet['user']['id_str']
    text = tweet['text'].encode('utf-8')
    cur.execute("""INSERT INTO twitterfeeeds(tweet_id, user_id,body,status) VALUES (%s,%s,%s,'0')"""%(tweetid,userid,text))
    db.commit()

这里的正文是tweet中的文本,状态是它是否被处理。

不要将tweet编码为UTF-8,也不要使用字符串格式来创建查询

请改用SQL参数:

tweetid = tweet['id_str']
userid = tweet['user']['id_str']
text = tweet['text']
cur.execute(
    """INSERT INTO twitterfeeeds(tweet_id, user_id,body,status) VALUES (%s, %s, %s, '0')""",
    (tweetid, userid, text))
是的,上述代码与您的代码之间存在差异;
tweetid
userid
text
值都作为一个单独的参数(元组)传递给
游标。execute()
方法

游标负责处理要插入数据库的数据的正确转义。这样可以避免SQL注入攻击(使用
;DROP TABLE twitterfeeeds
的tweet会立即破坏您的数据库),并启用查询计划优化

这一切都需要您配置数据库连接以支持Unicode数据;在连接上将字符集设置为UTF-8:

conn = MySQLdb.connect(host="localhost", user='root', password='', 
                       db='', charset='utf8')
或者更好的是,将数据库配置为使用UTF8MB4字符集(MySQL使用的UTF-8版本无法处理表情符号或U+FFFF以外的其他代码点):


不要将推文编码为UTF-8,也不要使用字符串格式来创建查询

请改用SQL参数:

tweetid = tweet['id_str']
userid = tweet['user']['id_str']
text = tweet['text']
cur.execute(
    """INSERT INTO twitterfeeeds(tweet_id, user_id,body,status) VALUES (%s, %s, %s, '0')""",
    (tweetid, userid, text))
是的,上述代码与您的代码之间存在差异;
tweetid
userid
text
值都作为一个单独的参数(元组)传递给
游标。execute()
方法

游标负责处理要插入数据库的数据的正确转义。这样可以避免SQL注入攻击(使用
;DROP TABLE twitterfeeeds
的tweet会立即破坏您的数据库),并启用查询计划优化

这一切都需要您配置数据库连接以支持Unicode数据;在连接上将字符集设置为UTF-8:

conn = MySQLdb.connect(host="localhost", user='root', password='', 
                       db='', charset='utf8')
或者更好的是,将数据库配置为使用UTF8MB4字符集(MySQL使用的UTF-8版本无法处理表情符号或U+FFFF以外的其他代码点):


use可以使用MySQLdb.escape\u字符串转义unicode字符

>> MySQLdb.escape_string("'")
"\\'"
此外,我认为您必须使用“use_unicode”打开“mysql.connector”:真正的配置:

config = {
'user': ...,
'password': ...,
'host': '127.0.0.1',
'use_unicode':True,
'charset':'utf8',
}
db = mysql.connector.connect(**config)

use可以使用MySQLdb.escape\u字符串转义unicode字符

>> MySQLdb.escape_string("'")
"\\'"
此外,我认为您必须使用“use_unicode”打开“mysql.connector”:真正的配置:

config = {
'user': ...,
'password': ...,
'host': '127.0.0.1',
'use_unicode':True,
'charset':'utf8',
}
db = mysql.connector.connect(**config)

如果未对tweet进行编码,则会引发另一个错误UnicodeEncodeError:“latin-1”编解码器无法对234:ordinal中的字符u'\u2026'进行编码,不在范围(256)内@Harwee:作为查询参数传入时不能。@Harwee:您需要将数据库配置为接受UTF-8 Unicode文本,当前您的数据库只能处理Latin-1。如果未对tweet进行编码,则会引发另一个错误UnicodeEncodeError:“Latin-1”编解码器无法对234:ordinal不在范围(256)中的字符u'\u2026'进行编码。@Harwee:当作为查询参数传入时,不会。@Harwee:您需要将数据库配置为接受UTF-8 Unicode文本,目前,您的数据库只能处理拉丁语-1。