add to db with create_engine of slqlalchemy
Jump to turn (11)
- 1add to db with createengine of slqlalchemy
- 2add to db with createengine of slqlalchemy if had Users table
- 3add to db with createengine of slqlalchemy if had Users model in other file
- 4add data to db with slqlalchemy
- 5add data to db with slqlalchemy, not orm
- 6csv file to dataframe
- 7csv dictreader to dataframe
- 8use FileStorage in DictReader
- 9how to use pd.readcsv if i dont know what delimeter will be
- 10what is separator
- 11it doesnt work to specify delimeter=None
- User
add to db with create_engine of slqlalchemy
- Assistant
import sqlalchemy from sqlalchemy import create_engine
Create engine that connects to database
engine = create_engine('sqlite:///mydatabase.db')
Connect to the database
conn = engine.connect()
Define metadata
metadata = sqlalchemy.MetaData()
Define table structure
my_table = sqlalchemy.Table('my_table', metadata, sqlalchemy.Column('id', sqlalchemy.Integer, primary_key=True), sqlalchemy.Column('name', sqlalchemy.String), sqlalchemy.Column('age', sqlalchemy.Integer) )
Create the table if it doesn't already exist
metadata.create_all(engine)
Insert data into the table
query = my_table.insert().values(name='John', age=25) conn.execute(query)
Close the connection
conn.close()