0

I get the error not all arguments converted during string formatting, when I execute the below-given code:

pro_title = "FSBT"
print "pro_title: " + pro_title
pro_id_query = "SELECT ID FROM projs WHERE pro_title=%s"
cursor.execute(pro_id_query, pro_title)
db.commit()
row = cursor.fetchone()
pro_id = None
if row is not None:
   pro_id = str(row[0])    
print "pro_id: " + pro_id

I also tried format:

pro_id_query = "SELECT ID FROM projs WHERE title={}"
cursor.execute(pro_id_query.format(pro_title))

It only works when I use ' around {}:

pro_id_query = "SELECT ID FROM projs WHERE title='{}'"
cursor.execute(pro_id_query.format(pro_title))

I do not understand why INSERT queries work well with %s, while SELECT queries do not:

insert_query = "INSERT INTO projs (title, description) VALUES (%s, %s) ON DUPLICATE KEY UPDATE `title`=%s"
cursor.execute(insert_query, (pro_title, pro_description, pro_title))
2
  • Try pro_id_query = "SELECT ID FROM projs WHERE pro_title=?" cursor.execute(pro_id_query, (pro_title,)); Commented Feb 4, 2018 at 13:38
  • @angel.bonev: I get the error not all arguments converted during string formatting Commented Feb 4, 2018 at 15:22

1 Answer 1

1
pro_title = "FSBI" 
pro_id_query = "SELECT * FROM %s"%(pro_title)
cursor = con.cursor() 
cursor.execute(q) 
result_list = result.fetchall()   
result_list[0][0] 
con.commit() 
con.close()

pro_id_query = cursor.fetchone() while row != False:
print ("The ID is : ", row[0])

*edit

id = input("Id : ")
name = input("Name : ")
cursor = con.cursor()
cursor.execute(""" INSERT INTO names (id, name) VALUES("%s", "%s")"""
          %(id, name))
Sign up to request clarification or add additional context in comments.

4 Comments

If you assign the last line to a variable it does.
Which line? This one pro_id_query = "SELECT * FROM %s"%(pro_title)?
If you refer to this line, I get the error INSERT INTO projs (title, description) VALUES (TES, TES) ON DUPLICATE KEY UPDATE title=TES (1054, "Unknown column 'TES' in 'field list'")
I edited my answer with input aswell. But it does work. I dont know how you have made your table offcourse, then i need to make everything a variable to fix it so that you can put it in manualy. Im sure you can figure it out.

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.