简体   繁体   English

如何使用python将值列表插入mysql数据库?

[英]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. 我不知道如何正确地将值插入mysql数据库。

I think it's a pretty simple fix but I'm new to mysql. 我认为这是一个非常简单的修复程序,但我是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 我收到错误:SQL语句中未使用所有参数

I would recommend using an ORM (Object Relational Mapper) like SQLAlchemy. 我建议使用像SQLAlchemy这样的ORM(对象关系映射器)。 If you are writing to your database over a flask application, try using the flask_sqlalchemy extension to support your software. 如果要通过Flask应用程序写入数据库,请尝试使用flask_sqlalchemy扩展来支持您的软件。 It's quite useful as it can be easily paired with the alembic library for database migrations. 它非常有用,因为它可以与Alembic库轻松配对以进行数据库迁移。

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. 如果使用flask_sqlalchemy,则插入数据库将变得很简单,就像创建一个新的软件对象,然后将其添加到数据库会话中一样。 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. 然后,您只需将更改提交到日志,并观察更改是否传播到数据库中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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