python链接postgreSQL的出现的小问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Yonggie/article/details/79947944

整理一下我python链接postgresql的小问题。

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,
        rsex     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,Rsex,Rboss, Raddress) values('R001','张三',20,'男','李四','416');
    insert into Reader (RNO,Rname,Rage,Rsex,Rboss, Raddress) values('R002','张三',35,'女',null,'417');
    insert into Reader (RNO,Rname,Rage,Rsex,Rboss, Raddress) values('R003','李四',30,'男',null,'416');
    insert into Reader (RNO,Rname,Rage,Rsex,Rboss, Raddress) values('R004','王五',20,'男',null,'417');
    insert into Reader (RNO,Rname,Rage,Rsex,Rboss, Raddress) values('R005','马六',40,'男',null,'416');
    insert into Reader (RNO,Rname,Rage,Rsex,Rboss, Raddress) values('R006','刘三',20,'男',null,'417');
    insert into Reader (RNO,Rname,Rage,Rsex,Rboss, Raddress) values('R007','王四',40,'男','李四','416');
    insert into Reader (RNO,Rname,Rage,Rsex,Rboss, Raddress) values('R008','李小龙',20,'男','李四','417');
    insert into Reader (RNO,Rname,Rage,Rsex,Rboss, Raddress) values('R009','王小倩',40,'男','李四','416');
    insert into Reader (RNO,Rname,Rage,Rsex,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()



阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页