Select sqlite queries

From TRCCompSci - AQA Computer Science
Revision as of 11:38, 6 October 2019 by Admin (talk | contribs) (Created page with "You need to ensure you have followed the tutorial to create a database connection. =Python Code= The 'create', 'update', 'insert', and 'delete' queries only require execution...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

You need to ensure you have followed the tutorial to create a database connection.

Python Code

The 'create', 'update', 'insert', and 'delete' queries only require execution, however the 'select' query needs to return values. So add the following

def select_query(conn, sql):

    cur = conn.cursor()
    cur.execute(sql)
 
    rows = cur.fetchall()
 
    for row in rows:
        print(row)

you can therefore run:

select_query("Select * from table")