forked from Sayan8981/crawlers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_db_tables.py
executable file
·66 lines (51 loc) · 2.46 KB
/
create_db_tables.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 mysql.connector
import sys
class createdb_table:
def __int__(self):
self.database="Hindunews"
self.username=''
self.password=''
self.db_list=[]
self.connection=''
self.cursor=''
def user_input(self):
#import pdb;pdb.set_trace()
self.username=sys.argv[1]
self.password=sys.argv[2]
def set_up_db_connection(self):
#import pdb;pdb.set_trace()
self.connection=mysql.connector.connect(host="localhost",user="%s"%self.username,passwd="%s"%self.password)
self.cursor=self.connection.cursor()
def create_require_db_tables(self):
self.user_input()
self.set_up_db_connection()
self.cursor.execute("show databases;")
for db in self.cursor:
self.db_list.append(db[0].encode())
#import pdb;pdb.set_trace()
if self.database not in self.db_list:
self.cursor.execute("create database %s;"%self.database)
print("\n %s db created"%self.database)
self.cursor.execute("use %s;"%self.database)
create_tables=["create table National_news_details (sk_key varchar(500) primary key, News_headlines varchar(1000), News_intro varchar(2000), News_details varchar(50000), Country varchar(100),Date varchar(100),Updated_at varchar(200), News_url varchar(1000));"]
for query in create_tables:
self.cursor.execute(query)
print("\n")
print("Table created.................")
else:
print("%s db is already exist"%self.database)
self.cursor.execute("use %s;"%self.database)
self.cursor.execute("show tables;")
table_cursor=self.cursor.fetchall()
if not table_cursor:
create_tables=["create table National_news_details (sk_key varchar(500) primary key, News_headlines varchar(1000), News_intro varchar(2000), News_details varchar(50000), Country varchar(100),Date varchar(100),Updated_at varchar(200), News_url varchar(1000));"]
for query in create_tables:
self.cursor.execute(query)
print ("table created.......")
else:
print ("\n")
print("Table is already exist in DB",table_cursor)
self.connection.close()
object_=createdb_table()
object_.__int__()
object_.create_require_db_tables()