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'))
  • 相关阅读:
    MFC学习篇(二):error LNK2005 及其解决方法
    MFC学习篇(一):用OpenCV显示视频
    记一次mysql安装!
    常用数据对应关系及简单介绍
    docker
    月份及星期 缩写
    java 面对对象笔记
    linux小案例 定时备份数据库
    rpm_yum_开发工具的安装
    shell入门
  • 原文地址:https://www.cnblogs.com/doudongchun/p/3699140.html
Copyright © 2011-2022 走看看