0

I have a large csv file with nearly 100 columns with varying data types that I would like to load into a sqlite database using sqlalchemy. This will be an ongoing thing where I will periodicly load new data as a new table in the database. This seems like it should be trivial, but I cannot get anything to work.

All the solutions I've looked at so far have defined the columns explicitly when creating the tables.

Here is a minimal example (with far fewer columns) of what I have at the moment.

from sqlalchemy import *
import pandas as pd

values_list = []
url = r"https://raw.githubusercontent.com/amanthedorkknight/fifa18-all-player-statistics/master/2019/data.csv"
df = pd.read_csv(url,sep=",")   
df = df.to_dict()
metadata = MetaData()
engine = create_engine("sqlite:///" + r"C:\Users\...\example.db")
connection = engine.connect()
# I would like define just the primary key column and the others be automatically loaded...
t1 = Table('t1', metadata, Column('ID',Integer,primary_key=True))
metadata.create_all(engine)
stmt = insert(t1).values()
values_list.append(df)
results = connection.execute(stmt, values_list)
values_list = []
connection.close()

3
  • Can you include a small sample of the data? Is all the data of the same type (e.g. Integer, Float, String)? Commented May 10, 2019 at 11:23
  • The sample url above is a good representation of the different data types present in the actual data Commented May 14, 2019 at 11:43
  • This answer does something like what you want. Commented May 18, 2019 at 11:35

1 Answer 1

1

Thanks for the suggestions. After some time searching, a decent solution is using the sqlathanor package. There is a function called generate-model-from-csv which allows you to read in a csv (also available for dictionary, json, etc) and build a sqlalchemy model directly. It is imperfect on datatype recognition, but certainly will save you some time if you have a lot of columns.

https://sqlathanor.readthedocs.io/en/latest/api.html#generate-model-from-csv

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.