mysql/python 连接器在处理空值时使用参数和字典死机
Posted
技术标签:
【中文标题】mysql/python 连接器在处理空值时使用参数和字典死机【英文标题】:mysql/python connector using a params and a dictionary dying when handling a null value 【发布时间】:2014-06-12 08:42:19 【问题描述】:insert_statement = ("INSERT INTO mydb.sets_ledger "
"( exercise, weight, reps, rpe) "
"VALUES ( %(Exercise)s, %(weight)s, %(reps)s, %(rpe)s")
#import the CSV file
workout_file = "squat_rpe.csv"
file_hook = open(workout_file, 'rb')
dictionary=csv.DictReader(file_hook)
print dictionary.dialect
print dictionary.fieldnames
for row in dictionary:
print row['Day'], row['Exercise'], row['weight'], row['reps'], row['rpe']
print "==> "
cursor.execute(insert_statement, row)
当我从字典中的其中一列(在本例中为“rpe”列为空的情况下)输入一行时,它崩溃了。
row = 'rpe': '', 'weight': '60', 'reps': '3', 'Day': '04/26/14', 'Exercise': 'Competition Back Squat’
给我最后的错误
mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
rpe、reps、weight 的 mysql 架构都是小数(10,5)
当数据周围有空值时,如何使用字典/参数方法生成 sql 查询?
====================================
我建立了一个单独的测试:
import csv
import mysql.connector
#engage the MySql Table
cnx= mysql.connector.connect(user='root', host='127.0.0.1', database='mydb')
cursor = cnx.cursor()
insert_statement_dic = ("INSERT INTO mydb.sets_ledger "
"( exercise, rpe) "
"VALUES ( %(exercise)s, %(rpe)s)")
insert_statement_tuple = ("INSERT INTO mydb.sets_ledger "
"( exercise, rpe) "
"VALUES ( %s, %s)")
tuple_param_10 = ('tuple insert test 10', 10)
tuple_param_pure_none = ('tuple insert test None', None)
tuple_param_quoted_none = ('tuple insert test quoted none', 'None')
cursor.execute(insert_statement_tuple, tuple_param_10);
cursor.execute(insert_statement_tuple, tuple_param_pure_none);
#cursor.execute(insert_statement_tuple, tuple_param_quoted_none);
dic_param_10 = 'exercise': 'dic_param_10', 'rpe': 19
dic_param_None = 'exercise': 'dic_param_None', 'rpe': None
dic_param_quoted = 'exercise': 'dic_param_None', 'rpe': 'None'
cursor.execute(insert_statement_dic, dic_param_10);
cursor.execute(insert_statement_dic, dic_param_None);
#cursor.execute(insert_statement_dic, dic_param_quoted);
cnx.commit()
cursor.close()
cnx.close()
要在十进制列中插入“无值”(NULL),我需要插入 >None'''None'
这意味着当我从逗号分隔值构建参数字典时,我需要执行一个清理函数,将任何空字符串 ('') 转换为 _builtin.None。 (除非我能想出办法让 CSV 模块自动将空字符串 ('') 转换为 None。
【问题讨论】:
【参考方案1】:错误不是关于您的值是空的,而是关于您的查询中的错误。错误消息只是提示您在查询中发生解析错误的位置,即''
。
如果您在查询后打印cursor._executed
,您会看得更清楚,它应该会显示如下内容:
INSERT INTO ... VALUES ( ..., ''
如您所见,真正的问题是您的查询缺少右括号,它应该是:
insert_statement = ("INSERT INTO mydb.sets_ledger "
"( exercise, weight, reps, rpe) "
"VALUES ( %(Exercise)s, %(weight)s, %(reps)s, %(rpe)s )")
【讨论】:
_executed = 'INSERT INTO mydb.sets_ledger ( exercise, weight, reps, rpe) VALUES ( \\'Competition Back Squat\\', \\'60\\', \\'3\\', \\'\\’)'
如果我删除了“\”并将该查询直接输入 mysql,它会抱怨我将 '' 插入 rpe(这是一个小数(10,5))Error Code: 1366. Incorrect decimal value: '' for column 'rpe' at row 1
以上是关于mysql/python 连接器在处理空值时使用参数和字典死机的主要内容,如果未能解决你的问题,请参考以下文章
简单 SQL Express 完全连接查询在应该返回空值时不返回
将 0 和 1 (float64) 转换为布尔值时将空值转换为 True [重复]