[英]Problem with insert and psycopg2
我是Python和psycopg2的新手,並且在插入時遇到問題。
這是我的桌子:
CREATE TABLE tabla
(
codigo integer NOT NULL DEFAULT nextval('dato_codigo_seq'::regclass),
informacion character(30) NOT NULL,
CONSTRAINT dato_pkey PRIMARY KEY (codigo)
)
字段codigo
是一個序列。
當我做這句話時:
cursor.execute("INSERT INTO tabla informacion) VALUES (%s)",("abcdef"))
PostgreSQL引發異常。
我必須這樣做
cursor.execute("INSERT INTO tabla (codigo,informacion) VALUES (nextval(%s),%s)",
("dato_codigo_seq","abcdef"))
其中dato_codigo_seq
是字段codigo
的序列。
我的問題是我可以這樣說嗎
insert into tabla(informacion)values('asdsa')
並讓PostgreSQL處理串行字段的處理?
我可以做這個:
cursor.execute("INSERT INTO tabla informacion) VALUES ("+valor+")")"
但是該語句可用於通過SQL注入進行攻擊。
就這樣。 感謝您閱讀我的問題,也很抱歉我的英語不好(我說西班牙語)。
cursor.execute("""insert into tabla (informacion) VALUES (%s);""",(asdas,))
那是解決方案
在您的示例中:
cursor.execute("INSERT INTO tabla informacion) VALUES (%s)",("abcdef",))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.