2017-11-13 2 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