简体   繁体   中英

How to insert a list of values into a mysql database using python?

I don't know how to correctly insert values into a mysql database.

I think it's a pretty simple fix but I'm new to mysql.

...

sqllist = "INSERT INTO station_fenelon (date, time, outsidetemp, outsidehumidity) VALUES (%s, %s, %f, %f)"

record = [('2019-06-21', '12:12:11', '414.44', '42.4')]

cursor.execute(sqllist, record)
sql.commit()

I get the error: Not all parameters were used in the SQL statement

I would recommend using an ORM (Object Relational Mapper) like SQLAlchemy. If you are writing to your database over a flask application, try using the flask_sqlalchemy extension to support your software. It's quite useful as it can be easily paired with the alembic library for database migrations.

If you use flask_sqlalchemy, then inserting into your database will become as simple as creating a new software object, and then adding it to your database session. Your database session I believe is a representation of your active connection, and adding to it is like writing to the log that is built on top of it. Then you would simply commit your changes to the log, and watch as they propagate into your database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM