Note that there are some explanatory texts on larger screens.

plurals
  1. POGet row id in sqlite when iterating over a fetchall?
    primarykey
    data
    text
    <p>I'm trying to get the id of the row as I iterate over it, but can't seem to figure out how. Here's what I have.</p> <pre><code>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. </code></pre> <p>Here's where I create and write to the DB. Everything is working except apparently my row ids.</p> <pre><code>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() </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload