2017-06-03 57 views
2

我在python中有一个MYSQL查询。它运行良好。但是,在删除表中的某个元组之后,会出现以下错误。_mysql_exceptions.OperationalError:(1271,“非法混合操作排序”IN)“python

​​

以下是我的代码到目前为止。

with open('out.csv', 'rb') as csvfile: 
reader = csv.DictReader(csvfile) 
for row in reader: 
    text=(row['text']) 
    emotion = (row['emotion']) 
    my_list = (text, emotion) 
    my_array = np.asarray(my_list) 
    sentence = (text.lower()) 
    w_tokenize = (word_tokenize(sentence)) 

    negation_words = [w for i, w in enumerate(w_tokenize) if i and (
     w_tokenize[i - 1] in ["not", "never", "no", "nobody", "nothing", "neither", "doesn't", "isn't", "wasn't", 
         "shouldn't", "wouldn't", "couldn't", "won't", "can't", "don't", "didn't"])] 

    if w_tokenize== []: 
     Value = '' 
    else: 
     cursor = conn.cursor() 
     format_strings = ','.join(['%s'] * len(w_tokenize)) 
     cursor.execute("SELECT emotion_type FROM emotion WHERE key_word IN (%s)" % format_strings, 
         tuple(w_tokenize)) 
     results = [res[0] for res in cursor.fetchall()] 
     if results: 
      frequencies = Counter(results) 
      Value = max(frequencies, key=frequencies.get) 
     else: 
      Value = '' 

但我找不到解决方案。谁能帮我。

+0

你能分享你的'w_tokenize'吗? –

回答

0

首先,你打电话execute错误的方式。您通过字符串格式传递参数。这是非常危险的(让你打开SQL注入),并导致意想不到的结果。正确的方法是

cursor.execute("SELECT emotion_type FROM emotion WHERE key_word IN (%s)" , tuple(w_tokenize)) 

我想你可能是为了作为参数传递是",".join(w_tokenize)

cursor.execute("SELECT emotion_type FROM emotion WHERE key_word IN (%s)" , ",".join(w_tokenize)) 

,因为所发生的IN子句中应该是一个子查询或逗号分隔字符串。 mysql连接器不会自动将你的元组翻译成CSV

+0

我尝试了你的方式。但它也会产生错误,即“query = query%db.literal(args) TypeError:并非在字符串格式化过程中转换的所有参数” –

+0

您应该使用'“,”。join(w_tokenize)'请参阅更新 – e4c5

+0

是的,我是纠正。但仍造成mysql 1271错误。 –

相关问题