最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python使用PyGreSQL操作PostgreSQL數(shù)據(jù)庫教程

 更新時(shí)間:2014年07月30日 16:32:50   投稿:shichen2014  
這篇文章主要介紹了Python使用PyGreSQL操作PostgreSQL數(shù)據(jù)庫,需要的朋友可以參考下

PostgreSQL是一款功能強(qiáng)大的開源關(guān)系型數(shù)據(jù)庫,本文使用python實(shí)現(xiàn)了對開源數(shù)據(jù)庫PostgreSQL的常用操作,其開發(fā)過程簡介如下:

一、環(huán)境信息:

   1、操作系統(tǒng):

        RedHat Enterprise Linux 4
        Windows XP SP2

  2、數(shù)據(jù)庫:

        PostgreSQL8.3

  3、 開發(fā)工具:

        Eclipse+Pydev+python2.6+PyGreSQL(提供pg模塊)

  4、說明:

        a、PostgreSQL數(shù)據(jù)庫運(yùn)行于RedHat Linux上,Windows下也要安裝pgAdmin(訪問PostgreSQL服務(wù)器的客戶端)。
        b、PyGreSQL(即pg)模塊下載路徑及API手冊:http://www.pygresql.org/
 PyGreSQL模塊點(diǎn)此本站下載

二、配置:

       1、將pgAdmin安裝路徑下以下子目錄添加到系統(tǒng)環(huán)境變量中:

             E:\Program Files\PostgreSQL\8.3\lib

             E:\Program Files\PostgreSQL\8.3\bin

       2、將python安裝目錄C:\Python26\Lib\site-packages\pywin32_system32下的dll文件拷貝到C:\WINDOWS\system32

       3、說明:如果跳過以上兩步,在import pg時(shí)將會(huì)報(bào)錯(cuò),并且會(huì)浪費(fèi)較長時(shí)間才能搞定。

三、程序?qū)崿F(xiàn):

#!/usr/bin/env python
# -*- coding: utf-8 -*-

#導(dǎo)入日志及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:日志模塊初始化函數(shù)
  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數(shù)據(jù)庫函數(shù)
  Input:NONE
  Output: NONE
  author: socrates
  date:2012-02-12
  ''' 
  pgdb_logger.debug("operate_postgre_tbl_product enter...") 
  
  #連接數(shù)據(jù)庫 
  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.") 
 
  #創(chuàng)建表
  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)    
   
  #關(guān)閉數(shù)據(jù)庫連接   
  pgdb_conn.close()    
  pgdb_logger.debug("operate_sqlite3_tbl_product leaving...") 

if __name__ == '__main__': 
  
  #初始化日志系統(tǒng)
  pgdb_logger = log_init(LOG_FILENAME, LOG_CONTENT_NAME)  
  
  #操作數(shù)據(jù)庫
  operate_postgre_tbl_product()
  

四、測試:

 1、運(yùn)行后命令行打印結(jié)果:

{'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、日志文件內(nèi)容:

[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查看結(jié)果:

[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 ~]$ 

相關(guān)文章

  • PyCharm:method may be static問題及解決

    PyCharm:method may be static問題及解決

    這篇文章主要介紹了PyCharm:method may be static問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • 一文帶你吃透Python中的日期時(shí)間模塊

    一文帶你吃透Python中的日期時(shí)間模塊

    Python?提供了?日期和時(shí)間模塊用來處理日期和時(shí)間,還可以用于格式化日期和時(shí)間等常見功能。這篇文章就來帶大家了解一下它的使用,需要的可以參考一下
    2023-02-02
  • Python selenium環(huán)境搭建實(shí)現(xiàn)過程解析

    Python selenium環(huán)境搭建實(shí)現(xiàn)過程解析

    這篇文章主要介紹了Python selenium環(huán)境搭建實(shí)現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • 自定義Django默認(rèn)的sitemap站點(diǎn)地圖樣式

    自定義Django默認(rèn)的sitemap站點(diǎn)地圖樣式

    這篇文章主要介紹了自定義Django默認(rèn)的sitemap站點(diǎn)地圖樣式,通過代碼給大家介紹了使用Django的sitemap功能,代碼很簡單非常不錯(cuò)對大家的工作或?qū)W習(xí)具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-03-03
  • python入門課程第三講之編碼規(guī)范知多少

    python入門課程第三講之編碼規(guī)范知多少

    這篇文章主要介紹了python入門課程第三講之編碼規(guī)范知多少,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-09-09
  • pandas與pyspark計(jì)算效率對比分析

    pandas與pyspark計(jì)算效率對比分析

    這篇文章主要介紹了pandas與pyspark計(jì)算效率對比,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-06-06
  • 解決Pytorch dataloader時(shí)報(bào)錯(cuò)每個(gè)tensor維度不一樣的問題

    解決Pytorch dataloader時(shí)報(bào)錯(cuò)每個(gè)tensor維度不一樣的問題

    這篇文章主要介紹了解決Pytorch dataloader時(shí)報(bào)錯(cuò)每個(gè)tensor維度不一樣的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • OpenCV?光流Optical?Flow示例

    OpenCV?光流Optical?Flow示例

    這篇文章主要為大家介紹了OpenCV?光流Optical?Flow示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-04-04
  • 在Python的while循環(huán)中使用else以及循環(huán)嵌套的用法

    在Python的while循環(huán)中使用else以及循環(huán)嵌套的用法

    這篇文章主要介紹了在Python的while循環(huán)中使用else以及循環(huán)嵌套的用法,是Python入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下
    2015-10-10
  • Tensorflow之構(gòu)建自己的圖片數(shù)據(jù)集TFrecords的方法

    Tensorflow之構(gòu)建自己的圖片數(shù)據(jù)集TFrecords的方法

    本篇文章主要介紹了Tensorflow之構(gòu)建自己的圖片數(shù)據(jù)集TFrecords的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-02-02

最新評論

新建县| 黄骅市| 瑞丽市| 庆云县| 甘谷县| 西宁市| 津南区| 中西区| 南平市| 理塘县| 福海县| 海原县| 南召县| 桂平市| 新乡市| 贵州省| 白水县| 榆社县| 越西县| 东乡| 内丘县| 三明市| 许昌县| 东城区| 上高县| 政和县| 大厂| 玉山县| 麻城市| 六盘水市| 泽州县| 铅山县| 大名县| 宁夏| 甘洛县| 美姑县| 莱芜市| 专栏| 同德县| 溆浦县| 道真|