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'))
  • 相关阅读:
    动态生成java、动态编译、动态加载
    boostrap标签
    Java后端WebSocket的Tomcat实现
    编程过程与规范
    敏捷开发过程
    软件过程模型
    软件过程
    ISO9126 质量模型
    手机淘宝架构演化实践
    蒋勋:知识分子应具备怎样的美学修养
  • 原文地址:https://www.cnblogs.com/doudongchun/p/3699140.html
Copyright © 2011-2022 走看看