Using a variable for a table name with python sql cursor

后端 未结 2 1773
天命终不由人
天命终不由人 2021-01-25 15:57

I am using python sql cursor to dynamically access my database and I am in a situation where I want to use a variable in place of a table name. So far all of my attempts have r

2条回答
  •  逝去的感伤
    2021-01-25 16:32

    You cannot dynamically bind object names, only values. You'll have to resort to string manipulation for the table's name. E.g.:

    sql = "INSERT INTO {} (word=%s,item_id=%s,word_tag=%s,unstemmed_word=%s, word_position=%s, TF=%s, normalized_term_frequency=%s, sentence=%s,anthology_id=%s)".format(table_name)
    
    cursor.execute(sql % (stemmedWord,fle.split()[0], str(word[1]), uniqeWord, word_pos, TF, normalized_term_frequency, sentence, fle.split()[1].split(".")[0]))
    

提交回复
热议问题