import pyodbc def get_user(username) -> int: with pyodbc.connect("Server=(LocalDB)\Test; Integrated Security=true; Driver={ODBC Driver 17 for SQL Server}") as conn: with conn.execute("SELECT id FROM Users WHERE username=?", username) as cursor: rows = cursor.fetchall() if len(rows) <= 0: return return rows[0].id print(f"user ID: {get_user("EGS")}")
16 August 2025
Python - Execute a query with pyodbc
To work with a database via ODBC, first install the PyOdbc package and import it.
The following simple query shows how to execute a select query and return data:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment