我可以将 INSERT 语句拆分为多个语句而不重复插入行吗?
我有这样一条 INSERT 语句:
mtemp = "station, calendar, type, name, date, time"
query = "INSERT INTO table (%s) VALUES ( '%s', '%s', '%s', %s, '%s', '%s' );"
query = query % (mtemp, mstation, mcalendar, mtype, mname, mdate, mtime)
curs.execute(query, )
conn.commit()
问题是我无法获取该语句中的变量:mcalendar、mdate、mtime。它们不是恒定值。我必须在 forloop 中访问它们中的每一个。但是,mstation、mtype 和 mname 的值是固定的。我尝试将 INSERT 语句拆分为多个语句:一个用于 for 循环中的三个变量中的每一个,另一个用于单个 for 循环中的三个固定值。 for循环基本上是定义何时插入行。我有一个 rows1 列表和一个 rows2 列表,rows1 是完整的记录列表,而 rows2 缺少其中一些记录。我正在检查 rows2 记录是否存在于 rows1 中。如果是,则执行 INSERT 语句,如果不是,则不执行任何操作。
我运行代码并发现两个问题:
- 它插入的行数比预期的多。由于每个传感器每天只出现 240 次,因此应该插入不超过 240 行。 (我想知道是否是因为我写了太多的forloop以至于它不断插入行)。现在它增加了 400 多行新行。
- 在插入到表中的这些新行中,它们仅具有固定值列中的值。对于我使用单个for循环插入数据的三个,它们根本没有价值。
希望有人在这里给我一些提示。提前致谢!如果需要的话我可以在这里放置更多代码。我什至不确定我是否走在正确的轨道上。
I have such an INSERT statement:
mtemp = "station, calendar, type, name, date, time"
query = "INSERT INTO table (%s) VALUES ( '%s', '%s', '%s', %s, '%s', '%s' );"
query = query % (mtemp, mstation, mcalendar, mtype, mname, mdate, mtime)
curs.execute(query, )
conn.commit()
The problem is that I can not get the variables: mcalendar, mdate, mtime in this statement. They are not constant values. I would have to access each of them within a forloop. However, the values of mstation, mtype and mname are fixed. I tried to split the INSERT statement into several ones: one for each of the three variables in a forloop, and one for the three fixed values in a single forloop. The forloop is basically to define when to insert rows. I have a list of rows1 and a list of rows2, rows1 is a full list of records while rows2 lack some of them. I’m checking if the rows2 record exist in rows1. If it does, then execute the INSERT statement, if not, do nothing.
I ran the codes and found two problems:
- It’s inserting way more rows than it is supposed to. It’s supposed to insert no more than 240 rows for there are only 240 time occurrences in each day for each sensor. (I wonder if it is because I wrote too many forloop so that it keeps inserting rows). Now it’s getting more than 400 new rows.
- In these new rows being inserted to the table, they only have values in the columns of fixed value. For the three ones that I use the single forloop to insert data, they don’t have value at all.
Hope someone give me some tip here. Thanks in advance! I can put more codes here if needed. I’m not even sure if I’m in the right track.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不确定我是否完全理解您的情况,但这就是您需要的东西吗?
伪代码
I'm not sure I understand exactly your scenario, but is this the sort of thing you need?
Pseudo code
一条 INSERT 语句始终对应于表中的一个新行。 (当然,除非在插入过程中出现错误。)您可以插入一行,然后稍后更新它以添加/更改信息,但不存在拆分插入这样的事情。
如果您有一个查询需要多次执行并更改数据,那么最好的选择是 准备好的声明。准备好的语句“编译” SQL 查询,但留下可以在每次执行时设置的占位符。这提高了性能,因为不需要每次都解析语句。你没有指定你使用什么库来连接到 postgres,所以我不知道语法是什么,但它是值得研究的东西。
如果您不能/不想使用准备好的语句,则只需为每个插入创建一次查询字符串。不要替换循环之前的值,等到您知道所有值后再创建查询。
One INSERT statement always corresponds to one new row in a table. (Unless of course there is an error during the insert.) You can INSERT a row, and then UPDATE it later to add/change information but there is no such thing as splitting up an INSERT.
If you have a query which needs to be executed multiple times with changing data, the best option is a prepared statement. A prepared statement "compiles" an SQL query but leaves placeholders that can set each time it is executed. This improves performance because the statement doesn't need to be parsed each time. You didn't specify what library you're using to connect to postgres so I don't know what the syntax would be, but it's something to look in to.
If you can't/don't want to use prepared statements, you'll have to just create the query string once for each insert. Don't substitute the values in before the loop, wait until you know them all before creating the query.
以下语法适用于 SQL Server 2008,但不适用于 SQL Server 2005。
Following syntax works in SQL Server 2008 but not in SQL Server 2005.