0

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")
5
  • What is the content of spliced? Commented Aug 25, 2022 at 5:08
  • hi, spliced variable contain a license plate characters ( string) Commented Aug 25, 2022 at 5:11
  • 1
    Your execute parameters do not match your substitutions. You have three substitutions, so you need a tuple with 3 items to pass to execute. You would have seen this if you had not included your blanket try/except. You should NEVER include try/except until you have the code working. Where does the id come from? Commented Aug 25, 2022 at 5:11
  • @TimRoberts the id is an auto increment attribute in the mysql table, do i need to include that in the INSERT statement? . Commented Aug 25, 2022 at 5:15
  • No, because you don't have a value for it. The answer below is correct. Commented Aug 25, 2022 at 5:24

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@ERENYEETGAR of course!

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.