conersion.py 966 Bytes
import sqlite3
import pandas as pd
import csv
# creating a connection to the database
conn = sqlite3.connect('database.sqlite3')
# creating a cursor object
cur = conn.cursor()

# reading data from the CSV file
with open('cancer.csv') as f:
   reader = csv.reader(f)
   data = list(reader)

# print(data)

# creating a table
# query  = """
# CREATE TABLE Customer(
#     CustomerID INT PRIMARY KEY,
#     CustomerName VARCHAR(50),
#     LastName VARCHAR(50),
#     Country VARCHAR(50),
#     Age int(2),
#   Phone int(10)
# );
# """
# cur.execute(query)
cur.execute("""CREATE TABLE CancerData ('index' INT,Year INT,nationality VARCHAR(30),
    Gender VARCHAR(10),
    cancer VARCHAR(10),
    death INT,
    age INT)"""
)

# # inserting data into the table
for row in data:
   cur.execute(f"INSERT INTO CancerData ('index', Year, nationality, Gender, cancer, death, age) values {tuple(row)}")

# committing changes
conn.commit()

# closing the connection
conn.close()