conersion.py
966 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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()