python - Get row id in sqlite when iterating over a fetchall? -


i'm trying id of row iterate on it, can't seem figure out how. here's have.

cursor = conn.cursor() cursor.execute("select * logs {} order created {} limit 0, {}".format(created_filter, settings.order, settings.limit))  logs = list() row in cursor.fetchall():     # want row id in here. 

here's create , write db. working except apparently row ids.

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 key in log_schema.keys() if key != 'rowid']     values = []     key in keys:         try:             values.append(str(getattr(log, key)))         except exception:             values.append('')     parameters = ['?' key in keys]     c.execute("insert logs ({}) values ({})".format(','.join(keys),','.join(parameters)), values)     conn.commit() 

your create table commands lacking field types. notice happens here:

in [27]: pin_schema = dict(rowid = 'integer primary key autoincrement',                 pinnedid = 'integer')  in [30]: pin_table_name = 'pins'  in [31]: sql = 'create table if not exists ' + pin_table_name + ' ({})'.format(','.join(pin_schema))  in [32]: sql out[32]: 'create table if not exists pins (pinnedid,rowid)' 

all field names no field types. instead, either define pin_schema , log_schema lists of strings include both field , field type, or iterate through pin_schema.items():

sql = 'create table if not exists ' + pin_table_name + ' ({})'.format(     ', '.join('{f} {t}'.format(f=field, t=field_type)              field, field_type in pin_schema.items()))  in [35]: sql out[35]: 'create table if not exists pins (pinnedid integer, rowid integer primary key autoincrement)' 

the lack of field types explains why rowid not autoincrementing.


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -