Read from SQL
read_sql runs a query and returns a DataFrame. to_sql writes one into a table. This example uses an in memory SQLite database.
Example
import pandas as pd
import numpy as np
import sqlite3
con = sqlite3.connect(':memory:')
pd.DataFrame({'id': [1, 2, 3], 'v': [10, 20, 30]}).to_sql('t', con, index=False)
print(pd.read_sql('SELECT id, v FROM t WHERE v > 10', con))Result
id v 0 2 20 1 3 30
Exercise
Write to SQL.
df.("t", con)
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.