无法使用 py找不到文件将数据文件加载到 MySQL 中



我正试图将一个分隔文件加载到我的MariaDB数据库中,但我一直收到以下错误:

pymysql.err.OperationalError:(1017,"找不到文件'b'/tmp/elected.txt''"(

b从哪里来?

当我从MariaDB提示符下运行LOAD DATA命令时,它运行得很好。

这是我的代码:

print("Prepare to import file")
cnopts=pysftp.CnOpts()
cnopts.hostkeys = None
with pysftp.Connection("8.8.8.8", username="osuser", password="ospassword", cnopts=cnopts) as sftp:
with sftp.cd("/tmp"):
sftp.put("C:/mydirectory/altered.txt")
db = pymysql.connect("8.8.8.8", "dbuser", "dbpassword", "database", local_infile=True)
cursor = db.cursor()
print("importing file")
cursor.execute('LOAD DATA LOCAL INFILE "/tmp/altered.txt" INTO TABLE daily FIELDS TERMINATED BY "|" LINES STARTING BY "148521" TERMINATED BY "n" ')
db.commit()
cursor.close()
LOAD DATA LOCAL INFOILE用于客户端(远程(机器上的文件。

通过使用LOAD DATA LOCAL INFILE,不需要sFTP该文件。

print("Prepare to import file")
db = pymysql.connect("serverIP", "user", "password", "database", local_infile=True)
cursor = db.cursor()
print("importing file")
load_sql = "LOAD DATA LOCAL INFILE 'C:/directory/file.txt' INTO TABLE table FIELDS TERMINATED BY '|' LINES STARTING BY '11111' TERMINATED BY 'n';"
print("SQL Statement loaded")
cursor.execute(load_sql)
db.commit()
cursor.close()

相关内容

  • 没有找到相关文章

最新更新