最好的办法,以确保我没有将信息插入的sqlite的分贝是已经存在? [英] Best Way to make sure I'm not inserting information into the sqlite db that is already there?

查看:67
本文介绍了最好的办法,以确保我没有将信息插入的sqlite的分贝是已经存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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:

  1. 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?

  2. 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?

  3. 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

这篇关于最好的办法,以确保我没有将信息插入的sqlite的分贝是已经存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