SQLite - UPSERT *不* INSERT或REPLACE(SQLite - UPSERT *not* INSERT or REPLACE)
http://en.wikipedia.org/wiki/Upsert
在SQL Server上插入更新存储的proc
在SQLite中有没有一些聪明的方法,我没有想到?
基本上,如果记录存在,我想更新四列中的三个,如果不存在,我想使用第四列的默认(NUL)值插入记录。
该ID是一个主要的关键,所以UPSERT只会有一个记录。
(我试图避免SELECT的开销,以确定是否需要UPDATE或INSERT显然)
建议?
http://en.wikipedia.org/wiki/Upsert
Insert Update stored proc on SQL Server
Is there some clever way to do this in SQLite that I have not thought of?
Basically I want to update three out of four columns if the record exists, If it does not exists I want to INSERT the record with the default (NUL) value for the fourth column.
The ID is a primary key so there will only ever be one record to UPSERT.
(I am trying to avoid the overhead of SELECT in order to determin if I need to UPDATE or INSERT obviously)
Suggestions?
I cannot confirm that Syntax on the SQLite site for TABLE CREATE. I have not built a demo to test it, but It doesnt seem to be supported..
If it was, I have three columns so it would actually look like:
CREATE TABLE table1( id INTEGER PRIMARY KEY ON CONFLICT REPLACE, Blob1 BLOB ON CONFLICT REPLACE, Blob2 BLOB ON CONFLICT REPLACE, Blob3 BLOB );
but the first two blobs will not cause a conflict, only the ID would So I asusme Blob1 and Blob2 would not be replaced (as desired)
UPDATEs in SQLite when binding data are a complete transaction, meaning Each sent row to be updated requires: Prepare/Bind/Step/Finalize statements unlike the INSERT which allows the use of the reset function
The life of a statement object goes something like this:
- Create the object using sqlite3_prepare_v2()
- Bind values to host parameters using sqlite3_bind_ interfaces.
- Run the SQL by calling sqlite3_step()
- Reset the statement using sqlite3_reset() then go back to step 2 and repeat.
- Destroy the statement object using sqlite3_finalize().
UPDATE I am guessing is slow compared to INSERT, but how does it compare to SELECT using the Primary key?
Perhaps I should use the select to read the 4th column (Blob3) and then use REPLACE to write a new record blending the original 4th Column with the new data for the first 3 columns?
原文:https://stackoverflow.com/questions/418898
最满意答案
这个答案现在有三个不同的部分。
虽然
using
语句方法除了源代码中的一个额外行之外没有任何开销,但包装器方法会以多种方式产生开销。
System.Math
有很多方法。 准确地再现包装纸充其量是乏味的。- 运行时的性能开销可能从无关紧要到严重,具体取决于JIT如何在内部处理对数学函数的调用。 这可能因实现和平台而异。
- 您的类的编译代码将更大。
- 更难理解数学类中的哪些功能是新功能,而这只是已经存在的东西的包装器。
通过使用不同的策略可以防止上述所有情况。
通常的做法是故意避免名称冲突,我命名新类
NameEx
,其中Name
是原始名称。 例如,您会对创建MathEx
类MathEx
。
您还可以使用以下内容来处理冲突的名称。
using Math = Yushatak.Libraries.Math; using SystemMath = System.Math;
这在Visual Studio扩展开发中经常发生,特别是对于名为
Constants
和IServiceProvider
类。This answer now has three distinct parts.
While the
using
statement approach does not have any overhead aside from one extra line in your source code, the wrapper methods would have overhead in a number of ways.
- There are a very large number of methods in
System.Math
. Accurately reproducing the wrappers would be tedious at best.- The performance overhead at runtime could range from insignificant to severe, depending on how the JIT handles calls to math functions internally. This could vary by both implementation and platform.
- The compiled code for your class will be larger.
- It will be harder to understand which functionality in your math class is new functionality, and which is simply a wrapper around something that already existed.
All of the above can be prevented by using a different strategy.
It is fairly common practice to intentionally avoid the name conflict my naming the new class
NameEx
, whereName
is the original name. For example, you would be interested in creating the classMathEx
.
You can also use something like the following to handle conflicting names.
using Math = Yushatak.Libraries.Math; using SystemMath = System.Math;
This frequently happens in Visual Studio extension development, in particular with classes named
Constants
andIServiceProvider
.
相关问答
更多-
通过晦涩的安全将把你的钱埋在一棵树下。 唯一使它安全的事情是没有人知道它在那里。 真正的安全是把它放在一个锁或组合,说在一个安全。 你可以把保险箱放在街角,因为什么使它安全是没有人可以在里面,但你 。 正如@ ThomasPadron-McCarty所提到的,下面的评论如下: 如果有人发现密码,您可以更改密码,这很简单。 如果有人找到位置,你需要挖掘钱,并将其移动到别的地方,这是更多的工作。 如果您在程序中使用安全性,则必须重写该程序。 Security through obscurity would be ...
-
这个答案现在有三个不同的部分。 虽然using语句方法除了源代码中的一个额外行之外没有任何开销,但包装器方法会以多种方式产生开销。 System.Math有很多方法。 准确地再现包装纸充其量是乏味的。 运行时的性能开销可能从无关紧要到严重,具体取决于JIT如何在内部处理对数学函数的调用。 这可能因实现和平台而异。 您的类的编译代码将更大。 更难理解数学类中的哪些功能是新功能,而这只是已经存在的东西的包装器。 通过使用不同的策略可以防止上述所有情况。 通常的做法是故意避免名称冲突,我命名新类NameEx ,其 ...
-
我实际上和你有过相同的经历。 我使用了与你非常相似的方法。 我把我的所有活动都转到了我的路由器PHP文件,然后从那里我将解析URL并调用需要调用的控制器。 另外,要调用我的页面,我只需要执行一个include ,这不会影响性能。 该系统似乎适用于路由。 总而言之,如果您想扩展网站,我会鼓励您真正查看一个框架。 我不是说你不能用你所得到的东西来扩展,但是道路会更长。 这只是因为你将不得不做或重复框架正在做的事情。 我推荐一个轻量级的框架,比如Yii Framework。 在你承诺之前,我会鼓励你稍微玩一下,看 ...
-
非常非常糟糕的主意。 不要将翻译与视图代码混合。 真的,不要这样做。 Very very bad idea. Do not mix translations with view code. Really, don't do it.
-
结果发现我的框架有问题,所以我不得不删除它们并从教师计算机上复制/粘贴新的框架。 这个问题在框架重新添加后立即解决。 Turns out my frameworks were bugged, so I had to delete them and copy/paste new ones from my Instructor's computer. The issue was fixed as soon as the frameworks were re-added.
-
看起来完全合理:stacktrace包含RpcProvider调用方法的位置的详细信息,并隐藏血腥和不必要的反射gubbins,所以你很好。 与所有错误处理代码一样,最终的消费者将是其他开发人员,因此最好问一下“如果出现问题,我是否有足够的细节来自行调试?” 由于没有那么多的噪音,好像抛出了外部异常,这种技术很好地完成了它的工作。 Looks completely reasonable: the stacktrace includes details of where the RpcProvider is ...
-
重点是new ,就像怀孕一样,创建一个手动管理的资源(即由你来管理),因此它带有责任 。 C ++是图书馆写作的语言,每当你看到责任时,“C ++”方法就是编写一个处理这个问题的库元素,只有这个责任。 对于动态内存分配,这些库组件已经存在并被简称为“智能指针”; 你会想看看std::unique_ptr和std::shared_ptr (或者它们的TR1或Boost等价物)。 在编写这些单一责任构建块时,您确实需要说new和delete 。 但是你一次就这样做,并仔细考虑,并确保提供正确的副本,分配和销毁语 ...
-
以同样的方式学习Ruby和Python是一个坏主意吗?(Is it a bad idea to learn Ruby and Python simutaneusly? [closed])[2021-09-14]
不是真的,但两种(Python和Ruby)语言在某种程度上是相似的(脚本语言,动态类型,强大的对象和类系统)。 学习一种非常不同的函数语言,比如Haskell或Ocaml(除了Ruby或Python)将会让你更开心。 Not really, but both (Python & Ruby) languages are somehow similar (scripting languages, dynamically typed, with powerful object & class systems). ... -
编写自己的通用Max和Min public static T Max
(T x, T y) { return (Comparer .Default.Compare(x, y) > 0) ? x : y; } Write your own generic Max and Min public static T Max (T x, T y) { return (Comparer .Default.Compare(x, y) > 0) ? x : y; } -
好吧,对我来说似乎没问题。 Eric Lippert之前曾写过关于将“ Dispose用于“非资源”场景的危险,但我认为这将被视为一种资源。 它可能会使升级方案中的生活变得棘手,但在此时您可能总是会回到更加手动的代码位。 另一种方法是编写单个锁获取/使用/释放方法,并提供在将锁作为委托时执行的操作。 Well, it seems okay to me. Eric Lippert has previously written about the dangers of using Dispose for "no ...