我正在尝试使用LOAD DATA LOCAL INFILE,但我继续得到:
sqlalchemy.exc.ProgrammingError: (pymysql.err.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 '' at line 1") [SQL: "LOAD DATA LOCAL INFILE 'file.csv' "]
SQL代码在Workbench中工作(它是我在VBA中使用的预先存在的代码)。另请注意,我在引擎创建中指定了local_infile = 1。
以下是代码:
import pymysql as mysql
import sqlalchemy
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
def main():
engine=create_engine('mysql+pymysql://user:pwd@localhost:3306/db?charset=utf8&local_infile=1')
SessionMaker = sessionmaker(bind=engine)
session = SessionMaker()
sql = "LOAD DATA LOCAL INFILE '/home/file.csv' "
"INTO TABLE settles "
"FIELDS TERMINATED BY ',' "
"lines terminated by '\n' "
"IGNORE 1 LINES "
"(product_symbol, contract_month, contract_year, contract_day, contract, "
"product_desc, open_price, high_price, high_ab_ind, low_price, low_ab_ind, "
"last_price, last_ab_ind, settle_price, "
"pt_chg, est_volume, prior_settle_price, prior_volume, prior_int, @var1) "
"set trade_date = str_to_date(@var1, '%m/%d/%Y');"
session.execute(sql)
session.flush()
engine.dispose()
if __name__ == "__main__":
main()
这是在Ubuntu 16.04上
请帮忙。
答案 0 :(得分:2)
此处的问题是使用text
sql = text("""
LOAD DATA LOCAL INFILE '/home/file.csv'
INTO TABLE settles
FIELDS TERMINATED BY ','
lines terminated by '\n'
IGNORE 1 LINES
(product_symbol, contract_month, contract_year, contract_day, contract,
product_desc, open_price, high_price, high_ab_ind, low_price, low_ab_ind,
last_price, last_ab_ind, settle_price,
pt_chg, est_volume, prior_settle_price, prior_volume, prior_int, @var1)
set trade_date = str_to_date(@var1, '%m/%d/%Y')
""")
在这种情况下很有用。
建议您阅读SQLAlchemy
部分{em> SQL表达式语言教程。
此Using Textual SQL也可以找到text
的引用。
答案 1 :(得分:1)
您必须使用"""
确保您的长SQL查询正确无误:
sql = """
LOAD DATA LOCAL INFILE '/home/file.csv'
INTO TABLE settles
FIELDS TERMINATED BY ','
lines terminated by '\n'
IGNORE 1 LINES
(product_symbol, contract_month, contract_year, contract_day, contract,
product_desc, open_price, high_price, high_ab_ind, low_price, low_ab_ind,
last_price, last_ab_ind, settle_price,
pt_chg, est_volume, prior_settle_price, prior_volume, prior_int, @var1)
set trade_date = str_to_date(@var1, '%m/%d/%Y')
"""