Connecting SqlLite3 database to a Python file is actually 1 line of code:
conn = sqlite3.connect(r"C:\Users\database.db")
I have used a database from sqllitetutorial.net, which was rather neat. Then DB Browser for SQLLite a LEFT JOIN was implemented, in order to unite the two tables and to test the SQL completely:
At the end the data was printed, with title from the left and the names of the bands on the next column, starting with a tab:
This is achieved with row[0] and row[1]:
1 2 3 |
for row in data : print (row[0]) print ('\t' +row[1]) |
The 0-th index is the first column and 1-st index is the second column.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import sqlite3 def Main(): conn = sqlite3.connect(r"C:\Users\vitos\Desktop\chinook\chinook.db") cur = conn.cursor() sqlCommand = """ SELECT al.Title, ar.Name FROM artists ar LEFT JOIN albums al ON ar.ArtistId = al.AlbumId """ cur.execute(sqlCommand) data = cur.fetchall () for row in data : print (row[0]) print ('\t' +row[1]) if __name__== "__main__": Main() |
Cheers!