user2948667
user2948667

Reputation: 65

Python - How to put sqlite result into a list

I'm trying to take an sqlite result at put it into a list for further use, but im getting a TypeError: an integer is required error, cant I just make an array where ID = 1 and flowpump = on and so on? In PHP that is an easy task, but in python I cant seem to make it Work :( I get the error in the first insert line: dbarray.insert('id',row['id'])

conn = sqlite3.connect("/mnt/ramdisk/heatcontroldb.db")
conn.row_factory = sqlite3.Row

curs = conn.cursor()
curs.execute("SELECT id, timeout, time, setby, vvbvalve, vvbflowvalve, flowpump, vvbsettemp, sunflowsettemp from status where id = 1")
for row in curs:
    print row['id']
    dbarray.insert('id',row['id'])
    dbarray.insert('timeout',row['timeout'])
    dbarray.insert('time',row['time'])
    dbarray.insert('setby',row['setby'])
    dbarray.insert('vvbvalve',row['vvbvalve'])
    dbarray.insert('vvbflowvalve',row['vvbflowvalve'])
    dbarray.insert('flowpump',row['flowpump'])
    dbarray.insert('vvbsettemp',row['vvbsettemp'])
    dbarray.insert('sunflowsettemp',row['sunflowsettemp'])

Can anyone help a python newbe? :)

Kind regards Morten

Upvotes: 0

Views: 1164

Answers (1)

Jon Clements
Jon Clements

Reputation: 142146

If you wanted a pure dict from the result, then instead of all that typing, do:

dbdict = dict(zip(row.keys(), row))

This also has the advantage that row.keys() will correspond to the columns in the SELECT statement. Note that it does require the sqlite3.Row row factory to be in use.

Upvotes: 1

Related Questions