我正在使用pg8000模块将python脚本连接到postgreSQL数据库,我想插入一些新行。当我运行这个脚本时,我没有遇到任何错误,它看起来运行良好,但是当我查看表格后,实际上没有添加任何内容。Python/PostgreSQL插入行没有效果
什么是困惑我的是,如果我打印出来的查询语句,我与pg8000光标对象执行,然后只是复制/粘贴,并在pgAdmin的界面执行它,它正确地插入一个新行。
另一个奇怪的事情是,表中有一个基于SERIAL的ID字段,每次插入行时都会自动更新。当我运行脚本时,该序列正在更新。我可以告诉B/C下一个要添加的ID,例如6000.如果我运行脚本(它应该添加大约1000行但不是),然后手动插入一行,新行的ID为7000.
这里的循环创建和执行的语句:
for row in new_signs:
query = "INSERT INTO {0}_signs (source_link, destination_link, exit_number) VALUES ({1},{2},'{3}');".format(city,row[0],row[1],row[2])
print query
cursor.execute(query)
而且一些示例输出:
INSERT INTO osm_newyork_signs (source_link, destination_link, exit_number) VALUES (56423,1833854,'26');
INSERT INTO osm_newyork_signs (source_link, destination_link, exit_number) VALUES (353212,310961,'45');
INSERT INTO osm_newyork_signs (source_link, destination_link, exit_number) VALUES (203823,1862344,'63N');
而且表定义:
CREATE TABLE public.osm_newyork_signs
(
source_link integer NOT NULL,
destination_link integer NOT NULL,
exit_number text,
"Branch_RouteID" character varying(64),
"Branch_RouteDir" character varying(6),
"Sign_TextType" character varying(6),
"Sign_Text" character varying(255),
"Toward_RouteID" character varying(64),
"Straight_On" character varying(255),
id integer NOT NULL DEFAULT nextval('osm_newyork_signs_id_seq'::regclass),
CONSTRAINT newyork_signs_pkey PRIMARY KEY (id),
CONSTRAINT check_branch_toward CHECK ("Sign_TextType"::text = 'B'::text OR "Sign_TextType"::text = 'T'::text OR "Sign_TextType" IS NULL)
)
WITH (
OIDS=FALSE
);
你有没有机会使用会话,忘记提交查询? –
不是我所知道的。我只是创建一个连接使用 conn = pg.connect(user ='',password ='',host ='',database ='') cursor = conn.cursor() – wmebane