muri 发表于 2016-11-29 08:49:06

Python:操作嵌入式数据库SQLite

  去年写过一篇通过C语言操作SQLite数据库的文章《SQLITE学习笔记一(打开、操作及关闭数据库,C程序实现)》,最近在学习python,所以使用ptyhon实现了一下,实现不多描述了,代码中的注释已经非常详细了。直接贴上来。
  1. 实现:
  #!/usr/bin/env python# -*- coding: utf-8 -*-#导入日志及SQLite3模块import loggingimport logging.configimport 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: loggerauthor: socratesdate:2012-02-11'''logging.config.fileConfig(log_config_filename)logger = logging.getLogger(logname)return loggerdef operate_sqlite3_tbl_product():'''Function:操作SQLITE3数据库函数Input:NONEOutput: NONEauthor: socratesdate: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)    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)sqlite_cursor.close()sqlite_conn.close()      returnsqlite_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)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)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 rowsqlite_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()
  

2. 运行后的日志信息:  DEBUG:operate_sqlite3_tbl_product enter... (test_log.py:39)INFO:conntect sqlite database(.\db\sqlite_pytest.db) succ. (test_log.py:49)INFO:drop table(tbl_product3) succ. (test_log.py:66)INFO:create table(tbl_product3) succ. (test_log.py:83)INFO:insert record into table(tbl_product3) succ. (test_log.py:97)INFO:(1, u'apple') (test_log.py:104)DEBUG:operate_sqlite3_tbl_product leaving... (test_log.py:110)
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\dbE:\Study\学习\工作程序\py_test\src\db>sqlite3.exe sqlite_pytest.dbSQLite version 3.7.9 2011-11-01 00:52:41Enter ".help" for instructionsEnter SQL statements terminated with a ";"sqlite> .tablestbl_product3sqlite> select * from tbl_product3;1|applesqlite> .quitE:\Study\学习\工作程序\py_test\src\db>
页: [1]
查看完整版本: Python:操作嵌入式数据库SQLite