0

I'm a newbie to python and i am trying to insert some data in to a mysql table .Seems the query executed with out any issues, however i don't see any record added on to the table.

Any help would be greatly appreciated.

Cheers, Aditya

    connection = mysql.connector.connect(user='sandboxbeta2503', password='XXX',
                                       host='myreleasebox.com',
                                       database='iaas')
            print ("Updating the history in bulk_notification_history")
            cursor = connection.cursor()

            timestamp = time.strftime("%Y-%m-%d %X")
            notification_type = "Notify Inactive users"
            usercount= 45
            query = ("INSERT INTO iaas.bulk_notification_history"
                "(nty_date,notification_type,user_count)"
                "VALUES (%s,%s,%s)")             
           data = (time.strftime('%Y-%m-%d %H:%M:%S'),notification_type, usercount)  
           linker1 = cursor.execute(query,data)   
           print (linker1) 
           cursor.close()
           connection.close() 
5
  • seems that there is an issue in INSERT query.. please check the syntax after VALUES clause Nov 18, 2015 at 11:57
  • I have ran the same set of commands on the python interactive terminal >>> cursor = connection.cursor() >>> timestamp = time.strftime("%Y-%m-%d %X") >>> notification_type = "Notify Inactive users" >>> usercount = 45 >>> query = "INSERT INTO iaas.bulk_notification_history (nty_date,notification_type,user_count) VALUES ('%s','%s',%d)" %(timestamp,notification_type,usercount) >>>linker1 = cursor.execute(query) >>> linker1 >>> print (linker1) None Nov 18, 2015 at 12:06
  • @bluto I did tried the same code on python console seems it returned None with out any syntax errors Nov 18, 2015 at 12:12
  • 1
    Never use string formatting to create sql queries!!! use cursor.execute(query, parameters).
    – mata
    Nov 18, 2015 at 12:54
  • Seems this has issues again " mysql.connector.errors.ProgrammingError: Not all parameters were used in the SQL statement" query = ("""INSERT INTO iaas.bulk_notification_history " "(nty_date,notification_type,user_count)" "VALUES (%s, %s, %d)""") data = (timestamp, notification_type, usercount) print (query) linker1 = cursor.execute(query,data) print (linker1) Nov 18, 2015 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.