<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當(dāng)前位置: 首頁 - 科技 - 知識百科 - 正文

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

        來源:懂視網(wǎng) 責(zé)編:小采 時間:2020-11-27 14:38:19
        文檔

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

        Python使用PyGreSQL操作PostgreSQL數(shù)據(jù)庫教程:PostgreSQL是一款功能強大的開源關(guān)系型數(shù)據(jù)庫,本文使用python實現(xiàn)了對開源數(shù)據(jù)庫PostgreSQL的常用操作,其開發(fā)過程簡介如下: 一、環(huán)境信息: 1、操作系統(tǒng): RedHat Enterprise Linux 4 Windows XP SP2 2、數(shù)據(jù)庫: PostgreS
        推薦度:
        導(dǎo)讀Python使用PyGreSQL操作PostgreSQL數(shù)據(jù)庫教程:PostgreSQL是一款功能強大的開源關(guān)系型數(shù)據(jù)庫,本文使用python實現(xiàn)了對開源數(shù)據(jù)庫PostgreSQL的常用操作,其開發(fā)過程簡介如下: 一、環(huán)境信息: 1、操作系統(tǒng): RedHat Enterprise Linux 4 Windows XP SP2 2、數(shù)據(jù)庫: PostgreS

        PostgreSQL是一款功能強大的開源關(guān)系型數(shù)據(jù)庫,本文使用python實現(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ù)庫運行于RedHat Linux上,Windows下也要安裝pgAdmin(訪問PostgreSQL服務(wù)器的客戶端)。
        b、PyGreSQL(即pg)模塊下載路徑及API手冊:http://www.pygresql.org/
        PyGreSQL模塊點此本站下載

        二、配置:

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

        E:Program FilesPostgreSQL8.3lib

        E:Program FilesPostgreSQL8.3in

        2、將python安裝目錄C:Python26Libsite-packagespywin32_system32下的dll文件拷貝到C:WINDOWSsystem32

        3、說明:如果跳過以上兩步,在import pg時將會報錯,并且會浪費較長時間才能搞定。

        三、程序?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、運行后命令行打印結(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 ~]$ 
        
        

        聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

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

        Python使用PyGreSQL操作PostgreSQL數(shù)據(jù)庫教程:PostgreSQL是一款功能強大的開源關(guān)系型數(shù)據(jù)庫,本文使用python實現(xiàn)了對開源數(shù)據(jù)庫PostgreSQL的常用操作,其開發(fā)過程簡介如下: 一、環(huán)境信息: 1、操作系統(tǒng): RedHat Enterprise Linux 4 Windows XP SP2 2、數(shù)據(jù)庫: PostgreS
        推薦度:
        標(biāo)簽: python 數(shù) postgreSql
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 亚洲国产成人久久一区久久| 成人免费无码大片a毛片| 免费人成网站在线高清| 亚洲精品天堂成人片AV在线播放| 国产福利在线免费| 亚洲日本人成中文字幕| 成人免费毛片内射美女-百度| 亚洲成人福利在线观看| 一个人免费观看视频www| 中文字幕无码精品亚洲资源网久久| 西西大胆无码视频免费| 亚洲成a∨人片在无码2023| 国产极品粉嫩泬免费观看| 在线播放免费人成视频网站| 亚洲伊人成无码综合网| 成人毛片免费网站| 亚洲AV无码AV男人的天堂不卡| 国产精品va无码免费麻豆 | 91亚洲性爱在线视频| 国产成人免费网站| 亚洲欧美自偷自拍另类视| 国产精品免费看久久久无码| 一级做a爰片久久毛片免费陪| 亚洲乱亚洲乱妇无码麻豆| 久久精品熟女亚洲av麻豆| 成人免费淫片在线费观看| 国产亚洲人成在线播放| 国产亚洲午夜高清国产拍精品| 免费看美女午夜大片| 日韩激情淫片免费看| 春意影院午夜爽爽爽免费| 亚洲国产成人片在线观看| 日本妇人成熟免费中文字幕| 爱情岛亚洲论坛在线观看 | 国产a不卡片精品免费观看| 亚欧洲精品在线视频免费观看| 337p欧洲亚洲大胆艺术| 成人永久免费福利视频网站| 91视频精品全国免费观看| 亚洲不卡视频在线观看| 亚洲午夜无码片在线观看影院猛|