im beginner with python.I want to convert sql results to a list.Here\'s my code:
cursor = connnect_db()
query = \"SELECT * FROM `tbl`\"
cursor.execute(query)
When I used the answer from Sudhakar Ayyar, the result was a list of lists, as opposed to the list of tuples created by .fetchall(). This was still not what I wanted. With a small change to his code, i was able to get a simple list with all the data from the SQL query:
cursor = connnect_db()
query = "SELECT * FROM `tbl`"
cursor.execute(query)
result = cursor.fetchall() //result = (1,2,3,) or result =((1,3),(4,5),)
final_result = [i[0] for i in result]
Additionally, the last two lines can be combined into:
final_result = [i[0] for i in cursor.fetchall()]