0

This might be a basic question but I can't seem to get the output of the python/sql statements I write. For example, when type the following,

from sql import *
from sql.aggregate import *
from sql.conditionals import *
user = Table('user')
select = user.select()
tuple(select)

I don't see the output (i.e the records) of these statements in the console. Why is this? When I type tuple(select) in the shell, I get the following as output:

('SELECT * FROM "combine2" AS "a"', ())

Note that I am using the sql module.

2
  • What module(s) are you using for SQL? Commented Aug 21, 2017 at 16:05
  • @ScottHunter that's python-sql library. Commented Aug 22, 2017 at 3:34

1 Answer 1

2

From what I understand, python-sql library, which you are using, is for creating/generating SQL queries only. In order to execute them, you need to use a database driver. E.g. in case of SQLite:

import sqlite3

# ...
statement = user.select()

conn = sqlite3.connect('example.db')
cursor = conn.cursor()

cursor.execute(statement)
for row in cursor.fetchall():
    print(row)
Sign up to request clarification or add additional context in comments.

3 Comments

When I type cursor.execute(tuple(statement)) or cursor.execute(statement)...it doesn't work.
@Damien wait, what do you mean by "does not work"? Do you fetch the results afterwards like in my sample? Thanks.
Make sure that the argument to sqlite2.connect is the correct path to your database file (by checking the value returned, of course).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.