Python:操作嵌入式数据库SQLite
来源:互联网 发布:置乱算法 arnold c语言 编辑:程序博客网 时间:2024/05/19 12:27
去年写过一篇通过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: logger author: socrates date:2012-02-11 ''' logging.config.fileConfig(log_config_filename) logger = logging.getLogger(logname) return loggerdef 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()
[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)
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>
- Python:操作嵌入式数据库SQLite
- java操作嵌入式数据库sqlite
- python SQlite数据库操作
- Python 操作SQLite数据库
- Python操作SQLite数据库
- Python 操作sqlite数据库
- python操作sqlite数据库
- Android操作嵌入式关系型SQLite数据库
- Android操作嵌入式关系型SQLite数据库
- Android操作嵌入式关系型SQLite数据库
- Android操作嵌入式关系型SQLite数据库
- Python操作数据库SQLite --------2
- python操作内置Sqlite数据库
- python SQLite数据库简单操作
- Python进行SQLite数据库操作
- python数据库操作-MySQL,SQLite
- 关于Python操作SQLite数据库
- python基础学习-python操作sqlite数据库
- 线程介绍
- CDHtmlDialog 与 网页交互技巧
- 南京广义软件:建立最简单的repo服务器实例讲解
- 车载网络模拟器move+ns2+sumo【Rapid Generation of Realistic Simulation for VANET】
- 用wscript 操作 Excel 实现 SQL 排序
- Python:操作嵌入式数据库SQLite
- 用python for s60发送中文短信
- 电视看魔术vs雄鹿,电脑看尼克斯vs森林狼。眼花缭乱。
- 最有效提升网站排名的六种SEO手段
- [U]2.4.4 Bessie Come Home 简单最短路问题
- iPhone不可超越的6大优势
- linux下machine_desc结构体中的phys_io与io_pg_offst 的作用及使用方法
- 二维数组指针理解(论坛转载)
- Asp.net DataPager 分页点两次的问题