本文实例讲述了Python 操作 PostgreSQL 数据库。分享给大家供大家参考,具体如下:
我使用的是 Python 3.7.0
PostgreSQL可以使用psycopg2模块与Python集成。
sycopg2是用于Python编程语言的PostgreSQL数据库适配器。
psycopg2是非常小,快速,稳定的。 您不需要单独安装此模块,因为默认情况下它会随着Python 2.5.x版本一起发布。
pip3 install python-psycopg2 pip3 install psycopg2-binary
连接到数据库
以下Python代码显示了如何连接到现有的数据库。 如果数据库不存在,那么它将自动创建,最后将返回一个数据库对象。
#!/usr/bin/python import psycopg2 conn = psycopg2.connect(database="testdb", user="postgres", password="pass123", host="127.0.0.1", port="5432") print("Opened database successfully")
在这里指定使用testdb作为数据库名称,如果数据库已成功打开连接,则会提供以下消息:
Open database successfully
创建表
以下Python程序将用于在先前创建的数据库(testdb)中创建一个表:
#!/usr/bin/python import psycopg2 conn = psycopg2.connect(database="testdb", user="postgres", password="pass123", host="127.0.0.1", port="5432") print("Opened database successfully") cur = conn.cursor() cur.execute('''CREATE TABLE COMPANY (ID INT PRIMARY KEY NOT NULL, NAME TEXT NOT NULL, AGE INT NOT NULL, ADDRESS CHAR(50), SALARY REAL);''') print "Table created successfully" conn.commit() conn.close()
当执行上述程序时,它将在数据库testdb中创建COMPANY表,并显示以下消息:
Opened database successfully
Table created successfully
插入操作
以下Python程序显示了如何在上述示例中创建的COMPANY表中创建记录:
#!/usr/bin/python import psycopg2 conn = psycopg2.connect(database="testdb", user="postgres", password="pass123", host="127.0.0.1", port="5432") print("Opened database successfully") cur = conn.cursor() cur.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (1, 'Paul', 32, 'California', 20000.00 )"); cur.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (2, 'Allen', 25, 'Texas', 15000.00 )"); cur.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (3, 'Teddy', 23, 'Norway', 20000.00 )"); cur.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 )"); conn.commit() print("Records created successfully"); conn.close()
当执行上述程序时,它将在COMPANY表中创建/插入给定的记录,并显示以下两行:
Opened database successfully
Records created successfully
SELECT操作
以下 Python 程序显示了如何从上述示例中创建的 COMPANY 表中获取和显示记录:
#!/usr/bin/python import psycopg2 conn = psycopg2.connect(database="testdb", user="postgres", password="pass123", host="127.0.0.1", port="5432") print("Opened database successfully") cur = conn.cursor() cur.execute("SELECT id, name, address, salary from COMPANY") rows = cur.fetchall() for row in rows: print("ID = ", row[0]) print("NAME = ", row[1]) print("ADDRESS = ", row[2]) print("SALARY = ", row[3], "\n") print("Operation done successfully"); conn.close()
执行上述程序时,会产生以下结果:
Opened database successfully
ID = 1
NAME = Paul
ADDRESS = California
SALARY = 20000.0ID = 2
NAME = Allen
ADDRESS = Texas
SALARY = 15000.0ID = 3
NAME = Teddy
ADDRESS = Norway
SALARY = 20000.0ID = 4
NAME = Mark
ADDRESS = Rich-Mond
SALARY = 65000.0Operation done successfully
更新操作
以下 Python 代码显示了如何使用UPDATE语句来更新任何记录,然后从COMPANY表中获取并显示更新的记录:
#!/usr/bin/python import psycopg2 conn = psycopg2.connect(database="testdb", user="postgres", password="pass123", host="127.0.0.1", port="5432") print("Opened database successfully") cur = conn.cursor() cur.execute("UPDATE COMPANY set SALARY = 25000.00 where ID=1") conn.commit print("Total number of rows updated :", cur.rowcount) cur.execute("SELECT id, name, address, salary from COMPANY") rows = cur.fetchall() for row in rows: print("ID = ", row[0]) print("NAME = ", row[1]) print("ADDRESS = ", row[2]) print("SALARY = ", row[3], "\n") print("Operation done successfully"); conn.close()
Python
执行上述程序时,会产生以下结果:
Opened database successfully
Total number of rows updated : 1
ID = 1
NAME = Paul
ADDRESS = California
SALARY = 25000.0ID = 2
NAME = Allen
ADDRESS = Texas
SALARY = 15000.0ID = 3
NAME = Teddy
ADDRESS = Norway
SALARY = 20000.0ID = 4
NAME = Mark
ADDRESS = Rich-Mond
SALARY = 65000.0Operation done successfully
删除操作
以下 Python 代码显示了如何使用 DELETE 语句来删除记录,然后从 COMPANY 表中获取并显示剩余的记录:
#!/usr/bin/python import psycopg2 conn = psycopg2.connect(database="testdb", user="postgres", password="pass123", host="127.0.0.1", port="5432") print("Opened database successfully") cur = conn.cursor() cur.execute("DELETE from COMPANY where ID=2;") conn.commit print("Total number of rows deleted :", cur.rowcount) cur.execute("SELECT id, name, address, salary from COMPANY") rows = cur.fetchall() for row in rows: print("ID = ", row[0]) print("NAME = ", row[1]) print("ADDRESS = ", row[2]) print("SALARY = ", row[3], "\n") print("Operation done successfully"); conn.close()
执行上述程序时,会产生以下结果:
Opened database successfully
Total number of rows deleted : 1
ID = 1
NAME = Paul
ADDRESS = California
SALARY = 20000.0ID = 3
NAME = Teddy
ADDRESS = Norway
SALARY = 20000.0ID = 4
NAME = Mark
ADDRESS = Rich-Mond
SALARY = 65000.0Operation done successfully
更多关于Python相关内容感兴趣的读者可查看本站专题:《Python常见数据库操作技巧汇总》、《Python数学运算技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》
希望本文所述对大家Python程序设计有所帮助。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
- 余天.2004-二十年来最畅销国语金曲系列2CD【丽歌】【WAV+CUE】
- 完美倒立.2024-夜色碳酸【草台回声】【FLAC分轨】
- 杨青《半山听雨贰HQ》头版限量编号[低速原抓WAV+CUE]
- 白玛多吉《云上西藏1+2》DTS-WAV
- 模拟之声慢刻CD《柏林之声3》[正版CD原抓WAV+CUE]
- 威神V(WayV)《The Highest》[FLAC/分轨][259.1MB]
- 余超颖《迷焕纪》[320K/MP3][201.15MB]
- 余超颖《迷焕纪》[FLAC/分轨][784.22MB]
- 温岚.2005-爱回温新歌加精选2CD【阿尔发】【WAV+CUE】
- 尤雅.1990-台语怀念金曲特选辑【太阳神】【WAV+CUE】
- 群星.2024-七夜雪电视剧影视原声带【听见时代】【FLAC分轨】
- 群星《胎教音乐 古典钢琴曲与水晶摇篮曲》[320K/MP3][134.14MB]
- 群星《胎教音乐 古典钢琴曲与水晶摇篮曲》[FLAC/分轨][654.13MB]
- 许惠钧《睡眠钢琴与校园民歌金曲 空灵新世纪催眠曲》[320K/MP3][201.15MB]
- wbg战队国籍分别都是哪里的 wbg战队2024阵容国籍介绍