murphyslemon / Raspberry_Pi_Server

International Sensor Development Project: This project is a voting system that uses a raspberry pi server and multiple voting devices. The system is easy, secure, scalable, and supports anonymous and registered voting. This repository focuses on the Raspberry Pi server.
0 stars 3 forks source link

DB config #17

Open Javimetro opened 7 months ago

Javimetro commented 7 months ago

This code configures the main app to get connection with the database. The code would typically be added to app.py file Requires SQLAlchemy

!pip install Flask-SQLAlchemy

Username: root Password: Supervote50000 Name of DB: votingsystem

Javimetro commented 7 months ago

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

# Configure SQLAlchemy with MariaDB
#app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://username:password@host:port/dbname'
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:Supervote50000@localhost/votingsystem'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False  # Optionally, to suppress a warning

db = SQLAlchemy(app)
MagnusLii commented 6 months ago

I've modified the code so the credentials are not hard coded in the main file. The credentials are now moved to an external file isdProjectImports/credentials.py and are defined thus:

dbUsername = ''
dbPassword = ''
dbHostname = ''
dbPort = ''
dbName = ''

The modified app.config URI:

from isdProjectImports import credentials
app.config['SQLALCHEMY_DATABASE_URI'] = f'mysql+pymysql://{credentials.dbUsername}:{credentials.dbPassword}@{credentials.dbHostname}:{credentials.dbPort}/{credentials.dbName}'