Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -68,7 +68,9 @@ class DatabaseConnection(BaseModel):
|
|
| 68 |
@app.post("/api/connect")
|
| 69 |
async def connect_to_database(connection: DatabaseConnection):
|
| 70 |
try:
|
| 71 |
-
|
|
|
|
|
|
|
| 72 |
conn = psycopg2.connect(
|
| 73 |
host=connection.server,
|
| 74 |
port=connection.port,
|
|
@@ -78,7 +80,6 @@ async def connect_to_database(connection: DatabaseConnection):
|
|
| 78 |
)
|
| 79 |
query_schemas = "SELECT schema_name FROM information_schema.schemata"
|
| 80 |
query_tables = "SELECT table_name FROM information_schema.tables WHERE table_schema = %s"
|
| 81 |
-
print(query_tables)
|
| 82 |
elif connection.database_type == "mysql":
|
| 83 |
print(f"inside mysql",connection.server,connection.port,connection.databaseName,connection.username,connection.password)
|
| 84 |
conn = mysql.connector.connect(
|
|
@@ -115,6 +116,14 @@ async def connect_to_database(connection: DatabaseConnection):
|
|
| 115 |
raise HTTPException(status_code=500, detail=str(e))
|
| 116 |
|
| 117 |
# Function to create a new database connection for MySQL (Example)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
def get_db_connection():
|
| 119 |
try:
|
| 120 |
cnx = mysql.connector.connect(user=DB_USER, password=DB_PASSWORD, host=DB_HOST, database=DB_NAME)
|
|
|
|
| 68 |
@app.post("/api/connect")
|
| 69 |
async def connect_to_database(connection: DatabaseConnection):
|
| 70 |
try:
|
| 71 |
+
print(f"Attempting to connect to database: {connection.database_type}")
|
| 72 |
+
if connection.database_type == "Postgres":
|
| 73 |
+
print(f"PostgreSQL connection details - Host: {connection.server}, Port: {connection.port}, Database: {connection.databaseName}, User: {connection.username}")
|
| 74 |
conn = psycopg2.connect(
|
| 75 |
host=connection.server,
|
| 76 |
port=connection.port,
|
|
|
|
| 80 |
)
|
| 81 |
query_schemas = "SELECT schema_name FROM information_schema.schemata"
|
| 82 |
query_tables = "SELECT table_name FROM information_schema.tables WHERE table_schema = %s"
|
|
|
|
| 83 |
elif connection.database_type == "mysql":
|
| 84 |
print(f"inside mysql",connection.server,connection.port,connection.databaseName,connection.username,connection.password)
|
| 85 |
conn = mysql.connector.connect(
|
|
|
|
| 116 |
raise HTTPException(status_code=500, detail=str(e))
|
| 117 |
|
| 118 |
# Function to create a new database connection for MySQL (Example)
|
| 119 |
+
def get_db_connection():
|
| 120 |
+
try:
|
| 121 |
+
cnx = mysql.connector.connect(user=DB_USER, password=DB_PASSWORD, host=DB_HOST, database=DB_NAME)
|
| 122 |
+
return cnx
|
| 123 |
+
except mysql.connector.Error as err:
|
| 124 |
+
logging.error(f"Database connection error: {err}")
|
| 125 |
+
return None
|
| 126 |
+
# Function to create a new database connection for MySQL (Example)
|
| 127 |
def get_db_connection():
|
| 128 |
try:
|
| 129 |
cnx = mysql.connector.connect(user=DB_USER, password=DB_PASSWORD, host=DB_HOST, database=DB_NAME)
|