fix my code: # These lines are useful to have in development try: Retrieve referenced TI…
- User
fix my code: # These lines are useful to have in development try: # Retrieve referenced TI inquiry_ids_str = ",".join(["'{}'".format(id) for id in recommended_df.inquiry_id.tolist()]) closed_inq = "SELECT inquiry_id, searchable FROM
i2ms-debug.inquiry_analytics_vw i WHERE i.inquiry_id IN({inquiry_ids_str})" mydb = connection.connect(**mydbconfig) mycursor = mydb.cursor() mycursor.execute(closed_inq) closed_inq = mycursor.fetchall() closed_inq = pd.DataFrame( closed_inq, columns=["inquiry_id", "searchable"])codeexcept Exception as e: print( { "error": { "message": "An unexpected error occurred.", "details": str(e), "code": 500, } } - Assistant
The code appears to be missing a closing parenthesis and a closing curly brace. Here is the corrected code:
codetry: inquiry_ids_str = ",".join(["'{}'".format(id) for id in recommended_df.inquiry_id.tolist()]) closed_inq = "SELECT inquiry_id, searchable FROM `i2ms-debug`.inquiry_analytics_vw i WHERE i.inquiry_id IN({inquiry_ids_str})" mydb = connection.connect(**mydbconfig) mycursor = mydb.cursor() mycursor.execute(closed_inq) closed_inq = mycursor.fetchall() closed_inq = pd.DataFrame( closed_inq, columns=["inquiry_id", "searchable"]) except Exception as e: print( { "error": { "message": "An unexpected error occurred.", "details": str(e), "code": 500, } } ) - User
inquiry_ids_str = “,”.join([“‘{}’”.format(id) this is wrong. i need id to be those from recommended_df.inquiry_id.tolist()
- Assistant
Apologies for the mistake. To fix it, replace the line
inquiry_ids_str = ",".join(["'{}'".format(id) for id in recommended_df.inquiry_id.tolist()])with the following line:pythoninquiry_ids_str = ",".join(["'{}'".format(id) for id in recommended_df["inquiry_id"].tolist()])This will correctly retrieve the
inquiry_idvalues from therecommended_dfdataframe. - User
': {'message': 'An unexpected error occurred.', 'details': "1064 (42000): 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 '})' at line 1", 'code': 500}}
- Assistant
The error message suggests that there is an issue with the SQL syntax near the line
inquiry_ids_strin the SQL query. To fix this, make the following modification:Replace this line:
codeclosed_inq = "SELECT inquiry_id, searchable FROM `i2ms-debug`.inquiry_analytics_vw i WHERE i.inquiry_id IN({inquiry_ids_str})"With this line:
codeclosed_inq = "SELECT inquiry_id, searchable FROM `i2ms-debug`.inquiry_analytics_vw i WHERE i.inquiry_id IN ({})".format(inquiry_ids_str)By including the
.format(inquiry_ids_str)at the end of the query string, it properly substitutes theinquiry_ids_strvalue into the query. This should resolve the SQL syntax error.