Skip to content

Commit d6c920d

Browse files
Initial commit - Channel Db file created
1 parent 74fc2ba commit d6c920d

File tree

15 files changed

+3181
-0
lines changed

15 files changed

+3181
-0
lines changed

SQL/Channels/Database.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Json to Database for app api
2+
3+
import json
4+
import os
5+
import sqlite3
6+
import mysql.connector
7+
8+
folderPath = r'H:\Github\PythonScripts\SQL\Channels\json'
9+
scriptDir = os.path.dirname(os.path.realpath(__file__))
10+
# connecting to the database
11+
connection = sqlite3.connect(scriptDir + os.path.sep + "packages.db")
12+
crsr = connection.cursor()
13+
14+
def putMemoryDb(path):
15+
with open(path) as f:
16+
channels_json = json.loads(f.read())
17+
fileName = path.split('.')
18+
fileName = fileName[0].split('\\')
19+
fileName = fileName[-1]
20+
print(fileName)
21+
22+
# SQL command to create a table in the database
23+
sql_command = """ CREATE TABLE """+fileName+""" (
24+
ID INTEGER PRIMARY KEY AUTOINCREMENT,
25+
channel_name varchar(255) ,
26+
channel_genre varchar(255),
27+
channel_language varchar(255),
28+
channel_price varchar(255),
29+
channel_quality varchar(255)
30+
); """
31+
32+
# execute the statement
33+
crsr.execute(sql_command)
34+
sql = "INSERT INTO "+ fileName +" (channel_name,channel_genre) VALUES (?,?)"
35+
for c in channels_json:
36+
crsr.execute(sql, (c['channel_name'] , c['channel_genre']))
37+
connection.commit()
38+
39+
40+
import mysql.connector
41+
42+
mydb = mysql.connector.connect(
43+
host="localhost",
44+
user="root",
45+
passwd="",
46+
database="channels_packages"
47+
)
48+
49+
mycursor = mydb.cursor()
50+
51+
def putLocalHostDb(path):
52+
with open(path) as f:
53+
channels_json = json.loads(f.read())
54+
fileName = path.split('.')
55+
fileName = fileName[0].split('\\')
56+
fileName = fileName[-1]
57+
print(fileName)
58+
mycursor.execute("CREATE TABLE "+fileName+" (id INT AUTO_INCREMENT PRIMARY KEY, channel_name VARCHAR(255), channel_genre VARCHAR(255) , channel_language VARCHAR(255) , channel_quality VARCHAR(255) , channel_price VARCHAR(255))")
59+
sql = "INSERT INTO "+fileName+" (channel_name, channel_genre) VALUES (%s, %s)"
60+
for c in channels_json:
61+
val = (c['channel_name'] , c['channel_genre'])
62+
mycursor.execute(sql, val)
63+
mydb.commit()
64+
65+
66+
for f in os.scandir(folderPath):
67+
# print(f.path)
68+
# putMemoryDb(f.path)
69+
putLocalHostDb(f.path)
70+
71+
# close the connection
72+
connection.close()

0 commit comments

Comments
 (0)