参数?不使用python postgres
我将参数?不使用python postgres,python,postgresql,parameters,Python,Postgresql,Parameters,我将python与postgres一起使用,我正在尝试这个简单的查询,但它不起作用,我无法找到原因 con = psycopg2.connect(**config) self.cursor.execute("INSERT INTO mytable (id, age, year) VALUES (nextval('my_id_seq'), ?, ?)", ('77', '44')) 我得到了这个错误 psycopg2.ProgrammingError:第1行处或附近的语法错误: …年)值(下
python
与postgres
一起使用,我正在尝试这个简单的查询,但它不起作用,我无法找到原因
con = psycopg2.connect(**config)
self.cursor.execute("INSERT INTO mytable (id, age, year) VALUES (nextval('my_id_seq'), ?, ?)", ('77', '44'))
我得到了这个错误
psycopg2.ProgrammingError:第1行处或附近的语法错误:
…年)值(下一个数值('my_id_seq'),?,?)
编辑
这就是错误所在
INSERT INTO mytable (id, age, year) VALUES (nextval('my_id_seq'), %s, %s)
'6.65', '4955/1'
File "off.py", line 80, in writeRows
self.cursor.execute(query, values)
TypeError: not all arguments converted during string formatting
代码:
psycopg2
使用pyformat
param样式:
>>> import psycopg2
>>> psycopg2.paramstyle
'pyformat'
将参数标记?
替换为%s
请参阅。我遇到如下语法错误
self.cursor.execute(“插入到mytable(id,年龄)值中”(nextval('my_id_seq'),%s“,'44'))TypeError:并非所有参数都在字符串格式化过程中转换
您能用%s
样式编写我的代码吗?这样我就知道还有什么需要修改的了chnage@user3113427,参数标记的数量和参数的长度应该匹配(参数应该是列表或元组):self.cursor.execute(“插入mytable(id、年龄、年份))值(nextval('my_id_seq'),%s,%s)”,('77','44'))
谢谢。如果我有值=('77','44')
和query=INSERT-INTO-mytable(id,age,year)值(nextval('my_id_seq'),%s)
我可以使用self.cursor.execute(查询,值)
还是那样wrong@user3113427,似乎没问题。顺便问一下,您是否引用了查询字符串?(query=“插入到mytable(id、年龄、年份)值(nextval('my_id_seq'),%s,%s)”
)如果您有任何错误,请向我显示错误。我已引用了查询。我已在问题中添加了错误,请您看一看,我尝试了self.cursor.execute(查询,元组(值))
,但没有成功
>>> import psycopg2
>>> psycopg2.paramstyle
'pyformat'