2017-11-13 12 views
0

準備されたステートメントを使用してデータベースにデータを挿入しようとしています。データベース内のすべての私の列はVARCHARですが、PyMySQLは次のエラーを与える:PyMySQL:データpeparedステートメントの挿入

Not all arguments converted during string formatting 

すべての変数は、XMLファイルからのchildNodesを解析されます。変数の例:

try: 
    description1 = product.getElementsByTagName('description')[0] 
    description = description1.childNodes[0].data 
except IndexError: 
    print('No description') 
    description = 'None' 

準備されたステートメントなしでデータを挿入すると、正常に動作します。しかし、私はエスケープ文字のために準備されたステートメントを使用したい。私が間違って何が起こっていたかを見

 sql = """INSERT INTO Studystore(daisycon_unique_id, title, author, isbn, price, link, image_location, category, product_condition, description, in_stock, in_stock_amount, price_shipping, language, book_edition, number_of_pages, status, insert_date, update_date) 
      VALUES ('%s')""" 

     args = (str(daisycon_unique_id), str(title), str(author), str(isbn), str(price), str(link), str(image_location), str(category), str(product_condition), str(description), str(in_stock), str(in_stock_amount), str(price_shipping), str(language), str(book_edition), str(number_of_pages), str(status), str(insert_date), str(update_date),) 

     try: 

      # Execute the SQL command 
      cursor.execute(sql, args) 
      # Commit your changes in the database 
      db.commit() 

     except Exception as e: 
      print(e) 
) 

答えて

0

これは私のプリペアドステートメントのコードです。 これは作業コードです。

# Prepare SQL query to INSERT a record into the database. 
    sql = """INSERT INTO Studystore(daisycon_unique_id, title, author, isbn, price, link, image_location, category, product_condition, description, in_stock, in_stock_amount, price_shipping, language, book_edition, number_of_pages, status, insert_date, update_date) 
     VALUES ("%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s")""" 

    args = (daisycon_unique_id, title, author, isbn, price, link, image_location, category, product_condition, description, in_stock, in_stock_amount, price_shipping, language, book_edition, number_of_pages, status, insert_date, update_date,) 

    try: 

     # Execute the SQL command 
     cursor.execute(sql, args) 
     # Commit your changes in the database 
     db.commit() 

    except Exception as e: 
     print(e) 
+0

プレースホルダの値を引用符で囲む必要はありません。それは通常、運転手が気をつけている。 – tadman

関連する問題