postgresql rows 使用PyGreSQL操作PostgreSQL数据库

postgresql rows 使用PyGreSQL操作PostgreSQL数据库

一、环境信息:

1、操作系统:

Linux 4

XP SP2

2、数据库:

.3

3、 开发工具:

+Pydev+.6+(提供pg模块)

4、说明:

a、数据库运行于 Linux上postgresql rowspostgresql rowspostgresql rows 使用PyGreSQL操作PostgreSQL数据库,下也要安装(访问服务器的客户端)。

postgresql rows

b、(即pg)模块下载路径及API手册:

二、配置:

1、将安装路径下以下子目录添加到系统环境变量中:

E: Files\8.3lib

E: Files\8.3bin

2、将安装目录C:\Libsite-下的dll文件拷贝到C:\

3、说明:

a. 如果跳过以上两步,在 pg时将会报错postgresql rows,浪费较长时间才搞定。

b. 如果对安装和配置不熟悉postgresql rows 使用PyGreSQL操作PostgreSQL数据库,请参考本博客中以下几篇文章:

《 Linux上安装》

《服务端监听设置及客户端连接方法》

《数据库创建、删除方法》

postgresql rows

更多相关知识请访问从我的博客专栏:系列

三、程序实现:

#!/usr/bin/env python  # -*- coding: utf-8 -*-    #导入日志及pg模块  import logging  import logging.config  import pg    #日志配置文件名  LOG_FILENAME = 'logging.conf'    #日志语句提示信息  LOG_CONTENT_NAME = 'pg_log'    def log_init(log_config_filename, logname):      '''      Function:日志模块初始化函数      Input:log_config_filename:日志配置文件名             lognmae:每条日志前的提示语句      Output: logger      author: socrates      date:2012-02-12      '''      logging.config.fileConfig(log_config_filename)      logger = logging.getLogger(logname)      return logger    def operate_postgre_tbl_product():      '''      Function:操作pg数据库函数      Input:NONE      Output: NONE      author: socrates      date:2012-02-12      '''        pgdb_logger.debug("operate_postgre_tbl_product enter...")             #连接数据库        try:          pgdb_conn = pg.connect(dbname = 'kevin_test', host = '192.168.230.128', user = 'dyx1024', passwd = '888888')      except Exception, e:           print e.args[0]           pgdb_logger.error("conntect postgre database failed, ret = %s" % e.args[0])               return                 pgdb_logger.info("conntect postgre database(kevin_test) succ.")                 #删除表      sql_desc = "DROP TABLE IF EXISTS tbl_product3;"      try:          pgdb_conn.query(sql_desc)      except Exception, e:          print 'drop table failed'          pgdb_logger.error("drop table failed, ret = %s" % e.args[0])          pgdb_conn.close()            return            pgdb_logger.info("drop table(tbl_product3) succ.")           #创建表      sql_desc = '''CREATE TABLE tbl_product3(          i_index INTEGER,          sv_productname VARCHAR(32)          );'''      try:              pgdb_conn.query(sql_desc)      except Exception, e:          print 'create table failed'          pgdb_logger.error("create table failed, ret = %s" % e.args[0])          pgdb_conn.close()            return                   pgdb_logger.info("create table(tbl_product3) succ.")               #插入记录         sql_desc = "INSERT INTO tbl_product3(sv_productname) values('apple')"      try:          pgdb_conn.query(sql_desc)      except Exception, e:          print 'insert record into table failed'          pgdb_logger.error("insert record into table failed, ret = %s" % e.args[0])          pgdb_conn.close()            return                 pgdb_logger.info("insert record into table(tbl_product3) succ.")                  #查询表 1             sql_desc = "select * from tbl_product3"      for row in pgdb_conn.query(sql_desc).dictresult():          print row          pgdb_logger.info("%s", row)          #查询表2              sql_desc = "select * from tbl_test_port"      for row in pgdb_conn.query(sql_desc).dictresult():          print row           pgdb_logger.info("%s", row)                     #关闭数据库连接           pgdb_conn.close()             pgdb_logger.debug("operate_sqlite3_tbl_product leaving...")     if __name__ == '__main__':             #初始化日志系统      pgdb_logger = log_init(LOG_FILENAME, LOG_CONTENT_NAME)               #操作数据库      operate_postgre_tbl_product()      

四、测试:

1、运行后命令行打印结果:

{'sv_productname': 'apple', 'i_index': None}  {'i_status': 1, 'i_port': 2, 'i_index': 1}  {'i_status': 1, 'i_port': 3, 'i_index': 2}  {'i_status': 1, 'i_port': 5, 'i_index': 3}  {'i_status': 1, 'i_port': 0, 'i_index': 5}  {'i_status': 1, 'i_port': 18, 'i_index': 7}  {'i_status': 1, 'i_port': 8, 'i_index': 8}  {'i_status': 1, 'i_port': 7, 'i_index': 9}  {'i_status': 1, 'i_port': 21, 'i_index': 10}  {'i_status': 1, 'i_port': 23, 'i_index': 11}  {'i_status': 1, 'i_port': 29, 'i_index': 12}  {'i_status': 1, 'i_port': 3000, 'i_index': 4}  {'i_status': 1, 'i_port': 1999, 'i_index': 6}

2、日志文件内容:

