我试图在迭代它时获取行的 id,但似乎无法弄清楚如何。这就是我所拥有的。
cursor = conn.cursor()
cursor.execute("SELECT * FROM Logs {} ORDER BY created {} LIMIT 0, {}".format(created_filter, settings.order, settings.limit))
logs = list()
for row in cursor.fetchall():
# I want to get the row id in here.
这是我创建和写入数据库的地方。除了显然我的行 ID 之外,一切都在工作。
file = settings.logs_path
log_table_name = 'Logs'
pin_table_name = 'Pins'
conn = sqlite3.connect(file, check_same_thread=False)
c = conn.cursor()
log_schema = dict(rowid = 'INTEGER PRIMARY KEY AUTOINCREMENT',
asctime = 'TEXT',
created = 'REAL',
exc_info = 'TEXT',
exc_text = 'TEXT',
filename = 'TEXT',
funcName = 'TEXT',
levelname = 'TEXT',
levelno = 'INTEGER',
lineno = 'INTEGER',
module = 'TEXT',
msecs = 'REAL',
message = 'TEXT',
msg = 'TEXT',
name = 'TEXT',
pathname = 'TEXT',
process = 'INTEGER',
processName = 'TEXT',
relativeCreated = 'REAL',
thread = 'INTEGER',
threadName = 'TEXT')
pin_schema = dict(rowid = 'INTEGER PRIMARY KEY AUTOINCREMENT',
pinnedId = 'INTEGER')
sql = 'CREATE TABLE IF NOT EXISTS ' + log_table_name + ' ({})'.format(','.join(log_schema))
c.execute(sql)
sql = 'CREATE TABLE IF NOT EXISTS ' + pin_table_name + ' ({})'.format(','.join(pin_schema))
c.execute(sql)
conn.commit()
def write_log(log):
keys = [key for key in log_schema.keys() if key != 'rowid']
values = []
for key in keys:
try:
values.append(str(getattr(log, key)))
except Exception:
values.append('')
parameters = ['?' for key in keys]
c.execute("INSERT INTO Logs ({}) VALUES ({})".format(','.join(keys),','.join(parameters)), values)
conn.commit()