2

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
  • I have done : c.execute('SELECT x1 FROM my_table WHERE pagenr = 2 AND x1 = 10').fetchall() but i haven't been able to get the results as list/tuple. Commented Jun 7, 2012 at 19:03

1 Answer 1

2

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()
Sign up to request clarification or add additional context in comments.

2 Comments

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]
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]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.