-
Notifications
You must be signed in to change notification settings - Fork 0
/
intodb.py
66 lines (48 loc) · 1.54 KB
/
intodb.py
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import pandas as pd
import sqlite3
#load a csv file into a dataframe
df = pd.read_csv("output_search.csv")
#create a connection
def create_connection():
conn = sqlite3.connect("employees_details.db")
curr = conn.cursor()
return conn, curr
#create a table, overwrite it if it exists
def create_table():
global conn
global curr
curr.execute("""DROP TABLE IF EXISTS careem_employees""")
curr.execute("""CREATE TABLE careem_employees(
name text,
title text,
location text,
profile text
)""")
#inserts data into the database
def insert_data(df):
global conn
global curr
df['name'] = df.name.str.title()
df['location'] = df['location'].str.normalize('NFKD').str.encode('ascii', errors='ignore').str.decode('utf-8')
for i in range(0, len(df)):
name = df.iloc[i]['name'].strip()
title = df.iloc[i]['title'].strip()
location = df.iloc[i]['location'].strip()
profile = df.iloc[i]['profile'].strip()
curr.execute("""INSERT INTO careem_employees(name,title, location, profile) VALUES (?,?,?,?)""", (name, title, location, profile))
conn.commit()
def fetch_data():
global conn
global curr
curr.execute("""SELECT * FROM careem_employees""")
rows = curr.fetchall()
for row in rows:
print("--------")
print(row[0])
print(row[1])
print(row[2])
print(row[3])
conn, curr = create_connection()
create_table()
insert_data(df)
fetch_data()