forked from alexmercerind/youtube-search-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_database.py
More file actions
49 lines (34 loc) · 1.25 KB
/
init_database.py
File metadata and controls
49 lines (34 loc) · 1.25 KB
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
from sqlite3 import Error
import sqlite3
import traceback
def create_connection(local_db_path):
try:
conn = sqlite3.connect(local_db_path)
# logging.info(f"SQLite DB has been created with version {sqlite3.version}")
return conn
except Error as e:
print(f"Error occured while connecting to DB: {e}")
def create_db(conn, config_data):
try:
print("Creating db", config_data["queries"]["create_maintable"])
conn.execute(config_data["queries"]["create_maintable"])
except Exception as e:
print(f"Error occured while creating DB: {e}")
# alternative way to insert without using DataFrames
def insert_into_db(df, conn):
cols = "`,`".join([str(i) for i in df.columns.tolist()])
cur = conn.cursor()
try:
for i, row in df.iterrows():
sql = "INSERT INTO `youtube_videos` (`" + cols + "`) VALUES (" + "%s," * (len(row) - 1) + "%s)"
cur.execute(sql, tuple(row))
conn.commit()
except Exception:
print(traceback.format_exc())
# just a fct to connect to db
def check_db():
con = sqlite3.connect("youtube_videos.db")
cur = con.cursor()
for row in cur.execute('SELECT * FROM youtube_videos1;'):
print(row)
con.close()