python 操做PostgreSQL

pip install psycopg

Python psycopg2 模块APIs

如下是psycopg2的重要的的模块例程能够知足Python程序与PostgreSQL数据库的工做。python

 

 

 

S.N. API & 描述
1 psycopg2.connect(database="testdb", user="postgres", password="cohondob", host="127.0.0.1", port="5432") 

这个API打开一个链接到PostgreSQL数据库。若是成功打开数据库时,它返回一个链接对象。 www.yiibai.comsql

2 connection.cursor()

该程序建立一个光标将用于整个数据库使用Python编程。 yiibai.com数据库

3 cursor.execute(sql [, optional parameters])

此例程执行SQL语句。可被参数化的SQL语句(即占位符,而不是SQL文字)。 psycopg2的模块支持占位符用%s标志 yiibai.com编程

例如:cursor.execute("insert into people values (%s, %s)", (who, age))yii

 

4 curosr.executemany(sql, seq_of_parameters)

该程序执行SQL命令对全部参数序列或序列中的sql映射。 www.yiibai.compost

5 curosr.callproc(procname[, parameters])

这个程序执行的存储数据库程序给定的名称。该程序预计为每个参数,参数的顺序必须包含一个条目。fetch

 

6 cursor.rowcount

这个只读属性,它返回数据库中的行的总数已修改,插入或删除最后 execute*().spa

 

7 connection.commit()

此方法提交当前事务。若是不调用这个方法,不管作了什么修改,自从上次调用commit()是不可见的,从其余的数据库链接。code

 

8 connection.rollback()

此方法会回滚任何更改数据库自上次调用commit()方法。对象

 

9 connection.close()

此方法关闭数据库链接。请注意,这并不自动调用commit()。若是你只是关闭数据库链接而不调用commit()方法首先,那么全部更改将会丢失! www.yiibai.com

10 cursor.fetchone()

这种方法提取的查询结果集的下一行,返回一个序列,或者无当没有更多的数据是可用的。

 

11 cursor.fetchmany([size=cursor.arraysize])

这个例程中取出下一个组的查询结果的行数,返回一个列表。当没有找到记录,返回空列表。该方法试图获取尽量多的行所显示的大小参数。

 

12 cursor.fetchall()

这个例程获取全部查询结果(剩余)行,返回一个列表。空行时则返回空列表。 www.yiibai.com

 

链接到数据库

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" 

建立表

如下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"

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()

INSERT 操做

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()

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

UPDATE 操做

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()  

DELETE 操做

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() 
相关文章
相关标签/搜索