python鏈結postgreSQL的出現的小問題

2021-08-18 19:25:19 字數 3241 閱讀 9728

1.cur.execute之後,一定要connect.commit()才會在資料庫中有相應結果。

import psycopg2 as p

con=p.connect(database='postgres',user='postgres',password='135246',host='127.0.0.1',port="5432")

# con=p.connect(database='postgres',user='postgres',password='135246',host='127.0.0.1',port='5432')

cur=con.cursor()

cur.execute('''

create table reader(

rno text primary key,

rname text not null,

r*** text,

rage int,

rboss text,

raddress text

);create table book(

bno text primary key,

bname text not null,

bauthor text,

bpress text,

bprice numeric

);create table rb(

rno text,

bno text,

rbdate date,

foreign key(rno) references reader(rno),

foreign key(bno) references book(bno)

);insert into reader (rno,rname,rage,r***,rboss, raddress) values('r001','張三',20,'男','李四','416');

insert into reader (rno,rname,rage,r***,rboss, raddress) values('r002','張三',35,'女',null,'417');

insert into reader (rno,rname,rage,r***,rboss, raddress) values('r003','李四',30,'男',null,'416');

insert into reader (rno,rname,rage,r***,rboss, raddress) values('r004','王五',20,'男',null,'417');

insert into reader (rno,rname,rage,r***,rboss, raddress) values('r005','馬六',40,'男',null,'416');

insert into reader (rno,rname,rage,r***,rboss, raddress) values('r006','劉三',20,'男',null,'417');

insert into reader (rno,rname,rage,r***,rboss, raddress) values('r007','王四',40,'男','李四','416');

insert into reader (rno,rname,rage,r***,rboss, raddress) values('r008','李小龍',20,'男','李四','417');

insert into reader (rno,rname,rage,r***,rboss, raddress) values('r009','王小倩',40,'男','李四','416');

insert into reader (rno,rname,rage,r***,rboss, raddress) values('r010','王一小',20,'男','李四','417');

insert into book (bno,bauthor,bname, bpress, bprice) values('b001','嚴蔚敏','資料結構','清華大學出版社',null);

insert into book (bno,bauthor,bname, bpress, bprice) values('b002','唐髮根','資料結構','北航出版社',24);

insert into book (bno,bauthor,bname, bpress, bprice) values('b003','王珊','資料庫原理','高等教育出版社',40);

insert into book (bno,bauthor,bname, bpress, bprice) values('b004','張飛','資料庫原理','清華大學出版社',30);

insert into book (bno,bauthor,bname, bpress, bprice) values('b005','王珊','資料庫原理','清華大學出版社',null);

insert into rb (rno,bno) values ('r001','b001');

insert into rb (rno,bno) values ('r001','b002');

insert into rb (rno,bno) values ('r001','b004');

insert into rb (rno,bno) values ('r002','b001');

insert into rb (rno,bno) values ('r003','b001');

insert into rb (rno,bno) values ('r004','b001');

insert into rb (rno,bno) values ('r004','b002');

insert into rb (rno,bno) values ('r005','b001');

insert into rb (rno,bno) values ('r006','b001');

insert into rb (rno,bno) values ('r006','b003');

insert into rb (rno,bno) values ('r006','b005');

insert into rb (rno,bno) values ('r006','b002');

insert into rb (rno,bno) values ('r006','b004');

''')

#這乙個是必須有的!!!

con.commit()#這個是必須有的!!!

con.close()

python連線postgre 資料庫

1 安裝 psycopg2 庫 pip install psycopg2 2 使用 導入庫 import psycopg2 import psycopg2.extras 需要返回結果是字典時使用此庫 建立連線 conn psycopg2.connect database basename,user ...

Linux Docker構建postgre映象

起初為什麼要構建乙個新的資料庫映象,是因為我們好多排序是漢字的問題,所以牽扯到編碼的問題 1 首先建立乙個資料夾 mkdir postgis 2 建立檔案dockerfile和build.sh 內容如下 dockerfile from mdillon postgis 11 run localedef...

Python大規模資料插入Postgresql

需要owner許可權 alter table t rs standard satellite image add constraint unique source product id unique source product id batchinsert批量插入 def batchinsert ...