使用 Psycopg2 将 JPEG 文件名插入 PostgreSQL table 导致 "not all arguments converted during string formatting" 错误
Inserting JPEG-filenames into PostgreSQL table using Psycopg2 causes "not all arguments converted during string formatting" error
我正在尝试用我在特定文件夹中的文件名填充 PostgreSQL table (psycopg2, Python)。我创建了一个应该可以解决问题的函数,但我收到错误:
并非所有参数在字符串格式化期间都已转换,
当我 运行 我的功能。我做了一个测试 运行 并按以下方式调用函数:
insert_file_names_into_database(["filename1_without_extension", "filename2_without_extension"]),
我没有遇到任何问题,INSERT 工作正常。如果我执行以下操作:
insert_file_names_into_database(["filename1.extension", "filename2.extension"]),
然后我得到上面的错误。所以问题似乎是“。”导致 SQL INSERT 失败的字符(例如 image.jpg)。我试图就此咨询 Psycopg2 文档,但没有找到与此特定案例相关的示例。
我应该如何编辑这段代码,以便即使使用“.”也能开始工作。文件名中的字符?
def insert_file_names_into_database(file_name_list):
""" insert multiple filenames into the table """
sql = "INSERT INTO mytable(filename) VALUES(%s)"
conn = None
try:
# read database configuration
# connect to the PostgreSQL database
conn = psycopg2.connect(
host="localhost",
database="mydatabase",
user="myusername",
password="mypassword")
# create a new cursor
cur = conn.cursor()
# execute the INSERT statement
cur.executemany(sql, file_name_list)
# commit the changes to the database
conn.commit()
# close communication with the database
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
自己已经解决了。我知道在使用 INSERT 时我应该使用元组,但是我的函数可以很好地处理没有“.”的字符串列表。字符。
我得到的解决方案是将字符串列表转换为元组列表,如下所示:
tuple_file_name = [tuple((file_name,)) for file_name in file_name_list]
例如如果:
file_name_list = ["filename1.jpg", "filename2.jpg"]
然后将此作为我的函数的输入失败。但是通过将其设为元组列表:
tuple_file_name = [tuple((file_name,)) for file_name in file_name_list]
print(tuple_file_name)
[('filename1.jpg',), ('filename2.jpg',)]
然后函数接受输入 tuple_file_name
并将文件名保存到 SQL table.
我正在尝试用我在特定文件夹中的文件名填充 PostgreSQL table (psycopg2, Python)。我创建了一个应该可以解决问题的函数,但我收到错误:
并非所有参数在字符串格式化期间都已转换,
当我 运行 我的功能。我做了一个测试 运行 并按以下方式调用函数:
insert_file_names_into_database(["filename1_without_extension", "filename2_without_extension"]),
我没有遇到任何问题,INSERT 工作正常。如果我执行以下操作:
insert_file_names_into_database(["filename1.extension", "filename2.extension"]),
然后我得到上面的错误。所以问题似乎是“。”导致 SQL INSERT 失败的字符(例如 image.jpg)。我试图就此咨询 Psycopg2 文档,但没有找到与此特定案例相关的示例。
我应该如何编辑这段代码,以便即使使用“.”也能开始工作。文件名中的字符?
def insert_file_names_into_database(file_name_list):
""" insert multiple filenames into the table """
sql = "INSERT INTO mytable(filename) VALUES(%s)"
conn = None
try:
# read database configuration
# connect to the PostgreSQL database
conn = psycopg2.connect(
host="localhost",
database="mydatabase",
user="myusername",
password="mypassword")
# create a new cursor
cur = conn.cursor()
# execute the INSERT statement
cur.executemany(sql, file_name_list)
# commit the changes to the database
conn.commit()
# close communication with the database
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
自己已经解决了。我知道在使用 INSERT 时我应该使用元组,但是我的函数可以很好地处理没有“.”的字符串列表。字符。
我得到的解决方案是将字符串列表转换为元组列表,如下所示:
tuple_file_name = [tuple((file_name,)) for file_name in file_name_list]
例如如果:
file_name_list = ["filename1.jpg", "filename2.jpg"]
然后将此作为我的函数的输入失败。但是通过将其设为元组列表:
tuple_file_name = [tuple((file_name,)) for file_name in file_name_list]
print(tuple_file_name)
[('filename1.jpg',), ('filename2.jpg',)]
然后函数接受输入 tuple_file_name
并将文件名保存到 SQL table.