2

私は 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 インジェクションによる攻撃に使用できます。

それで全部です。私の質問を読んでくれてありがとう、そして私の悪い英語でごめんなさい(私はスペイン語を話します)。

4

2 に答える 2

4
cursor.execute("""insert into tabla (informacion) VALUES (%s);""",(asdas,))

that is the solution

于 2010-05-25T21:38:58.693 に答える