views:

73

answers:

3

Hello, I can't show the data from database sqlite in python.

connection = sqlite3.connect('db')
connection.cursor().execute('CREATE TABLE IF NOT EXISTS users (     \
                        id TEXT,                                    \
                        name TEXT,                                  \
                        avatar TEXT                                 \
                )')

# In cycle:
query = 'INSERT INTO users VALUES ("' + str(friend.id) + '", "' + friend.name + '", "' + friend.avatar +'" )'
print query
connection.cursor().execute(query)
connection.commit()

# After cycle
print connection.cursor().fetchall()

Sample output of query variable:

INSERT INTO users VALUES ("111", "Some Name", "http://avatar/path" )

In result, fetchall returns empty tuple. Why?


UPD Forgotten code:

connection.cursor().execute('SELECT * FROM users')
connection.cursor().fetchall()

→ []

+3  A: 

INSERT does not return data. To get the data back out, you'll have to issue a SELECT statement.

Larry Lustig
Sorry, I forgot about that code. I did SELECT statement.
Ockonal
A: 

Because the create table string as displayed is syntactically invalid Python, as is the insert into string.

msw
What do you mean? WHat's wrong is there?
Ockonal
I think he's referring to the quotes, although I'm guessing that the mismatch is not really present in your code since you don't get a Python syntax error, and since the INSERT statement prints out ok.
Larry Lustig
@larry-lustig @msw oh sorry, that's my fault. There is passing constant string and I forgot to take out the quotes after replacing it with simple string.
Ockonal
+1  A: 
import sqlite3

con = sqlite3.connect("db")

con.execute("create table users(id, name, avatar)")

con.execute("insert into users(id, name, avatar) values (?, ?, ?)", (friend.id, friend.name, friend.avatar))
con.commit()

for row in con.execute("select * from users")
    print row

con.close()
wok