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'))

 

转载于:https://www.cnblogs.com/doudongchun/p/3699140.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值