id5h
id5h

Reputation: 205

MariaDB query from python code

I'm writing a python script that connects to a MariaDB server (V 10.1.12) and saves to file the results of some queries. However, when sending the following query:

sql =   'SELECT * FROM Monitor_Run_Tracking WHERE Entry IN (SELECT MAX(Entry) ' \
        'FROM Monitor_Run_Tracking WHERE Run in ({0}) ' \
        "AND WhenEntered<'{1}' GROUP BY Run)".format( runstr, quality_date )

followed by cursor.execute( sql ), I get the following error:

File "build/bdist.linux-x86_64/egg/MySQLdb/cursors.py", line 174, in execute
File "build/bdist.linux-x86_64/egg/MySQLdb/connections.py", line 36, in defaulterrorhandler _mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ') AND WhenEntered<'2020-01-01' GROUP BY Run)' at line 1")

Could someone explain to me what the mistake is?

Thanks!

Upvotes: 0

Views: 1749

Answers (1)

Nee
Nee

Reputation: 586

You didn't close the parenthesis around the nested select. It should be

SELECT * FROM Monitor_Run_Tracking WHERE Entry IN (
        SELECT MAX(Entry)
        FROM Monitor_Run_Tracking WHERE Run in ({0})
        AND WhenEntered<'{1}')
GROUP BY Run

or

SELECT * FROM Monitor_Run_Tracking WHERE Entry IN (
        SELECT MAX(Entry)
        FROM Monitor_Run_Tracking WHERE Run in ({0})
        AND WhenEntered<'{1}'
        GROUP BY Run)

EDIT: For the other problem, you're grouping by a field that you're not selecting. Check out this question for help.

Upvotes: 1

Related Questions