1
#endcding=utf-8
import MySQLdb
conn = MySQLdb.connect{
    host='localhost'
    port=3306,   
    user='root', 
    passwd='admin
    db='db01',   
    charset='utf8
    )            
cur = conn.cursor()
count = cur.execute("select * from t_r_def_audit")
results = cur.fetchmany(count)
provcode = cur.execute("select * from t_r_params where 
param_tag='PROVINCE_CODE' and param_code not in(1,95,99)")
provResults = cur.fetchmany(provcode)
i = 0
sql = "insert into ti_r_audit values({0},{1},{2},{3})"
values = "["
for result in results:
i = i+1
prev = result[8]
audit_id = result[0]
if(prev == "1"):
    prov = 31
    for index in range(prov):
        values =values + "("+str(i)+","+audit_id+",'"+provResults[index]
     [0]+"',0),"
        i = i+1
elif prev=="0":
    values =values + "("+str(i)+","+audit_id+",'"+provResults[31][0]+"',0),"
values = values + "]"
cur.executemany(sql,values)
cur.close
conn.commit()
conn.close()

Manually stitched the parameters of sql, with executableemany implementation of sql, but after the implementation of the script will be reported The error is as follows

λ python audit.py
Traceback (most recent call last):
File "audit.py", line 34, in <module>
cur.executemany(sql,values)
File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 255, in 
 executemany
self.errorhandler(self, TypeError, msg)
File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 36, in 
 defaulterrorhandler
raise errorclass, errorvalue
TypeError: not all arguments converted during string formatting

Write a manual parameters of their own and found no mistakes, indicating that the script should be no problem, the problem may be in the splicing sql parameters, but did not find where there is a problem

1
  • 1
    The code you have posted is clearly broken; look at the syntax highlight. Please cut it down to a minimal reproducible example Commented Aug 30, 2017 at 12:53

1 Answer 1

1

Your code is a mess, but the line

sql = "insert into ti_r_audit values({0},{1},{2},{3})"

needs to eventually be called as something along the lines of

sql.format('first', 'second', 'third', 'forth')

so ensure your list values contains 4 values when you call:

cur.executemany(sql,values)

otherwise you will see the error:

TypeError: not all arguments converted during string formatting

Update:

For debugging, you can insert the line:

print(sql.format(*values))

before you run the line

cur.executemany(sql,values)
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry, the code is incomplete. My parameters are part of the splicing, values ​​is part of the parameters, I print out, it is indeed four parameters per line
Check my update and make sure that all of your SQL statements are intact
Or being given,Print out the amount “insert into ti_r_audit values([,(,1,,)”

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.