KK KTP arsip
This commit is contained in:
39
database.py
Normal file
39
database.py
Normal file
@@ -0,0 +1,39 @@
|
||||
"""
|
||||
Database Configuration for OCR Application
|
||||
Using Flask-SQLAlchemy with MySQL (PyMySQL driver)
|
||||
"""
|
||||
|
||||
import os
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
|
||||
db = SQLAlchemy()
|
||||
|
||||
# Database configuration
|
||||
DB_CONFIG = {
|
||||
'host': os.environ.get('DB_HOST', 'localhost'),
|
||||
'port': os.environ.get('DB_PORT', '3306'),
|
||||
'database': os.environ.get('DB_NAME', 'ocr_db'),
|
||||
'user': os.environ.get('DB_USER', 'ocr_user'),
|
||||
'password': os.environ.get('DB_PASSWORD', 'ocr_password123')
|
||||
}
|
||||
|
||||
def get_database_uri():
|
||||
"""Generate SQLAlchemy database URI"""
|
||||
return f"mysql+pymysql://{DB_CONFIG['user']}:{DB_CONFIG['password']}@{DB_CONFIG['host']}:{DB_CONFIG['port']}/{DB_CONFIG['database']}?charset=utf8mb4"
|
||||
|
||||
def init_db(app):
|
||||
"""Initialize database with Flask app"""
|
||||
app.config['SQLALCHEMY_DATABASE_URI'] = get_database_uri()
|
||||
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
||||
app.config['SQLALCHEMY_ENGINE_OPTIONS'] = {
|
||||
'pool_recycle': 3600,
|
||||
'pool_pre_ping': True
|
||||
}
|
||||
|
||||
db.init_app(app)
|
||||
|
||||
with app.app_context():
|
||||
db.create_all()
|
||||
print(f"✓ Database connected: {DB_CONFIG['database']}@{DB_CONFIG['host']}")
|
||||
|
||||
return db
|
||||
Reference in New Issue
Block a user