0

I build a SQL statement in python that yields this result (a print of the "query" variable sent to the console during execution):

"INSERT INTO tbl_order_line (order_line,Item_Name,Item_Variant,SKU,Qty,Item_Price,Item_Weight,Item_Custom_Text,Qty_refunded,Qty_restocked) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"

I also build a variable that yields this for the data to be used in the INSERT (again a print to the console made during execution of the "parms" variable):

("10034","Lemons (3ea)","","PRODICE-25","2","1.4","0","","0","0")

I have verified that the table name and field names are correct and that the number of fields and parameters are 10 each.

The data elements are VARCHAR to eliminate any data type issues.

The error I receive is:

TypeError: not all arguments converted during bytes formatting.

The execute statement is:

c.execute(query, parms)

I have looked through several threads and have added a comma after the "parms" in the execute but get the same result.

Any guidance is appreciated.

2
  • What are the types of all 10 columns in your SQL table? Commented Jul 3, 2020 at 3:08
  • As noted all are VARCHAR - I changed them hoping to eliminate any data type issues Commented Jul 3, 2020 at 3:19

1 Answer 1

1

I can speculate here and suggest that maybe some other part of your prepared statement setup has an issue. Here is a valid way of doing what you are attempting:

conn = mysql.connector.connect(...)
c = conn.cursor(prepared=True)        # critical, turn on prepared statement mode here

sql = """INSERT INTO tbl_order_line (order_line, Item_Name, Item_Variant, SKU, Qty,
                                     Item_Price, Item_Weight, Item_Custom_Text,
                                     Qty_refunded, Qty_restocked)
         VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"""

parms = ("10034", "Lemons (3ea)", "", "PRODICE-25", "2", "1.4", "0", "", "0", "0")

c.execute(sql, parms)
c.commit()
Sign up to request clarification or add additional context in comments.

4 Comments

After some review I sorted that the connect did have an error (some old 2.7 code) and the conn.cursor(prepared=True) is now installed without error. I am building the query statement with field names pulled from a database so I can't "hardcode" the statement as above but am hoping the print to consolw which shows the contents of the query variable is an accurate representation of what is being passed. If it is correct then the only difference is the extra quotes which I added but got error:mysql.connector.errors.InterfaceError: You have an error in your SQL syntax;
I did comment out my code and paste in yours and it works - my problem is that I don't see a difference when I run my code and see the parameters in the console and the code you provided - could the fact that I create the field names from input rather than hardcoding them cause an issue?
I really don't know, I just tried to give you a canonical version of what you are doing.
Understood - thank you - back the the drawing board :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.