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'))
上一篇:Cron表达式简单的介绍


下一篇:linux kernel的cmdline参数解析原理分析【转】