【发布时间】:2018-06-05 05:32:53
【问题描述】:
尝试在某些 python 代码中执行查询时,我收到一个 InterfaceError: (0,'')。我将在下面发布我正在使用的代码。
def insert_into_table(schema, table, connection, filename = 'pathToAnInputFile'):
x = connection.cursor()
with open(filename, 'rb') as csvfile:
reader = csv.reader(csvfile, delimiter=',', quotechar='"')
next(reader)
for line in reader:
query = "INSERT INTO {0}.{1} (field1, field2, field3) VALUES ('{2}', '{3}', '{4}')".format(table, schema, line[0], line[1], line[2])
print query
x.execute(query)
conn.commit()
conn.close()
import MySQLdb
import csv as csv
conn = MySQLdb.connect("localhost", "nottheusername", "notthepassword", db="adb")
insert_into_table('atablename', 'adb', conn)
这是我得到的输出
INSERT INTO adb.atablename (field1, field2, field3) VALUES ('dogs', '小狗','2017 年 11 月 21 日') -------------------------------------------------- ------------------------- InterfaceError Traceback(最近一次调用最后一次) 在 () ----> 1 insert_into_table('atablename', 'adb', conn)
<ipython-input-24-02df4bf9bbdb> in insert_into_table(schema, table, connection, filename) 8 query = "INSERT INTO {0}.{1} (RMS_CLASS_NM, campaign, ad_group) VALUES ('{2}', '{3}', '{4}')".format(table,架构,行[0],行[1],行[2]) 9 打印查询 ---> 10 x.execute(查询) 11 conn.commit() 12
C:\Users\braddavi\AppData\Local\Continuum\Anaconda2\lib\site-packages\MySQLdb\cursors.pyc在执行(自我,查询,参数) 224 例外: 225 exc,值 = sys.exc_info()[:2] --> 226 self.errorhandler(self, exc, value) 第227章 228 如果不是 self._defer_warnings: self._warning_check()
C:\Users\braddavi\AppData\Local\Continuum\Anaconda2\lib\site-packages\MySQLdb\connections.pyc在默认错误处理程序中(解析参数失败) 34德尔连接 35 if isinstance(errorvalue, BaseException): ---> 36 引发错误值 37 如果错误类不是无: 38 引发错误类(错误值)
InterfaceError: (0, '')
我已经搜索了有关此特定错误的信息,似乎它通常与创建全局游标有关,而不是在使用它的函数中创建本地游标,但这不是在这种情况下的问题 - 或者至少如果是的话,这对我来说并不明显。
我还应该指出,如果我从打印查询行中打印出查询并直接在 mysqldb 终端中执行它,它会毫无困难地执行并插入数据。
如果有人可以提供帮助,将不胜感激。
祝大家节日快乐。 布拉德
【问题讨论】:
标签: python mysql-python