简体   繁体   English

Python和MySQL插入查询

[英]Python and MySQL Insert query

I have a question regarding insert query and python mysql connection. 我有关于插入查询和python mysql连接的问题。 I guess that I need to commit after every insert query made 我想我需要在每次插入查询后提交

Is there a different way to do that? 有没有不同的方法呢? I mean a fast way like one in php. 我的意思是像PHP一样快速的方式。

Second this also is same for update query I guess ? 第二,这对于更新查询也是一样的我猜?

Another problem here is that once you commit your query connection is closed, assume that I have a different insert queries and every time i prepare it I need to insert it to the table. 这里的另一个问题是,一旦你提交了你的查询连接关闭,假设我有一个不同的插入查询,每次我准备它我需要将它插入到表中。 How can I achieve that with python. 我怎么能用python实现这一点。 I am using MySQLdb Library 我正在使用MySQLdb Library

Thanks for your answers. 谢谢你的回答。

You don't need to commit after each insert. 每次插入后都不需要提交。 You can perform many operations and commit on completion. 您可以执行许多操作并在完成时提交。

The executemany method of the DBAPI allows you to perform many inserts/updates in a single roundtrip DBAPI的executemany方法允许您在单个往返中执行许多插入/更新

There is no link between committing a transaction and disconnecting from the database. 提交事务和断开数据库之间没有联系。 See the Connection objects methods for the details of the commit and close methods 有关commitclose方法的详细信息,请参阅Connection对象方法

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

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