Search code examples
pythonmysqlmysql-workbenchpymysql

how to change mysql concat code to pymysql code


this is my mysql code

select ID, GROUP_CONCAT(CONCAT('{R_ICODE:"', R_ICODE, '", RANK:"',RANK,'"}')) list from RESULT where USER_ID='ehae065@daum.net' group by ID;

and this is my pymysql code

cursor.execute("ID, GROUP_CONCAT(CONCAT('{R_ICODE:"', R_ICODE, '", RANK:"', RANK, '"}')) list from RESULT where USER_ID='ehae065@daum.net' group by ID")

when i ran the pymysql code i got the syntex error so if someones knows the right way plz teach me thank you


Solution

  • first, you missed your select statement, 2nd is you need to escape your double quotes.

    cursor.execute("SELECT ID
        , GROUP_CONCAT(CONCAT('{R_ICODE:\"', R_ICODE, '\", RANK:"', RANK, '\"}')) list 
        FROM RESULT WHERE USER_ID='ehae065@daum.net' group by ID")