0

I would like to fetch data from a sqlite3 database and save them in a list.

conn=sqlite3.connect("words.db")
cur=conn.cursor()
cur.execute("SELECT word FROM words WHERE state='positive'")
rows=cur.fetchall()
conn.commit()
conn.close()
print(rows)

The output of rows is this:

[('abound',), ('abounds',), ('abundance',)]

I would like to have:

['abound', 'abounds', 'abundance']

I created the db with this:

cur.execute("CREATE TABLE IF NOT EXISTS words (id INTEGER PRIMARY KEY, word TEXT, meta TEXT)")

1 Answer 1

2

Just take the first column of each returned row tuple.

rows = [r[0] for r in cur]

You don't need to commit after a select either.

Sign up to request clarification or add additional context in comments.

Comments

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.