Python mysql dynamic add column and update values in the new columns

懵懂的女人 提交于 2019-12-12 04:16:00

问题


for i in onlycolumns:
if i not in pat and i not in ref:
    new=lister.index(i)
    value1 = lister[new+1]
    query="ALTER TABLE test add %s varchar(30)" %i
    cursor.execute(query)

    query1 = """ UPDATE test
           SET % = %s
            WHERE patient_name = %s """
    data = (i,value1,value)
    cursor.execute(query1, data)     

The columns are added dynamically but the values are not updated in it ...any wrong with my update command......value variable here is the name of patient name ...it is right and the value in value1 is 'a'.pLEASE HELP ME ANYONE WITH THE UPDATE COMMAND


回答1:


The problema is with the SET: Use SET %s = %s.

You miss the the s near one %.



来源:https://stackoverflow.com/questions/35956991/python-mysql-dynamic-add-column-and-update-values-in-the-new-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!