首页 \ 问答 \ SQLite - UPSERT *不* INSERT或REPLACE(SQLite - UPSERT *not* INSERT or REPLACE)

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:

  1. Create the object using sqlite3_prepare_v2()
  2. Bind values to host parameters using sqlite3_bind_ interfaces.
  3. Run the SQL by calling sqlite3_step()
  4. Reset the statement using sqlite3_reset() then go back to step 2 and repeat.
  5. 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
更新时间:2022-10-31 10:10

最满意答案

这个答案现在有三个不同的部分。

虽然using语句方法除了源代码中的一个额外行之外没有任何开销,但包装器方法会以多种方式产生开销。

  1. System.Math有很多方法。 准确地再现包装纸充其量是乏味的。
  2. 运行时的性能开销可能从无关紧要到严重,具体取决于JIT如何在内部处理对数学函数的调用。 这可能因实现和平台而异。
  3. 您的类的编译代码将更大。
  4. 更难理解数学类中的哪些功能是新功能,而这只是已经存在的东西的包装器。

通过使用不同的策略可以防止上述所有情况。


通常的做法是故意避免名称冲突,我命名新类NameEx ,其中Name是原始名称。 例如,您会对创建MathExMathEx


您还可以使用以下内容来处理冲突的名称。

using Math = Yushatak.Libraries.Math;
using SystemMath = System.Math;

这在Visual Studio扩展开发中经常发生,特别是对于名为ConstantsIServiceProvider类。


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.

  1. There are a very large number of methods in System.Math. Accurately reproducing the wrappers would be tedious at best.
  2. 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.
  3. The compiled code for your class will be larger.
  4. 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, where Name is the original name. For example, you would be interested in creating the class MathEx.


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 and IServiceProvider.

相关问答

更多
  • 通过晦涩的安全将把你的钱埋在一棵树下。 唯一使它安全的事情是没有人知道它在那里。 真正的安全是把它放在一个锁或组合,说在一个安全。 你可以把保险箱放在街角,因为什么使它安全是没有人可以在里面,但你 。 正如@ 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 。 但是你一次就这样做,并仔细考虑,并确保提供正确的副本,分配和销毁语 ...
  • 不是真的,但两种(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 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)