最好的办法,以确保我没有将信息插入的sqlite的分贝是已经存在?最好的、分贝、存在、办法

2023-09-04 23:31:36 作者:孤帆去悠悠

我在做一个程序,让您可以从应用程序进行搜索的过程。该应用将然后下载搜索结果,并把它们存储在数据库中。我用的是URL作为主键,这样我可以确保我没有输入相同的数据两次(如URL是肯定会是一个唯一的标识符),然而,这引起了一些问题。试图挽救一个行具有相同的URL时,该数据库将给出一个错误和崩溃。我想这是很好的,因为我可以把我的INSERT语句在try / catch块。我有一些问题,但:

我应该怎么抓(try catch块)检测时,它正试图插入到数据库的项目已被存储发生这样的错误?

这正与一个简单的每个搜索结果一次添加到数据库1的方式。如果我想做一个数据库事务,并添加〜25的时间。我将如何找出哪一个被示数,而这将是做到这一点的最好方法是什么?有没有办法让它继续与补充说,没有问题的休息吗?

我怎么会去这样做,如果我不希望使用URL作为主键? 解决方案

您可以尝试使用插入或更换的 http://www.sqlite.org/lang_insert.html

加快Mac运行macOS 10.15 Catalina的Mac的10个最佳技巧

I'm in the process of making a program that allows you to perform searches from the app. The App will then download the search results and store them in the database. I was using the url as the primary key so that I could make sure that I'm not inputting the same data twice (As URLs are sure to be a unique identifier), however this caused a few issues. The database would give an error and crash when trying to save a row with the same URL. I suppose this is good since I can put my insert statement in a try/catch block. I have some questions though:

What should I be catching (try catch block) to detect when an error like this occurs where the item it's trying to insert into the db is already being stored?

This is working with a simple "add each search result to the DB 1 at a time" approach. What if I wanted to do a database transaction and add ~25 at a time. How would I figure out which one was erroring, and what would be the best way to do this? Is there a way for it to continue on with adding the rest that didn't have the issue?

How would I go about doing this if I didn't want to use the url as the primary key?

解决方案

You could try using INSERT OR REPLACE http://www.sqlite.org/lang_insert.html