Postgresql 专题
专题目录
您的位置:database > Postgresql专题 > Python连接PostgreSQL数据库
Python连接PostgreSQL数据库
作者:--    发布时间:2019-11-20

postgresql可以使用psycopg2模块与python集成。sycopg2是用于python编程语言的postgresql数据库适配器。 psycopg2是非常小,快速,稳定的。 您不需要单独安装此模块,因为默认情况下它会随着python 2.5.x版本一起发布。 如果还没有在您的机器上安装它,那么可以使用yum命令安装它,如下所示:

$yum install python-psycopg2

要使用psycopg2模块,必须首先创建一个表示数据库的connection对象,然后可以选择创建可以帮助您执行所有sql语句的游标对象。

连接到数据库

以下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.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 = 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()

执行上述程序时,会产生以下结果:

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 = 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.0

id =  3
name =  teddy
address =  norway
salary =  20000.0

id =  4
name =  mark
address =  rich-mond
salary =  65000.0

operation done successfully

网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册