Skip to content
Advertisement

fetchall() not working even with values in database

I am working in python with sqlite3 and I am trying to create a database.

I created a table:

c.execute("CREATE TABLE persoane (nume text, varsta integer, salariu integer)")

Then I added the values into the table:

c.execute("INSERT INTO persoane VALUES('David', 16, 1000)")

When I try to get the values out of the database with fetchall() I get an empty list []

I know the values had been added because I verified it with an online program.

This is the full code for reference:

import sqlite3

conn = sqlite3.connect('baza_date.db')

c = conn.cursor()

c.execute("CREATE TABLE persoane (nume text, varsta integer, salariu integer)")

conn.commit()

c.execute("INSERT INTO persoane VALUES('David', 16, 1000)")

conn.commit()

print(c.fetchall())

Advertisement

Answer

You need to execute a query and then use the fetchall(). For example:

info = c.execute("SELECT * FROM persoane;").fetchall()
print(info)

Otherwise, you’re not selecting anything from your database, and your c cursor doesn’t know what you want to fetch.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement