Failed processing format-parameters with mysql.connector in Python

后端 未结 1 917
闹比i
闹比i 2021-01-06 00:05

I can\'t figure out what I\'m doing wrong with this insert statement. The error I\'m getting is:

 \"Failed processing format-parameters; %s\" % err)
mysql.co         


        
相关标签:
1条回答
  • 2021-01-06 01:01

    The first option is the correct way to put query parameters into the query - it is called a parameterized query. In this case, you are letting the database driver to escape the query parameters, safely insert them into the query and handle the Python-to-MySQL type conversions.

    The error you are getting means that it could not convert one of the ID, Record, Latitude, Longitude or code parameter values to a valid MySQL database type. To be specific, see the variable types you have posted:

    ID        <type 'unicode'> 
    Record    <type 'unicode'>
    Latitude  <class 'bs4.element.NavigableString'>
    Longitude <class 'bs4.element.NavigableString'>
    code      <type 'unicode'>
    

    The problem is with Latitude and Longitude - they are BeautifulSoup's NavigableString class instances - the MySQL converter having difficulties in understanding how to convert a NavigableString object into a valid MySQL type. Convert them to strings explicitly beforehand:

    update = """
        INSERT INTO 
            myDB.newtable 
            (ID,Record,Latitude,Longitude,code) 
        VALUES 
            (%s,%s,%s,%s,%s)
    """
    cursor2.execute(update, (ID, Record, str(Latitude), str(Longitude), code))
    
    0 讨论(0)
提交回复
热议问题