pETL 无法将数据加载到 MySQL 数据库
pETL fails to load data to MySQL DB
我正在使用 petl package 构建从 Python 2.7 到 MySQL5.6
的 ETL 管道
我的数据库连接器是 MySQLdb (mysql-python)。
以下代码执行失败:
import MySQLdb as mdb
import petl as etl
con = mdb.connect(host = '127.0.0.1', user = '<someuser>', passwd = '<somepass>')
cur = con.cursor() # get the cursor
cur.execute('DROP SCHEMA IF EXISTS petltest')
cur.execute('CREATE SCHEMA petltest')
cur.execute('USE petltest')
dat = [{'id':1,'name':'One'},
{'id':2,'name':'Two'},
{'id':3,'name':'Three'}]
table = etl.fromdicts(dat) # petl object
etl.todb(table,cur,'table',schema='petltest',create=True)
错误代码为:
ProgrammingError: (1064, '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 \'"table" (\n\tid INTEGER NOT NULL, \n\tname
VARCHAR(5) NOT NULL\n)\' at line 1')
尝试单独创建 table 或 运行 petl.appenddb
时也会出现此错误
我该如何解决/克服这个问题?
谢谢
显然问题出在 PETL 使用的引号样式上。
如果你 运行:
cur.execute('SET SQL_MODE=ANSI_QUOTES')
在 petl sql (petl.todb()
) 语句之前执行得很好。
我正在使用 petl package 构建从 Python 2.7 到 MySQL5.6
的 ETL 管道我的数据库连接器是 MySQLdb (mysql-python)。
以下代码执行失败:
import MySQLdb as mdb
import petl as etl
con = mdb.connect(host = '127.0.0.1', user = '<someuser>', passwd = '<somepass>')
cur = con.cursor() # get the cursor
cur.execute('DROP SCHEMA IF EXISTS petltest')
cur.execute('CREATE SCHEMA petltest')
cur.execute('USE petltest')
dat = [{'id':1,'name':'One'},
{'id':2,'name':'Two'},
{'id':3,'name':'Three'}]
table = etl.fromdicts(dat) # petl object
etl.todb(table,cur,'table',schema='petltest',create=True)
错误代码为:
ProgrammingError: (1064, '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 \'"table" (\n\tid INTEGER NOT NULL, \n\tname VARCHAR(5) NOT NULL\n)\' at line 1')
尝试单独创建 table 或 运行 petl.appenddb
时也会出现此错误我该如何解决/克服这个问题?
谢谢
显然问题出在 PETL 使用的引号样式上。 如果你 运行:
cur.execute('SET SQL_MODE=ANSI_QUOTES')
在 petl sql (petl.todb()
) 语句之前执行得很好。