无法使用 pymysql 将数据文件加载到 MySQL - 找不到文件
Unable to load data file into MySQL using pymysql - Can't find file
我正在尝试将带分隔符的文件加载到我的 MariaDB 数据库中,但我不断收到以下错误:
pymysql.err.OperationalError: (1017, "找不到文件 'b'/tmp/altered.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 INFILE 用于客户端(远程)计算机上的文件。
通过使用 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()
我正在尝试将带分隔符的文件加载到我的 MariaDB 数据库中,但我不断收到以下错误:
pymysql.err.OperationalError: (1017, "找不到文件 'b'/tmp/altered.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 INFILE 用于客户端(远程)计算机上的文件。
通过使用 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()