DBMNG数据库管理与应用

才能是来自独创性。独创性是思维、观察、理解和判断的一种独特的方式。
当前位置:首页 > PostgreSQL > 驱动及连接

PostgreSQL连接Python

安装

PostgreSQL可以用Python psycopg2模块集成。 sycopg2是Python编程语言的PostgreSQL数据库的适配器。 其程序代码少,速度快,稳定。不需要单独安装这个模块,因为它默认情况下被运往随着Python版本在2.5.x一起的。如果不把它安装在机器上,然后可 以使用yum命令安装它,如下所示:

  1. $yum install python-psycopg2 

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

Python psycopg2 模块APIs

以下是psycopg2的重要的的模块例程可以满足Python程序与PostgreSQL数据库的工作。如果寻找一个更复杂的应用程序,那么可以看看Python psycopg2的模块的官方文档。

 

连接到数据库

Python代码显示了如何连接到一个现有的数据库。如果数据库不存在,那么它就会被创建,最终将返回一个数据库对象。

  1. #!/usr/bin/python 
  2.  
  3. import psycopg2 
  4.  
  5. conn = psycopg2.connect(database="testdb", user="postgres", password="pass123", host="127.0.0.1", port="5432") 
  6.  
  7. print "Opened database successfully" 


在这里,也可以提供数据库testdb的名称,如果数据库成功打开,那么它会给下面的消息:

  1. Open database successfully 


创建表

以下Python程序将使用以前创建的数据库中创建一个表:

  1. #!/usr/bin/python 
  2.  
  3. import psycopg2 
  4.  
  5. conn = psycopg2.connect(database="testdb", user="postgres", password="pass123", host="127.0.0.1", port="5432") 
  6. print "Opened database successfully" 
  7.  
  8. cur = conn.cursor() 
  9. cur.execute('''CREATE TABLE COMPANY 
  10.        (ID INT PRIMARY KEY     NOT NULL, 
  11.        NAME           TEXT    NOT NULL, 
  12.        AGE            INT     NOT NULL, 
  13.        ADDRESS        CHAR(50), 
  14.        SALARY         REAL);''') 
  15. print "Table created successfully" 
  16.  
  17. conn.commit() 
  18. conn.close() 

上述程序执行时,它会创建表COMPANY 在数据库test.db中,它会显示以下消息:

  1. Opened database successfully 
  2. Table created successfully 


INSERT 操作

Python程序显示了我们如何创建表COMPANY 在上面的例子中创建表中的记录:

  1. #!/usr/bin/python 
  2.  
  3. import psycopg2 
  4.  
  5. conn = psycopg2.connect(database="testdb", user="postgres", password="pass123", host="127.0.0.1", port="5432") 
  6. print "Opened database successfully" 
  7.  
  8. cur = conn.cursor() 
  9.  
  10. cur.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \ 
  11.       VALUES (1, 'Paul', 32, 'California', 20000.00 )"); 
  12.  
  13. cur.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \ 
  14.       VALUES (2, 'Allen', 25, 'Texas', 15000.00 )"); 
  15.  
  16. cur.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \ 
  17.       VALUES (3, 'Teddy', 23, 'Norway', 20000.00 )"); 
  18.  
  19. cur.execute("INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) \ 
  20.       VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 )"); 
  21.  
  22. conn.commit() 
  23. print "Records created successfully"; 
  24. conn.close() 

上述程序执行时,它会创建COMPANY表中的记录,并显示以下两行:

  1. Opened database successfully 
  2. Records created successfully 

SELECT 操作

Python程序,显示如何获取并显示COMPANY 表在上面的例子中创建的记录:

  1. #!/usr/bin/python 
  2.  
  3. import psycopg2 
  4.  
  5. conn = psycopg2.connect(database="testdb", user="postgres", password="pass123", host="127.0.0.1", port="5432") 
  6. print "Opened database successfully" 
  7.  
  8. cur = conn.cursor() 
  9.  
  10. cur.execute("SELECT id, name, address, salary  from COMPANY") 
  11. rows = cur.fetchall() 
  12. for row in rows: 
  13.    print "ID = ", row[0] 
  14.    print "NAME = ", row[1] 
  15.    print "ADDRESS = ", row[2] 
  16.    print "SALARY = ", row[3], "\n" 
  17.  
  18. print "Operation done successfully"; 
  19. conn.close() 

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

  1. Opened database successfully 
  2. ID =  1 
  3. NAME =  Paul 
  4. ADDRESS =  California 
  5. SALARY =  20000.0 
  6.  
  7. ID =  2 
  8. NAME =  Allen 
  9. ADDRESS =  Texas 
  10. SALARY =  15000.0 
  11.  
  12. ID =  3 
  13. NAME =  Teddy 
  14. ADDRESS =  Norway 
  15. SALARY =  20000.0 
  16.  
  17. ID =  4 
  18. NAME =  Mark 
  19. ADDRESS =  Rich-Mond 
  20. SALARY =  65000.0 
  21.  
  22. Operation done successfully 

