Python操作數據庫SQLite

fmms 12年前發布 | 51K 次閱讀 Python SQLite 數據庫服務器

使用ptyhon實現了一下,實現不多描述了,代碼中的注釋已經非常詳細了。直接貼上來。

  1. 實現:
    #!/usr/bin/env python
    # -- coding: utf-8 --

導入日志及SQLite3模塊

import logging import logging.config import sqlite3

日志配置文件名

LOG_FILENAME = 'logging.conf'

日志語句提示信息

LOG_CONTENT_NAME = 'sqlite_log'

SQLite數據庫名稱

DB_SQLITE_PATH = ".\db\sqlite_pytest.db"

def log_init(log_config_filename, logname): ''' Function:日志模塊初始化函數 Input:log_config_filename:日志配置文件名 lognmae:每條日志前的提示語句 Output: logger author: socrates date:2012-02-11 ''' logging.config.fileConfig(log_config_filename) logger = logging.getLogger(logname) return logger

def operate_sqlite3_tbl_product(): ''' Function:操作SQLITE3數據庫函數 Input:NONE Output: NONE author: socrates date:2012-02-11 '''
sqlite_logger.debug("operate_sqlite3_tbl_product enter...")

#連接數據庫  
try:
    sqlite_conn = sqlite3.connect(DB_SQLITE_PATH)
except sqlite3.Error, e:
     print 'conntect sqlite database failed.'
     sqlite_logger.error("conntect sqlite database failed, ret = %s" % e.args[0])    
     return    

sqlite_logger.info("conntect sqlite database(%s) succ." % DB_SQLITE_PATH) 

#獲取游標 
sqlite_cursor = sqlite_conn.cursor()

#刪除表
sql_desc2 = "DROP TABLE IF EXISTS tbl_product3;"
try:
    sqlite_cursor.execute(sql_desc2)
except sqlite3.Error, e:
     print 'drop table failed'
     sqlite_logger.error("drop table failed, ret = %s" % e.args[0])
     sqlite_cursor.close()
     sqlite_conn.close()      
     return
sqlite_conn.commit()    

sqlite_logger.info("drop table(tbl_product3) succ.") 

#建表
sql_desc = '''CREATE TABLE tbl_product3(
i_index INTEGER PRIMARY KEY,
sv_productname VARCHAR(32)
);'''
try:
    sqlite_cursor.execute(sql_desc)
except sqlite3.Error, e:
     print 'drop table failed.'
     sqlite_logger.error("drop table failed, ret = %s" % e.args[0])
     sqlite_cursor.close()
     sqlite_conn.close()    
     return    
sqlite_conn.commit()

sqlite_logger.info("create table(tbl_product3) succ.") 

#插入記錄
sql_desc = "INSERT INTO tbl_product3(sv_productname) values('apple')"
try:
    sqlite_cursor.execute(sql_desc)
except sqlite3.Error, e:
    print 'insert record failed.'
    sqlite_logger.error("insert record failed, ret = %s" % e.args[0])  
    sqlite_cursor.close()
    sqlite_conn.close()    
    return    
sqlite_conn.commit()

sqlite_logger.info("insert record into table(tbl_product3) succ.") 

#查詢記錄
sql_desc = "SELECT * FROM tbl_product3;"    
sqlite_cursor.execute(sql_desc)
for row in sqlite_cursor:
    print row
    sqlite_logger.info("%s", row) 

#關閉游標和數據庫句柄    
sqlite_cursor.close()
sqlite_conn.close()

sqlite_logger.debug("operate_sqlite3_tbl_product leaving...") 

if name == 'main':

#初始化日志系統
sqlite_logger = log_init(LOG_FILENAME, LOG_CONTENT_NAME)   

#操作數據庫
operate_sqlite3_tbl_product()
</pre>2. 運行后的日志信息:<pre class="brush:python; toolbar: true; auto-links: false;">[2012-02-12 12:13:52,131  sqlite_log]DEBUG:  operate_sqlite3_tbl_product enter... (test_log.py:39)

[2012-02-12 12:13:52,147 sqlite_log]INFO: conntect sqlite database(.\db\sqlite_pytest.db) succ. (test_log.py:49) [2012-02-12 12:13:52,147 sqlite_log]INFO: drop table(tbl_product3) succ. (test_log.py:66) [2012-02-12 12:13:52,240 sqlite_log]INFO: create table(tbl_product3) succ. (test_log.py:83) [2012-02-12 12:13:52,365 sqlite_log]INFO: insert record into table(tbl_product3) succ. (test_log.py:97) [2012-02-12 12:13:52,365 sqlite_log]INFO: (1, u'apple') (test_log.py:104) [2012-02-12 12:13:52,365 sqlite_log]DEBUG: operate_sqlite3_tbl_product leaving... (test_log.py:110)</pre>3.通過命令行查看:

Microsoft Windows XP [版本 5.1.2600]
(C) 版權所有 1985-2001 Microsoft Corp.

C:\Documents and Settings\socrates.WINXP-DUANYX>cd /d E:\Study\學習\工作程序\py_ test\src\db

E:\Study\學習\工作程序\py_test\src\db>sqlite3.exe sqlite_pytest.db SQLite version 3.7.9 2011-11-01 00:52:41 Enter ".help" for instructions Enter SQL statements terminated with a ";" sqlite> .tables tbl_product3 sqlite> select * from tbl_product3; 1|apple sqlite> .quit

E:\Study\學習\工作程序\py_test\src\db></pre>來自:http://blog.csdn.net/dyx1024/article/details/7252274

 本文由用戶 fmms 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!