1

where I am doing wrong? query runs successfully but no data store in database table

My block of code is

import pymysql                                                                                         
db = pymysql.connect(host='', user='', passwd='',db='')                     
cursor = db.cursor()                                                                                   
cursor.executemany("""INSERT INTO Mention ( keyword, required, optional, excluded)                     
      VALUES (%s,%s,%s,%s)""",                                                                         
     [                                                                                                 
     ("Spam","fg","gtg","uu") ,                                                                        
     ("dd","fg","gtg","uu") ,                                                                          
     ("dd","fg","gtg","uu")                                                                            
     ])                                                                                                
print(cursor.fetchmany())   

enter image description here

2
  • did you get any error? you says "no store data" because the print not return nothing or why?. I runned your code and the data is stored, but the print not returns nothing Commented Aug 10, 2018 at 4:46
  • I don't think this has anything to do with SQL injection. You appear to be using the parameters properly. But you didn't commit the transaction, and pymysql does not commit implicitly. Commented Aug 10, 2018 at 15:35

1 Answer 1

0

It is advised to pass the parameters into the execute function instead because they will be escaped automatically.

Try this:

insert_stmt = (
  "INSERT INTO Mention ( keyword, required, optional, excluded)                     
      VALUES (%s,%s,%s,%s)"
)
data = [                                                                                                 
     ("Spam","fg","gtg","uu") ,                                                                        
     ("dd","fg","gtg","uu") ,                                                                          
     ("dd","fg","gtg","uu")                                                                            
     ]
cursor.execute(insert_stmt, data)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.