UPDATE 操作

Python代码显示如何,我们可以使用UPDATE语句来更新记录,然后从COMPANY表获取并显示更新的记录:

  1. #!/usr/bin/python 
  2.  
  3. import psycopg2 
  4.  
  5. conn = psycopg2.connect(database="testdb", user="postgres", password="pass123", host="127.0.0.1", port="5432") 
  6. print "Opened database successfully" 
  7.  
  8. cur = conn.cursor() 
  9.  
  10. cur.execute("UPDATE COMPANY set SALARY = 25000.00 where ID=1") 
  11. conn.commit 
  12. print "Total number of rows updated :", cur.rowcount 
  13.  
  14. cur.execute("SELECT id, name, address, salary  from COMPANY") 
  15. rows = cur.fetchall() 
  16. for row in rows: 
  17.    print "ID = ", row[0] 
  18.    print "NAME = ", row[1] 
  19.    print "ADDRESS = ", row[2] 
  20.    print "SALARY = ", row[3], "\n" 
  21.  
  22. print "Operation done successfully"; 
  23. conn.close() 

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

  1. Opened database successfully 
  2. Total number of rows updated : 1 
  3. ID =  1 
  4. NAME =  Paul 
  5. ADDRESS =  California 
  6. SALARY =  25000.0 
  7.  
  8. ID =  2 
  9. NAME =  Allen 
  10. ADDRESS =  Texas 
  11. SALARY =  15000.0 
  12.  
  13. ID =  3 
  14. NAME =  Teddy 
  15. ADDRESS =  Norway 
  16. SALARY =  20000.0 
  17.  
  18. ID =  4 
  19. NAME =  Mark 
  20. ADDRESS =  Rich-Mond 
  21. SALARY =  65000.0 
  22.  
  23. Operation done successfully 


DELETE 操作

Python代码显示了如何我们可以使用DELETE语句删除记录,然后获取并显示COMPANY 表剩余的记录:

  1. #!/usr/bin/python 
  2.  
  3. import psycopg2 
  4.  
  5. conn = psycopg2.connect(database="testdb", user="postgres", password="pass123", host="127.0.0.1", port="5432") 
  6. print "Opened database successfully" 
  7.  
  8. cur = conn.cursor() 
  9.  
  10. cur.execute("DELETE from COMPANY where ID=2;") 
  11. conn.commit 
  12. print "Total number of rows deleted :", cur.rowcount 
  13.  
  14. cur.execute("SELECT id, name, address, salary  from COMPANY") 
  15. rows = cur.fetchall() 
  16. for row in rows: 
  17.    print "ID = ", row[0] 
  18.    print "NAME = ", row[1] 
  19.    print "ADDRESS = ", row[2] 
  20.    print "SALARY = ", row[3], "\n" 
  21.  
  22. print "Operation done successfully"; 
  23. conn.close() 

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

  1. Opened database successfully 
  2. Total number of rows deleted : 1 
  3. ID =  1 
  4. NAME =  Paul 
  5. ADDRESS =  California 
  6. SALARY =  20000.0 
  7.  
  8. ID =  3 
  9. NAME =  Teddy 
  10. ADDRESS =  Norway 
  11. SALARY =  20000.0 
  12.  
  13. ID =  4 
  14. NAME =  Mark 
  15. ADDRESS =  Rich-Mond 
  16. SALARY =  65000.0 
  17.  
  18. Operation done successfully 

原文链接:http://www.yiibai.com/html/postgresql/2013/080998.html

本站文章内容,部分来自于互联网,若侵犯了您的权益,请致邮件chuanghui423#sohu.com(请将#换为@)联系,我们会尽快核实后删除。
Copyright © 2006-2023 DBMNG.COM All Rights Reserved. Powered by DEVSOARTECH            豫ICP备11002312号-2

豫公网安备 41010502002439号