Python Forum

Full Version: Using pyodbc&pandas to load a Table data to df
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Greetings!
I'm connecting to a DB and can get all the tables from it. No errors
I wanted to find a table named "ToolHistory" and later some other tables and then use pandas DF and filter some rows from the table/tables.
Here is what I got so far:
conn = pyodbc.connect('DRIVER={SQL Server};SERVER=SomeServer.com,1433;DATABASE=QQS;UID=me;PWD=allgetlost')
cursor = conn.cursor()
for row in cursor.tables():
    print(f" >> {row.table_name}")
    sql_query = 'SELECT * FROM ToolHistory'
    df = pd.read_sql(sql_query, conn)
    # tb_one = row.table_name
    # if tb_one == 'ToolHistory' :
        # print('Found Table ->',tb_one)    
        #df = pd.read_sql_table('tb_one', conn)
        #df = pd.read_sql_table('tb_one', cursor)
I cannot pass the line after the
print(f" >> {row.table_name}")
Any help is appreciated.
Thank you.
To what type of database are you trying to connect with pyodbc?
(name + extension) - version
Paul
From what I understand it is a Microsoft SQL Server.
I have no problem connecting to it and getting all the table names from it.
I found out how to get data from a table.

sql_query = 'SELECT * FROM ToolHistory'
df = pd.read_sql(sql_query, conn)
print(df)
thank you.