Python Forum
add database rows to dataframe
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
add database rows to dataframe
#4
I am sure Python gives you more information about the error than "pandas.io.sql.DatabaseEror: Execution failed on sql". It will tell you the exact line number in each module. I guess it will go wrong in "pd.read_sql_query()".
Best way is to add debug lines in your program to test whether variables contain what you expect.
First: somewhere in your program you must have logged in to the SQL Server database. Did that succeed? Can you test that with a simple query?
Second: just before executing the query: add statements to print the contents of the variables that matter.
        userid, rolename = row[['User Id', 'Role Name']]
        print("userid   = " + userid)
        print("rolename = " + rolename)
        Print("SQL      = " + SQL)
        sql_query = pd.read_sql_query((SQL, (userid, rolename)), conn)
What does it show?
Third: try to simplify the SQL. Do it without the variable substitution by SQL Server.
SQL = "select * from table_name where userid = 'John' and roleName = 'administrator'"
...
    sql_query = pd.read_sql_query(SQL, conn)
(First fill in the correct table_name, userid and rolename.) What is the result of that?
Reply


Messages In This Thread
add database rows to dataframe - by cspower - Jan-23-2020, 06:37 PM
RE: add database rows to dataframe - by ibreeden - Jan-24-2020, 12:54 PM
RE: add database rows to dataframe - by cspower - Jan-24-2020, 09:55 PM
RE: add database rows to dataframe - by ibreeden - Jan-25-2020, 09:45 AM
RE: add database rows to dataframe - by cspower - Jan-27-2020, 02:48 PM
RE: add database rows to dataframe - by cspower - Jan-27-2020, 07:25 PM
RE: add database rows to dataframe - by ibreeden - Jan-27-2020, 07:27 PM
RE: add database rows to dataframe - by cspower - Jan-27-2020, 09:28 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Rows not adding to sqlite3 database using SQLAlchemy Calab 11 2,110 Jun-02-2023, 05:53 PM
Last Post: bowlofred
  Converting a json file to a dataframe with rows and columns eyavuz21 13 5,466 Jan-29-2023, 03:59 PM
Last Post: eyavuz21
  How to assign a value to pandas dataframe column rows based on a condition klllmmm 0 988 Sep-08-2022, 06:32 AM
Last Post: klllmmm
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,714 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  Partial Matching Rows In Pandas DataFrame Query eddywinch82 1 2,500 Jul-08-2021, 06:32 PM
Last Post: eddywinch82
  Pandas DataFrame combine rows by column value, where Date Rows are NULL rhat398 0 2,214 May-04-2021, 10:51 PM
Last Post: rhat398
  Indexing [::-1] to Reverse ALL 2D Array Rows, ALL 3D, 4D Array Columns & Rows Python Jeremy7 8 7,610 Mar-02-2021, 01:54 AM
Last Post: Jeremy7
Question Dataframe Manipulation Coping Rows and Removing Dates ashleysnl 1 1,872 Feb-26-2021, 10:00 PM
Last Post: nilamo
  How to split dataframe object rows to columns Mekala 1 2,720 Nov-12-2020, 04:18 PM
Last Post: michael1789
  Adding loop results as rows in dataframe Shreya10o 2 2,330 May-09-2020, 11:00 AM
Last Post: Shreya10o

Forum Jump:

User Panel Messages

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