设为首页 收藏本站
查看: 749|回复: 0

[经验分享] Python:操作嵌入式数据库SQLite

[复制链接]

尚未签到

发表于 2016-11-29 08:49:06 | 显示全部楼层 |阅读模式
  去年写过一篇通过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[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()      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[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 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. 运行后的日志信息:  [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>

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-306943-1-1.html 上篇帖子: sqlite排序规则 下篇帖子: SQLite FAQ中文版
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表