Fail to insert data into mysql table using python script

Question:

i’m trying to insert data from my python script into my sql table. The script run with no error, however when i check in the table no data actually inserted. I have search around the internet and still couldn’t figure out what is the problem. Any help would be very appreciated. Code below is what i’ve tried so far.

 ts = time.time()
    timestamp = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
    try:
        mydb = mysql.connector.connect(host="localhost", user="root", passwd="", database="dummy_monke")
        mycursor = mydb.cursor()  # to point at database table
        queries = "INSERT INTO monitoring(id,lp,time) values (%s, %s, %s)"
        mycursor.execute(queries,spliced, timestamp)
        mydb.commit()
        print(mycursor.rowcount, "record updated successfully")

    except:
        mydb.rollback()
        print("record fail to update")
Asked By: mandebo

||

Answers:

Based on this post, you can do

queries = "INSERT INTO monitoring(lp,time) values (%s, %s)"
mycursor.execute(queries, (spliced, timestamp))

to generate a tuple on the fly and have the auto-increment done for you.

Side note: it may make sense to rename queries to a singular query.

Answered By: t_krill
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.