for row in sql database python loop 1

for row in sql database python loop

import sqlite3
conn = sqlite3.connect('file.txt') 
cur = conn.cursor()
cur.execute('''
CREATE TABLE Table (first TEXT, second INTEGER)''')
	#Do things with Table
for row in cur.execute('SELECT * FROM Table') :
	print(row)
cur.close()

Here is what the above code is Doing:
1. Create a connection to the file.txt database.
2. Create a cursor object.
3. Call the cursor’s execute() method to execute SQL commands.
4. Call the cursor’s fetchall() method to retrieve all the data returned by the SELECT statement.
5. Close the cursor and the connection.

Similar Posts