gpt4 book ai didi

python - 如何解决此 Psycopg 错误消息(python)

转载 作者:行者123 更新时间:2023-11-29 13:13:44 28 4
gpt4 key购买 nike

<分区>

我最近修改了我的插入查询并开始收到此错误消息:

error: argument 1 must be a string or unicode object: got tuple instead

这是导致此错误的 SQL 插入字符串:

timestamp = 'NOW()::timestamp'

sql_insert_command =
"INSERT INTO test_table(
article_id,
date_published,
date_modified,
row_created_on,
row_modified_on )"
"VALUES(%s,%s,%s,%s,%s)",
(new_record[0],new_record[1],new_record[2],timestamp,timestamp)

这是传递给我的查询函数的 SQL 字符串:

('INSERT INTO test_table(
article_id,
date_published,
date_modified,
row_created_on,
row_modified_on)
VALUES(%s,%s,%s,%s,%s)',
('0530973',
'2018-01-10 17:29:00',
'2018-02-15 11:58:32',
'NOW()::timestamp',
'NOW()::timestamp'))

这是我的旧字符串,可以使用,但违反了这里的 NEVER do 语句:http://initd.org/psycopg/docs/usage.html#query-parameters

sql_insert_command = 
"INSERT INTO test_table(
article_id,
date_published,
date_modified,
row_created_on,
row_modified_on ) " \
"VALUES('" + new_record[0] + "','" +
new_record[1]+ "','" +
new_record[2] + "'," +
timestamp + "," +
timestamp + ")"

这是通过上面的命令传递给我的查询函数的 SQL 字符串:

INSERT INTO test_table(article_id, 
date_published,
date_modified,
row_created_on,
row_modified_on)
VALUES('0530973',
'2018-01-10 17:29:00',
'2018-02-15 11:58:32',
NOW()::timestamp,
NOW()::timestamp)

导致此错误消息的原因是什么?我该如何解决这个问题?

这是我用来连接到数据库并插入记录的代码:

class DatabaseConnection(object):

_instance = None

def __new__(cls):
if cls._instance is None:
cls._instance = object.__new__(cls)

db_config = {'dbname': 'development', 'host': 'localhost',
'password': 'somepassword', 'port': 5432, 'user': 'postgres'}
try:
print('connecting to PostgreSQL database...')
connection = DatabaseConnection._instance.connection = psycopg2.connect(**db_config)
connection.autocommit = True
cursor = DatabaseConnection._instance.cursor = connection.cursor()
cursor.execute('SELECT VERSION()')
db_version = cursor.fetchone()

except Exception as error:
print('Error: connection not established {}'.format(error))
DatabaseConnection._instance = None

else:
print('connection established\n{}'.format(db_version[0]))

return cls._instance

def __init__(self):
self.connection = self._instance.connection
self.cursor = self._instance.cursor

def insert_new_records(self, insert_query):
try:
# used for testing
print (insert_query)

result = self.cursor.execute(insert_query)

except Exception as error:
print('error execting query "{}", error: {}'.format(insert_query, error))
return None
else:
return result

def __del__(self):
self.connection.close()
self.cursor.close()

我用来插入记录的代码:

new_record = ("0530973", "2018-01-10 17:29:00", "2018-02-15 11:58:32",
"0530974", "2018-01-10 17:29:00", "2018-02-15 11:58:32")


timestamp = 'NOW()::timestamp'

sql_insert_command =
"INSERT INTO test_table(
article_id,
date_published,
date_modified,
row_created_on,
row_modified_on ) " \
"VALUES('" + new_record[0] + "','" +
new_record[1]+ "','" +
new_record[2] + "'," +
timestamp + "," +
timestamp + ")"

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com