0

I have a sqlite database that is populated with values from csv files. I would like to create a script that when run:

  1. deletes the old tables
  2. creates new tables with the same schema (with newly updated values)

I noticed that sqlite script files don't accept ".mode csv" or .import "csv". Is there a way to automate this is with a script of some sort?

1
  • Not clear, but I suppose you should use some gems, for example require "csv" and require "sqlite3" Commented Apr 26, 2018 at 14:44

1 Answer 1

1

If you want a Python approach, you can use to_sql method from the pandas package to write to SQLite. Pandas can replace existing tables and automatically generate the schema based on the CSV file read.

import sqlite3
import pandas as pd

conn = sqlite3.connect('my.db')

# read the csv file
df = pd.read_csv("my.csv")

# write to SQLite
df.to_sql("my_tbl", conn, if_exists="replace")

conn.close()
Sign up to request clarification or add additional context in comments.

1 Comment

awesome, this is exactly what I was looking for!

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.