Ebook123

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

python连接pgsql数据库操作

复制代码
 1 import psycopg2
 2 #连接数据库
 3 conn = psycopg2.connect(database='test',user='postg',
 4                  password='y0',host='10.13.108.5',port='5432')
 5 
 6 cursor = conn.cursor()   #创建一个游标
 7 
 8 #execuet方法是针对数据库的请求
 9 cursor.execute('create table test_1(id int,name varchar(20) )') 
10 cursor.execute("insert into test_1 values(1,'lg')")
11 cursor.execute("drop table test_1")
12 
13 conn.commit()  #提交
14 cursor.execute("select * from test_1")
15 rows = cursor.fetchall()   #返回所有行的元组
16 for row in rows:
17     print(row[0],row[1])
18 cursor.close()
19 conn.close()
复制代码

 

posted on   Ebook123  阅读(121)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示