zoukankan      html  css  js  c++  java
  • python数据库操作——sqlite3模块

    # -*- coding: utf-8 -*-
    '''
    Version : Python27
    Author  : Spring God
    Date    : 2012-4-26
    '''
    
    import sqlite3
    
    
    def set_conf(db_file, key, value):
    
        _db = sqlite3.connect(db_file)
        _db.execute('create table if not exists section(key varchar PRIMARY KEY , value varchar)')
        try:
            _db.execute("insert into section(key, value) values ('%s','%s')"
                       % (key, value))
        except sqlite3.IntegrityError:
            _db.execute("update section set value = '%s' where key = '%s'"
                       % (value, key))
        _db.commit()
        _db.close()
    
    
    def get_conf(db_file, key):
    
        _db = sqlite3.connect(db_file)
        _db.execute('create table if not exists section(key varchar PRIMARY KEY , value varchar)')
        cur = _db.cursor()
        cur.execute("select value from section where key = '%s'" % key)
        res = cur.fetchone()
        _db.close()
    
        if res == None:
            return None
        else:
            return res[0]
    
    
    def del_conf(db_file, key):
    
        _db=sqlite3.connect(db_file)
        _db.execute("delete from section where key = '%s'" % key)
        _db.commit()
        _db.close()
    
    
    if __name__ == '__main__':
    
        set_conf('set.db', 'key1', 'value1')
        set_conf('set.db', 'key2', 'value2')
        del_conf('set.db', 'key')
        print(get_conf('set.db', 'key2'))
  • 相关阅读:
    个人总结05
    微软拼音的用户体验
    个人总结04
    典型用户和用户场景模式
    个人总结03
    个人总结02
    构建之法阅读笔记06
    个人总结01
    学习进度条——第七周
    WebApi学习总结系列第五篇(消息处理管道)
  • 原文地址:https://www.cnblogs.com/doudongchun/p/3699140.html
Copyright © 2011-2022 走看看