1

I am attempting to insert a Date, Time, and Temperature value from sensors I have installed in the field. I have all the computations and variables working as intended but the sql statement fails. Here is the a rough format of how the argument is formatted:

  arg=('2019-07-21', '07:00:00', 29.323330729166656)

My SQL table is formatted as follows:

Date (as DATE), Time (as TIME), Temperature (as DOUBLE)

However, the insert into statement fails due to the standard 1064 error in MariaDB. " (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ':00:00,29.323330729166656)' at line 1")"

Here is the SQL statement:

for row in arg:
        print(row)
        cursor.execute("INSERT INTO flirtest (Date, Time, Temperature) VALUES (%s,%s,%s)" % row)
db.commit()

I think this error is due to the quotes around the first two variables. But I am honestly not sure. How should these statements be properly formatted in python? Should quotes be included or not?

1
  • Do you need a trailing comma in that array? Commented Aug 5, 2019 at 23:32

2 Answers 2

1

1. in your case, arg is a Tuple, so it goes thorugh 3 times: in the first round, row is '2019-07-21', in the secound round row is '07:00:00', you get it.

Just put "[ ]" around it to set it to a list, then there is only 1 round with everything :)

2. Yours should then work too, but try this:

arg=[('2019-07-21', '07:00:00', 29.323330729166656)]

for row in arg:
        print(row)
        cursor.execute("INSERT INTO flirtest (Date, Time, Temperature) VALUES (%s,%s,%s)", row)
db.commit()

But im not 100% sure, because I use mysql :)

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

1 Comment

Thank you! The extra brackets was the fix!
0

I don't know python, but I think this will be closer:

arg=('2019-07-21', '07:00:00', 29.323330729166656,)
cursor.execute("INSERT INTO flirtest (Date, Time, Temperature) VALUES (%s,%s,%s)", %row)
db.commit()

Comments

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.