[2012-02-12 18:09:53,536  pg_log]DEBUG:  operate_postgre_tbl_product enter... (test_func.py:36)  [2012-02-12 18:09:53,772  pg_log]INFO:  conntect postgre database(kevin_test) succ. (test_func.py:46)  [2012-02-12 18:09:53,786  pg_log]INFO:  drop table(tbl_product3) succ. (test_func.py:58)  [2012-02-12 18:09:53,802  pg_log]INFO:  create table(tbl_product3) succ. (test_func.py:73)  [2012-02-12 18:09:53,802  pg_log]INFO:  insert record into table(tbl_product3) succ. (test_func.py:85)  [2012-02-12 18:09:53,802  pg_log]INFO:  {'sv_productname': 'apple', 'i_index': None} (test_func.py:91)  [2012-02-12 18:09:53,802  pg_log]INFO:  {'i_status': 1, 'i_port': 2, 'i_index': 1} (test_func.py:97)  [2012-02-12 18:09:53,802  pg_log]INFO:  {'i_status': 1, 'i_port': 3, 'i_index': 2} (test_func.py:97)  [2012-02-12 18:09:53,802  pg_log]INFO:  {'i_status': 1, 'i_port': 5, 'i_index': 3} (test_func.py:97)  [2012-02-12 18:09:53,802  pg_log]INFO:  {'i_status': 1, 'i_port': 0, 'i_index': 5} (test_func.py:97)  [2012-02-12 18:09:53,819  pg_log]INFO:  {'i_status': 1, 'i_port': 18, 'i_index': 7} (test_func.py:97)  [2012-02-12 18:09:53,819  pg_log]INFO:  {'i_status': 1, 'i_port': 8, 'i_index': 8} (test_func.py:97)  [2012-02-12 18:09:53,819  pg_log]INFO:  {'i_status': 1, 'i_port': 7, 'i_index': 9} (test_func.py:97)  [2012-02-12 18:09:53,819  pg_log]INFO:  {'i_status': 1, 'i_port': 21, 'i_index': 10} (test_func.py:97)  [2012-02-12 18:09:53,819  pg_log]INFO:  {'i_status': 1, 'i_port': 23, 'i_index': 11} (test_func.py:97)  [2012-02-12 18:09:53,819  pg_log]INFO:  {'i_status': 1, 'i_port': 29, 'i_index': 12} (test_func.py:97)  [2012-02-12 18:09:53,819  pg_log]INFO:  {'i_status': 1, 'i_port': 3000, 'i_index': 4} (test_func.py:97)  [2012-02-12 18:09:53,819  pg_log]INFO:  {'i_status': 1, 'i_port': 1999, 'i_index': 6} (test_func.py:97)  [2012-02-12 18:09:53,819  pg_log]DEBUG:  operate_sqlite3_tbl_product leaving... (test_func.py:101)

3、psql查看结果:

[root@kevin ~]# su - postgres  [postgres@kevin ~]$ psql -U dyx1024 -d kevin_test  psql (8.4.2)  Type "help" for help.    kevin_test=# dt                  List of relations   Schema |     Name      | Type  |     Owner        --------+---------------+-------+----------------   public | tbl_product3  | table | dyx1024   public | tbl_test_port | table | pg_test_user_3  (2 rows)    kevin_test=# select * from tbl_product3;   i_index | sv_productname   ---------+----------------           | apple  (1 row)    kevin_test=# select * from tbl_test_port;   i_index | i_port | i_status   ---------+--------+----------         1 |      2 |        1         2 |      3 |        1         3 |      5 |        1         5 |      0 |        1         7 |     18 |        1         8 |      8 |        1         9 |      7 |        1        10 |     21 |        1        11 |     23 |        1        12 |     29 |        1         4 |   3000 |        1         6 |   1999 |        1  (12 rows)    kevin_test=# q  [postgres@kevin ~]$ 

公告:
1. 本站所有资源来源于用户上传和网络,如有侵权请联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系站长处理!
6. 本站不售卖代码,资源标价只是站长收集整理的辛苦费!如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。
7. 站长QQ号码 2205675299

资源库 - 资源分享下载网 » postgresql rows 使用PyGreSQL操作PostgreSQL数据库

常见问题FAQ

关于资源售价和售后服务的说明?
本站所有资源的标价均为本站收集资源的辛苦费,不代表资源本身的价值。软件是高智慧高价值的商品,不可能是白菜价。本站资源标价只是赞助费用,收取的赞助费仅用来维持本站的日常运营!毕竟收集整理资料需要投入云计算资源和站长大量的精力。
代码有没有售后服务和技术支持?
由于代码的运行具有不可预见性,本站不保证代码完整可运行,不提供技术支持和售后服务。 本站原创代码都是站长自己开发的,可以有偿提供技术支持服务。 网站里标明【亲测】的代码都是站长亲测过的,其他的代码由于精力有限,没有一一测试,不能保证代码就一定能够使用,更没有技术支持服务,下载前请自行斟酌。
有没有搭建服务?
由于搭建服务比较费时费力,所以本站除了原创代码外均不提供搭建服务。本站分享代码纯属兴趣爱好,不以盈利为目的,请勿咨询有没有搭建服务,谢谢理解。
链接地址失效了怎么办?
请带上资源链接地址联系客服,工作时间内我们看到后将第一时间回复。
关于解压密码
本站资源一般都没有加密,如果发现需要解压密码的,那么就输入 hao.35dc.com 试试。

发表评论

资源库,由老程序员细心甄别、精心筛选,只为提供优质的源码资源

关于我们 联系我们