use sqlalchemy to create sqlite3 database

Solutions on MaxInterview for use sqlalchemy to create sqlite3 database by the best coders in the world

showing results for - "use sqlalchemy to create sqlite3 database"
María Fernanda
02 Jun 2017
1from sqlalchemy import create_engine
2import pandas as pd
3# creates an engine for a database in current directory 
4engine = create_engine('sqlite:///yourDatabaseName.sqlite3') 
5# creates a dataframe in memory
6df = pd.DataFrame({'tablename' : ['Data 1', 'Data 2', 'Data 3']})
7# stores the dataframe as a table in the database
8df.to_sql('tablename', con=engine)
9# queries the table and stores the results in another dataframe
10dfOne = pd.read_sql_table('tablename', engine)
11# or you can create a custom sql query and store the results in a dataframe
12dfTwo = pd.DataFrame(engine.execute("SELECT * FROM tablename").fetchall())
13