Python操作PostgreSQL数据库⽰例【连接、增删改查
等】
本⽂实例讲述了Python 操作 PostgreSQL 数据库。分享给⼤家供⼤家参考,具体如下:
我使⽤的是 Python 3.7.0
PostgreSQL可以使⽤psycopg2模块与Python集成。
sycopg2是⽤于Python编程语⾔的PostgreSQL数据库适配器。
不要用strcat函数psycopg2是⾮常⼩,快速,稳定的。您不需要单独安装此模块,因为默认情况下它会随着Python 2.5.x版本⼀起发布。pip3 install python-psycopg2
pip3 install psycopg2-binary
连接到数据库
pycharm python安装教程以下Python代码显⽰了如何连接到现有的数据库。如果数据库不存在,那么它将⾃动创建,最后将返回⼀个数据库对象。#!/usr/bin/python
import psycopg2
conn = t(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 = t(database="testdb", user="postgres", password="pass123", host="127.0.0.1", port="5432")
print("Opened database successfully")
cur = conn.cursor()
(ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY REAL);''')
print "Table created successfully"
connmit()
conn.close()
当执⾏上述程序时,它将在数据库testdb中创建COMPANY表,并显⽰以下消息:
Opened database successfully
Table created successfully
插⼊操作
以下Python程序显⽰了如何在上述⽰例中创建的COMPANY表中创建记录:
#!/usr/bin/python
import psycopg2
conn = t(database="testdb", user="postgres", password="pass123", host="127.0.0.1", port="5432") print("Opened database successfully")
cur = conn.cursor()
VALUES (1, 'Paul', 32, 'California', 20000.00 )");
VALUES (2, 'Allen', 25, 'Texas', 15000.00 )");
哈夫曼树的构造的课程设计目录VALUES (3, 'Teddy', 23, 'Norway', 20000.00 )");
VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 )");
connmit()
print("Records created successfully");
conn.close()
当执⾏上述程序时,它将在COMPANY表中创建/插⼊给定的记录,并显⽰以下两⾏:Opened database successfully
Records created successfully
SELECT操作
以下 Python 程序显⽰了如何从上述⽰例中创建的 COMPANY 表中获取和显⽰记录:
#!/usr/bin/python
import psycopg2
conn = t(database="testdb", user="postgres", password="pass123", host="127.0.0.1", port="5432") print("Opened database successfully")
cur = conn.cursor()
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.0
ID = 2
NAME = Allen
ADDRESS = Texas
SALARY = 15000.0
ID = 3
NAME = Teddy
ADDRESS = Norway
SALARY = 20000.0
ID = 4
NAME = Mark
ADDRESS = Rich-Mond
SALARY = 65000.0
Operation done successfully
更新操作
以下 Python 代码显⽰了如何使⽤UPDATE语句来更新任何记录,然后从COMPANY表中获取并显⽰更新的记录:#!/usr/bin/python
import psycopg2
conn = t(database="testdb", user="postgres", password="pass123", host="127.0.0.1", port="5432")
print("Opened database successfully")
cur = conn.cursor()
connmit
print("Total number of rows updated :", wcount)
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");
theater怎么读音发音英语conn.close()
Python
执⾏上述程序时,会产⽣以下结果:
Opened database successfully
Total number of rows updated : 1
ID = 1
NAME = Paul
ADDRESS = California
SALARY = 25000.0
ID = 2
NAME = Allen
ADDRESS = Texas
SALARY = 15000.0
ID = 3
NAME = Teddy
ADDRESS = Norway
SALARY = 20000.0
ID = 4
NAME = Mark
ADDRESS = Rich-Mond
SALARY = 65000.0
Operation done successfully
删除操作
以下 Python 代码显⽰了如何使⽤ DELETE 语句来删除记录,然后从 COMPANY 表中获取并显⽰剩余的记录:
#!/usr/bin/python
import psycopg2
conn = t(database="testdb", user="postgres", password="pass123", host="127.0.0.1", port="5432")
print("Opened database successfully")
cur = conn.cursor()
connmit
二进制转换十进制的题目print("Total number of rows deleted :", wcount)
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.0
ID = 3
NAME = Teddy
ADDRESS = Norway
SALARY = 20000.0
ID = 4
NAME = Mark
ADDRESS = Rich-Mond
SALARY = 65000.0
Operation done successfully
更多关于Python相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》及《》希望本⽂所述对⼤家Python程序设计有所帮助。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论