| import sqlite3 |
| import pandas as pd |
| import datetime |
|
|
| |
|
|
| def create_insert_table(db_name, table_name, df): |
| try: |
| sqliteconnection = sqlite3.connect('sqlite_databases/{}.db'.format(db_name)) |
| cursor = sqliteconnection.cursor() |
| print('DB Init') |
|
|
| |
| |
| |
|
|
| |
| |
| |
|
|
| |
| cursor.execute("DROP TABLE IF EXISTS {}".format(table_name)) |
|
|
| |
| bizrate_table_string = """ CREATE TABLE {} ( |
| title VARCHAR(500), |
| Brand CHAR(100), |
| url TEXT, |
| Image TEXT, |
| Skus TEXT, |
| price VARCHAR(50), |
| originalPrice VARCHAR(50), |
| markdownPercent VARCHAR(50), |
| totalPrice VARCHAR(50), |
| condition VARCHAR(10), |
| stock VARCHAR(10), |
| relevancy REAL); """.format(table_name) |
|
|
| recsys_table_string = """ CREATE TABLE {} ( |
| title VARCHAR(500), |
| Brand CHAR(100), |
| url TEXT, |
| Image TEXT, |
| Skus TEXT, |
| price REAL, |
| originalPrice REAL, |
| markdownPercent REAL, |
| totalPrice REAL, |
| condition VARCHAR(10), |
| stock VARCHAR(10), |
| relevancy REAL); """.format(table_name) |
|
|
| |
| if db_name == 'bizrate': |
| table_string = bizrate_table_string |
| cursor.execute(table_string) |
| elif db_name == 'RecSysData': |
| table_string = recsys_table_string |
| cursor.execute(table_string) |
|
|
|
|
| |
| df.to_sql(table_name, sqliteconnection, if_exists='replace', index=False) |
| sqliteconnection.commit() |
| |
| except sqlite3.Error as error: |
| print('Error Occured - ', error) |
|
|
| |
| finally: |
| if sqliteconnection: |
| sqliteconnection.close() |
| print('SQLite Connection Closed.') |
|
|
|
|
| def query_table(db_name, table_name): |
| try: |
| sqliteconnection = sqlite3.connect('sqlite_databases/{}.db'.format(db_name)) |
| cursor = sqliteconnection.cursor() |
| print('DB Init') |
|
|
|
|
| query_string = ''' |
| SELECT * |
| FROM {} |
| '''.format(table_name) |
|
|
| query_op_df = pd.read_sql_query(query_string, sqliteconnection) |
|
|
| |
| except sqlite3.Error as error: |
| print('Error Occured - ', error) |
|
|
| |
| finally: |
| if sqliteconnection: |
| sqliteconnection.close() |
| print('SQLite Connection Closed.') |
|
|
| return query_op_df |
|
|
|
|
| def insert_clickdata_table(session_id, keyword, publisherid, sku, count): |
| try: |
| conn = sqlite3.connect('sqlite_databases/{}.db'.format('session_data')) |
| cursor = conn.cursor() |
| print('Click Data DB Init') |
|
|
| |
| table_string = """ CREATE TABLE IF NOT EXISTS session_data ( |
| clicked_at TIMESTAMP, |
| session_id TEXT, |
| keyword VARCHAR(100), |
| publisherid VARCHAR(100), |
| Skus TEXT, |
| count INTEGER); """.format(table_name) |
| cursor.execute(table_string) |
| currentDateTime = datetime.datetime.now() |
| insert_string = '''INSERT INTO session_data VALUES ('{}', '{}', '{}', '{}', '{}', {})'''.format(currentDateTime, session_id, keyword, publisherid, sku, count) |
| print(insert_string) |
| cursor.execute(insert_string) |
| |
|
|
| conn.commit() |
|
|
|
|
| |
| except sqlite3.Error as error: |
| print('Error Occured - ', error) |
|
|
| |
| finally: |
| if conn: |
| conn.close() |
| print('SQLite Connection Closed.') |
|
|
|
|
| def check_for_table(db_name, table_name): |
| '''Checks whether the specified table exists within the specified database. |
| Returns True if it does. |
| Else returns False.''' |
|
|
| filepath = 'sqlite_databases/' |
| conn = sqlite3.connect(filepath + db_name + ".db") |
| cursor = conn.cursor() |
|
|
| query_string = ''' |
| SELECT name |
| FROM sqlite_master |
| WHERE type = 'table' AND name='{}'; |
| '''.format(table_name) |
|
|
| result = cursor.execute(query_string) |
| list_of_tables = result.fetchall() |
| conn.close() |
| |
| return bool(len(list_of_tables)) |
|
|
| |
|
|
| |
| file_path = 'bizrate/aqua_725895.xlsx' |
|
|
| file_name = file_path.split('/') |
| db_name = file_name[0] |
| table_name = file_name[1].split('.')[0] |
|
|
|
|
| |
| |
|
|
| |
| |
|
|
| |
|
|
|
|
| |
| |
| |
| |
|
|