Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,72 +1,10 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from sqlalchemy import create_engine, text
|
| 3 |
import pyodbc
|
| 4 |
|
| 5 |
-
|
| 6 |
-
SERVER = "indsr-foundry.database.windows.net"
|
| 7 |
-
DATABASE = "foundry_db_monarch"
|
| 8 |
-
DRIVER = "ODBC Driver 18 for SQL Server"
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
st.
|
| 15 |
-
|
| 16 |
-
st.title("SQL Server Connection Test")
|
| 17 |
-
|
| 18 |
-
# User inputs for authentication
|
| 19 |
-
username = st.text_input("Database Username", placeholder="Enter your username")
|
| 20 |
-
password = st.text_input("Database Password", type="password", placeholder="Enter your password")
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
def get_user_key():
|
| 24 |
-
"""Step 1: Connect using service account and retrieve user key"""
|
| 25 |
-
try:
|
| 26 |
-
service_conn_str = (
|
| 27 |
-
f"mssql+pyodbc://{SERVICE_USERNAME}:{SERVICE_PASSWORD}@{SERVER}/{DATABASE}?"
|
| 28 |
-
f"driver={DRIVER}&Encrypt=yes&TrustServerCertificate=no"
|
| 29 |
-
)
|
| 30 |
-
engine = create_engine(service_conn_str, pool_pre_ping=True)
|
| 31 |
-
|
| 32 |
-
with engine.connect() as connection:
|
| 33 |
-
result = connection.execute(text("EXEC [entity].[PR_user_find] @people_id=:user_id"), {"user_id": username})
|
| 34 |
-
user_key = result.scalar() # Extract the user key
|
| 35 |
-
|
| 36 |
-
return user_key
|
| 37 |
-
except Exception as e:
|
| 38 |
-
return f"❌ Failed to retrieve user key: {str(e)}"
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
def test_connection(user_key, password):
|
| 42 |
-
"""Step 2: Reconnect using the retrieved user key (like VBA)"""
|
| 43 |
-
try:
|
| 44 |
-
if not user_key:
|
| 45 |
-
return "⚠️ User key not found. Cannot proceed."
|
| 46 |
-
|
| 47 |
-
conn_str = (
|
| 48 |
-
f"mssql+pyodbc://{user_key}:{password}@{SERVER}/{DATABASE}?"
|
| 49 |
-
f"driver={DRIVER}&Encrypt=yes&TrustServerCertificate=no"
|
| 50 |
-
)
|
| 51 |
-
engine = create_engine(conn_str, pool_pre_ping=True)
|
| 52 |
-
|
| 53 |
-
with engine.connect() as connection:
|
| 54 |
-
result = connection.execute(text("SELECT 1"))
|
| 55 |
-
|
| 56 |
-
return "✅ Connection successful! You are connected to SQL Server."
|
| 57 |
-
|
| 58 |
-
except Exception as e:
|
| 59 |
-
return f"❌ Connection failed: {str(e)}"
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
# Button to test connection
|
| 63 |
-
if st.button("Test Connection"):
|
| 64 |
-
if username and password:
|
| 65 |
-
user_key = get_user_key()
|
| 66 |
-
if isinstance(user_key, str) and user_key.startswith("❌"):
|
| 67 |
-
st.write(user_key) # Display error if user key retrieval failed
|
| 68 |
-
else:
|
| 69 |
-
result = test_connection(user_key, password)
|
| 70 |
-
st.write(result)
|
| 71 |
-
else:
|
| 72 |
-
st.write("⚠️ Please enter both username and password.")
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
import pyodbc
|
| 3 |
|
| 4 |
+
st.title("ODBC Driver Test")
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
try:
|
| 7 |
+
drivers = pyodbc.drivers()
|
| 8 |
+
st.write("✅ Available ODBC Drivers:", drivers)
|
| 9 |
+
except Exception as e:
|
| 10 |
+
st.write(f"❌ Error fetching ODBC drivers: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|