![]() |
Using pyodbc&pandas to load a Table data to df - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Using pyodbc&pandas to load a Table data to df (/thread-40697.html) |
Using pyodbc&pandas to load a Table data to df - tester_V - Sep-09-2023 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. RE: Using pyodbc&pandas to load a Table data to df - DPaul - Sep-09-2023 To what type of database are you trying to connect with pyodbc? (name + extension) - version Paul RE: Using pyodbc&pandas to load a Table data to df - tester_V - Sep-09-2023 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. RE: Using pyodbc&pandas to load a Table data to df - tester_V - Sep-09-2023 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. |