psycopg2.ProgrammingError:“st”处或附近出现语法错误\r,

2024-04-28 21:03:15 发布

您现在位置:Python中文网/ 问答频道 /正文

在这里,我需要在python的postgresql表中插入一些值。

我尝试了下面的代码,但出现了一个错误“psycopg2.ProgrammingError:syntax error at or near“st”\r,referer:http://localhost:8080/

conn = psycopg2.connect(database="Test", user="dev", password="123456", host="192.168.1.104", port="5432")
cursor = conn.cursor()

cursor.execute('''INSERT INTO signup (id, name, email, dob, address, mobile, password) VALUES (1,%s,%s,%s,%s,%s,%s)''' % (name,email,dob,address,mobile,password))
conn.commit()

请提前解决这个问题,谢谢。。。。。


Tags: 代码nameaddressemailpostgresql错误errorpassword
2条回答

在一个或多个参数中有回车符。因为您使用的是参数插值,所以这会打断查询字符串。但参数插值的更大问题是,这段代码容易受到SQL注入的攻击。

首先,请阅读: http://initd.org/psycopg/docs/usage.html#the-problem-with-the-query-parameters 那么这个: http://initd.org/psycopg/docs/usage.html#passing-parameters-to-sql-queries 然后,将代码重写为:

cursor.execute('''INSERT INTO signup (id, name, email, dob, address, mobile, password) VALUES (1,%s,%s,%s,%s,%s,%s)''',  (name,email,dob,address,mobile,password))

现在,如果您愿意,可以将“\r”传递到数据库,而且您也可以安全地避免SQL注入。

检查所有这些变量的值。其中一些或全部可能包含'\r'

编辑:如果错误显示“语法错误位于或靠近”st“\r”,则可能是address变量。

相关问题 更多 >