1 | initial version |
To convert a directory containing a CSV file into an SQLite3 database using Python, you can follow the below steps:
pip install pandas
Create a SQLite3 database: Create an empty SQLite3 database using the SQLite3 command-line tool or any other SQLite3 management tool.
Create a table: Once you have created the database, create a table with the same columns as the CSV file. You can use the following SQL statement to create a table:
CREATE TABLE table_name (
column1 datatype1,
column2 datatype2,
column3 datatype3,
.....
);
import pandas as pd
df = pd.read_csv('file.csv')
import sqlite3
conn = sqlite3.connect('database.db')
df.to_sql('table_name', conn, if_exists='replace', index=False)
conn.close()
In the above code, the 'if_exists' parameter is used to replace the table if it already exists in the database. The 'index' parameter is set to False to prevent Pandas from adding an index as a new column in the database.
Using the above steps, you can easily convert a directory containing a CSV file into an SQLite3 database using Python.