def __init__(self, filename, tablename, pkName=None, pkType=None, no_pk=False): self.filename = filename self.tablename = tablename self.db = sqlite3.connect(filename) if no_pk: # 没有主键,创建一个占位符 sql = "CREATE TABLE IF NOT EXISTS `%s` (_id int);" % tablename elif pkName is None: # 只有integer允许AUTOINCREMENT sql = "CREATE TABLE IF NOT EXISTS `%s` (id integer primary key autoincrement);" % tablename else: # sqlite允许主键重复,允许空值 sql = "CREATE TABLE IF NOT EXISTS `%s` (`%s` %s primary key);" % ( tablename, pkName, pkType) self.execute(sql)
def __init__(self, dbpath=None): if dbpath is None: dbpath = config.DB_PATH self.path = dbpath self.conn = sqlite3.connect(dbpath) self.cursor = self.conn.cursor()