Reading a SQL file

Interacting with a SQL database through pandas requires the sqlalchemy dependency to be installed.

First, let's define the engine from which connection parameters can be obtained:

engine = create_engine('sqlite:///:memory:')

Now, let's read the data_sql table from the SQL database:

with engine.connect() as conn, conn.begin():
print(pd.read_sql_table('data_sql', conn))

This results in the following output:

Output of read_sql_table

The read_sql_table() function reads an entire table for the given table name. A specific column can be set as the index when reading: 

pd.read_sql_table('data_sql', engine, index_col='index')

This results in the following output:

Output of read_sql_table with indexing

The columns argument lets us choose specific columns when reading data by passing the column names as a list. Any date columns can be parsed into a specific format during the read process, as shown in the following code:

pd.read_sql_table('data_sql', engine, parse_dates={'Entry_date': '%Y-%m-%d'})
pd.read_sql_table('data_sql', engine, parse_dates={'Entry_date': {'format': '%Y-%m-%d %H:%M:%S'}})

The schema argument in this function helps specify the schema from which the table is to be extracted.

Instead of reading the entire table, it is also possible to use a SQL query to get data in the necessary format. We can do this with the read_sql_query() function:

pd.read_sql_query("SELECT Last_name, Score FROM data_sql", engine)

This results in the following output:

Output of read_sql_query

To run the INSERT and CREATE queries, which do not return any output, the sql.execute() function can be used. This requires an sql file of pandas.io to be imported:

from pandas.io import sql
sql.execute("INSERT INTO tablename VALUES (90, 100, 171)", engine)

With a sqlite database, the connection to the engine has to be defined as follows so that it can be used in the read_sql_table() or read_sql_query() functions. The sqlite module must be imported prior to this:

import sqlite3
conn = sqlite3.connect(':memory:')

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.47.178