0% found this document useful (0 votes)
1 views3 pages

CSV To SQL

converting csv to sqlite3

Uploaded by

hariharanstu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views3 pages

CSV To SQL

converting csv to sqlite3

Uploaded by

hariharanstu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

import sqlite3

import os

# Specify the folder where you want the DB file


folder_path = r"C:\Users\Sivadhanya\OneDrive\Desktop\internship\Day-4" # Change this

# Make sure the folder exists (create if not)


os.makedirs(folder_path, exist_ok=True)

# Full path to your .db file


db_path = os.path.join(folder_path, "swiggy.db")

# Create/connect to the SQLite DB


conn = sqlite3.connect(db_path)
cursor = conn.cursor()

# Example: Create table


cursor.execute("""
CREATE TABLE IF NOT EXISTS test_table (
id INTEGER PRIMARY KEY,
name TEXT
);
""")

conn.commit()
conn.close()

print(f"✅ SQLite DB created at: {db_path}")


import pandas as pd
import sqlite3
csv_path=r"C:\Users\Sivadhanya\OneDrive\Desktop\internship\Day-4\swiggy.csv"
df=pd.read_csv(csv_path)
# === Step 2: Connect to SQLite DB (creates file if it doesn't exist) ===
db_path = r"C:\Users\Sivadhanya\OneDrive\Desktop\internship\Day-4\swiggy.db" # ← This is where
the .db file will be created
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# Step 3: Create table (adjust types as needed)
cursor.execute("""
CREATE TABLE IF NOT EXISTS swiggy_data (
ID INTEGER,
Area TEXT,
City TEXT,
Restaurant TEXT,
Price INTEGER,
Avg_ratings REAL,
Total_ratings INTEGER,
Food_type TEXT,
Address TEXT,
Delivery_time INTEGER
);
""")

# Step 4: Insert data into table


for _, row in df.iterrows():
cursor.execute("""
INSERT INTO swiggy_data (ID, Area, City, Restaurant, Price, Avg_ratings,
Total_ratings, Food_type, Address, Delivery_time)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""", tuple(row))
# Step 5: Commit and close
conn.commit()
conn.close()

print("Data inserted into SQLite database successfully.")

You might also like