Closed RithvikPranav closed 1 year ago
check the mysql connector
you can use sqlite or any sql server connector like Postgres over the mysql if you don`t have mysql
import sqlite3
class Database:
path = './libs/'
dbName = 'store.db'
def __init__(self):
self.connection =
sqlite3.connect(self.path+self.dbName)
self.cursor = self.connection.cursor()
def fetch_query(self, query):
self.cursor.execute(query)
results = self.cursor.fetchall()
return results
def fetch_columns_names(self, table_name):
query = "PRAGMA table_info({})".format(table_name)
results = self.fetch_query(query)
columns_names = []
print(results)
for row in results:
if row[1]!='id':
columns_names.append(row[1])
self.connection.commit()
return columns_names
def create_table(self, table_name, columns):
query = "CREATE TABLE IF NOT EXISTS {} ({})".format(table_name, columns)
self.cursor.execute(query)
def insert_data(self, table_name, data):
query = "INSERT INTO {} ({}) VALUES ({})".format(table_name,self.fetch_columns_names(table_name), data).replace('[', '').replace(']', '')
print(query, "--------> Running ")
self.cursor.execute(query)
self.connection.commit()
self.close_connection()
def select_data(self, table_name, where_clause=None,fetchall = True):
query = f"SELECT * FROM {table_name} {where_clause}"
if where_clause:
query = query.format(where_clause)
print(query, "--------> Running ")
self.cursor.execute(query)
if fetchall ==False:
results01 = self.cursor.fetchone()
else:
results01 = self.cursor.fetchall()
self.connection.commit()
self.close_connection()
return results01
I think this was a localhost connector problem 🙃 Closing this... feel free to create another issue if this issue still persists :)
it is throwning an exception saying that it is not allow to connect to Mysql Server