EEBB
EEBB

Reputation: 41

Not enough arguments for format string Python-MySQL

I'm getting this error in python and i don't know how to solve it. Sorry for my english, is not my native language :(

TypeError: not enough arguments for format string

save_data = "INSERT INTO mediciones_historial VALUES('%s', '%s', '%s', '%s')" % (sensor, data, data_type)

In my database I have five rows:

id (int) sensor (int) data (text) data_type(text) fecha(timestamp)

What is that I'm doing wrong?

Thanks!

Upvotes: 2

Views: 23257

Answers (2)

Samir Selia
Samir Selia

Reputation: 7065

This is because the number of values does not match the number of arguments.

save_data = "INSERT INTO mediciones_historial VALUES('%s', '%s', '%s', '%s')" % (sensor, data, data_type, fecha)

Upvotes: 3

coder3521
coder3521

Reputation: 2646

In your code:

save_data = "INSERT INTO mediciones_historial VALUES('%s', '%s', '%s', '%s')" % (sensor, data, data_type)

The number of %s specifier doesn't matches number of arguments you are passing, also the column names are missing after table name

you have to make it according to the column in table but python always have a better choice.

Try using this :

save_data = "INSERT INTO mediciones_historial(column1,column2,column3) VALUES({0},{1},{2})".format(sensor, data, data_type)

but you have to take care of data type i.e if it is string or date type then use single quotes(') : '{0}' instead of {0}.

Also the above seems more understandable and readable. :)

Upvotes: 3

Related Questions