I have a sqlite table with 4 columns, say: id, pagenr, x1, y1. I need to get the y1 column values as a list/tuple, but only from rows where pagenr and x1 have certain values. Help much appriciated. NB: I am using python 2.6.
1 Answer
This code using the sqlite3 module should fetch the rows of the result from the query into the output list:
conn = sqlite3.connect('database.db')
c = conn.cursor()
c.execute('select y1 from table where pagenr=? and x1=?',
(pagenr_value, x1_value))
output = c.fetchall()
2 Comments
gypaetus
The result of the fetchall method is a list of tuples. If you are interested in the first value you could do OutputList[0][0]
gypaetus
A tuple with a single item is always constructed by following a value with a comma. If you prefer to get a list of values you could use a list comprehension: [x[0] for x in TupleList]