def registerPlayer(name):
code for connecting to db and cursor
c.execute("INSERT INTO players(player_name) VALUES({name});".format(name=name))
code for committing to db and closing the connection
这是我的表架构:
CREATE TABLE players(
player_id serial PRIMARY KEY,
player_name varchar(50) NOT NULL
);
下面是错误:
psycopg2.ProgrammingError: syntax error at or near "Nalaar" LINE 1:
INSERT INTO玩家(player_name)VALUES(Chandra Nalaar);
答案 0 :(得分:1)
您永远不应该使用字符串格式将值放入sql查询中。相反,您应该使用%s
并在vars
参数中传递名称。这样做的原因是因为它可以帮助您将参数转换为适当的数据类型。
顺便说一句,在;
调用时,sql字符串末尾有cursor.execute
是多余的,因为它会自动为你完成。
c.execute("INSERT INTO players(player_name) VALUES(%(name)s)", {"name":name})
有关详细信息,请参阅此页: