Python Forum
Using pyodbc&pandas to load a Table data to df
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using pyodbc&pandas to load a Table data to df
#1
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.
Reply
#2
To what type of database are you trying to connect with pyodbc?
(name + extension) - version
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#3
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.
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Load data in Oracle muzokh 0 259 Mar-08-2024, 11:19 PM
Last Post: muzokh
  Better python library to create ER Diagram by using pandas data frames as tables klllmmm 0 1,145 Oct-19-2023, 01:01 PM
Last Post: klllmmm
  Trying to get counts/sum/percentages from pandas similar to pivot table cubangt 6 1,420 Oct-06-2023, 04:32 PM
Last Post: cubangt
  python pandas sql table with header mg24 3 1,971 Dec-08-2022, 08:31 PM
Last Post: Larz60+
  Load multiple Jason data in one Data Frame vijays3 6 1,560 Aug-12-2022, 05:17 PM
Last Post: vijays3
  panda table data kucingkembar 0 1,132 Mar-01-2022, 10:38 PM
Last Post: kucingkembar
  Sorting table data Blacktime2 1 1,336 Feb-26-2022, 07:05 PM
Last Post: ibreeden
  pyodbc gmerritt 8 2,925 Feb-21-2022, 07:21 PM
Last Post: gmerritt
  Move a particular row in pandas data frame to last row klllmmm 0 3,776 Dec-27-2021, 09:11 AM
Last Post: klllmmm
  Strategy on updating edits back to data table and object variables hammer 0 1,205 Dec-11-2021, 02:58 PM
Last Post: hammer

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020