You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
1.8 KiB
72 lines
1.8 KiB
# Based on https://mariadb.com/resources/blog/how-to-connect-python-programs-to-mariadb/
|
|
|
|
import mariadb
|
|
import sys
|
|
import datetime
|
|
|
|
#stores current time
|
|
currentTime = datetime.datetime.now()
|
|
|
|
#Make a global cursor in order to use the same one all the time
|
|
cursor = mariadb.cursors.Cursor
|
|
|
|
#Connects to the databse
|
|
def connect(dbName):
|
|
# Connect to MariaDB Platform
|
|
try:
|
|
conn = mariadb.connect(
|
|
user="root",
|
|
password="KyKvMdRt586591!*",
|
|
host="db.keydev.me",
|
|
port=3306,
|
|
database=dbName
|
|
)
|
|
except mariadb.Error as e:
|
|
print(f"Error connecting to MariaDB Platform: {e}")
|
|
sys.exit(1)
|
|
cur = conn.cursor()
|
|
|
|
return cur
|
|
|
|
#Get all the column naes of the given table
|
|
def getColumnNames(cursor, table):
|
|
command = "SHOW COLUMNS FROM " + table
|
|
cursor.execute(command)
|
|
colName = []
|
|
index = 0
|
|
for row in cursor:
|
|
colName.append(row[0])
|
|
return colName
|
|
|
|
#Gets all the Foreign Keys of the given table
|
|
def getForeignKeys(cursor, table):
|
|
command = "SHOW INDEX FROM " + table
|
|
cursor.execute(command)
|
|
colName = []
|
|
index = 0
|
|
for row in cursor:
|
|
if not row[2] == "PRIMARY":
|
|
colName.append(row[4])
|
|
return colName
|
|
|
|
#Gets all the Primary Keys of the given table
|
|
def getPrimaryKey(cursor, table):
|
|
command = "SHOW INDEX FROM " + table
|
|
cursor.execute(command)
|
|
index = 0
|
|
for row in cursor:
|
|
if row[2] == "PRIMARY":
|
|
pk = row[4]
|
|
return pk
|
|
|
|
|
|
def addData(table,column,data):
|
|
command = "INSERT INTO "+table+" ("+column+") VALUES (NULL, " +data+")"
|
|
print(command)
|
|
|
|
addData("market", "id, name, upload_date ", "'test', '2023-07-20'")
|
|
#INSERT INTO `market` (`id`, `name`, `upload_date`, `last_update_date`) VALUES (NULL, 'selam', '2023-07-20', '2023-07-20 17:19:11');
|
|
|
|
|
|
|