MariaDB,pypyodbc,"未知的预处理语句处理程序"执行" SELECT"对载有" LOAD DATA LOCAL INFILE的表进行查询。"

时间:2015-09-08 22:30:33

标签: python-3.x odbc windows-7-x64 mariadb pypyodbc

Python 3.4.3,MariaDB 10.0.21,MariaDB ODBC Connector 1.0.0,pypyodbc 1.3.3,64位Windows 7上的所有64位。

我有一个python脚本,它应该创建一个表,用固定宽度文件中的数据填充它,然后对它运行一个SELECT语句。所有简单的东西。我的脚本看起来像这样:

import pypyodbc

def do_stuff(name, password, filepath):
    db = pypyodbc.connect(driver = "{MariaDB ODBC 1.0 Driver}",
                    server = "localhost", uid = name,
                    pwd = password, autocommit = True)

    cursor = db.cursor()

    cursor.execute("CREATE TABLE `foo`.`bar` (`col1` INT);")
    cursor.execute("LOAD DATA LOCAL INFILE '%s' INTO TABLE `foo`.`bar` (@row) SET col1 = SUBSTR(@row,1,1)" % filepath.replace("\\", "\\\\"))
    for row in cursor.execute("SELECT * FROM `foo`.`bar`"):
        print(row)
    db.close()

do_stuff("root", "password", r"C:\\Users\\laj\\Desktop\\test.txt")

它从文本文件中的每一行抓取第一个字符,并将其粘贴在表格的唯一列中。当" SELECT"声明到来,然而,我遇到以下错误:

Traceback (most recent call last):
  File "test.py", line 25, in <module>
    do_stuff("root", "oag123", r"C:\\Users\\laj\\Desktop\\test.txt")
  File "test.py", line 21, in do_stuff
    for row in cursor.execute("SELECT * FROM `foo`.`bar`"):
  File "C:\Python34\lib\site-packages\pypyodbc-1.3.3-py3.4.egg\pypyodbc.py", line 1605, in execute
  File "C:\Python34\lib\site-packages\pypyodbc-1.3.3-py3.4.egg\pypyodbc.py", line 1631, in execdirect
  File "C:\Python34\lib\site-packages\pypyodbc-1.3.3-py3.4.egg\pypyodbc.py", line 986, in check_success
  File "C:\Python34\lib\site-packages\pypyodbc-1.3.3-py3.4.egg\pypyodbc.py", line 964, in ctrl_err
pypyodbc.Error: ('HY000', '[HY000] Unknown prepared statement handler (5) given to mysqld_stmt_reset')

真正让我感到惊讶的是,我只需在填充表格和执行&#34; SELECT,&#34;之间关闭并重新打开数据库连接即可摆脱错误。像这样:

import pypyodbc

def do_stuff(name, password, filepath):
    db = pypyodbc.connect(driver = "{MariaDB ODBC 1.0 Driver}",
                    server = "localhost", uid = name,
                    pwd = password, autocommit = True)

    cursor = db.cursor()

    cursor.execute("CREATE TABLE `foo`.`bar` (`col1` INT);")
    cursor.execute("LOAD DATA LOCAL INFILE '%s' INTO TABLE `foo`.`bar` (@row) SET col1 = SUBSTR(@row,1,1)" % filepath.replace("\\", "\\\\"))

    db.close()
    db = pypyodbc.connect(driver = "{MariaDB ODBC 1.0 Driver}",
                    server = "localhost", uid = name,
                    pwd = password, autocommit = True)

    cursor = db.cursor()


    for row in cursor.execute("SELECT * FROM `foo`.`bar`"):
        print(row)
    db.close()

do_stuff("root", "password", r"C:\\Users\\laj\\Desktop\\test.txt")

不幸的是,这实际上并不是我问题的有效解决方案。这不仅是我不应该做的事情,而且在涉及临时表时也没有帮助,因为它们只是在断开连接期间被删除了#34;修复。&#34 ;任何见解都会很棒,这会让我陷入困境。

1 个答案:

答案 0 :(得分:0)

execute没有回复你的想法:

cursor.execute("SELECT ...");
rows = cur.fetchall();
for row in rows ...