简体   繁体   English

Android中的sqlite插入新行并重复一些值

[英]sqlite in Android insert new row with some values repeated

:I have this table: :我有这张桌子:

id            |      name          |      chapter      |    book

 1                  hamlet                  I               Hamlet

 2                  Ismael                  IV              Moby Dick

The behaviour I expect for new values is: 我期望新值的行为是:

Same name && same book => Character exists so Same id than the existing one: 同名&&同书=>字符存在,因此ID与现有ID相同:

 2                   Ismael                   X              Moby Dick

Same name && different book => Character doesn't exist so Changes the id: 同名&&不同书=>字符不存在,因此更改ID:

 3                   Ismael                  XX              The Bible     

My question is: 我的问题是:

Do I have to make the query before insert new values and then insert the new value? 在插入新值然后插入新值之前,是否必须进行查询?

or there is a way to do it in a automatic way by setting up a trigger or something? 还是有一种通过设置触发器或其他方式自动执行此操作的方法?

My CREATE TABLE statement 我的CREATE TABLE语句

    db.execSQL("CREATE TABLE characters (id INTEGER NOT NULL , 
   name TEXT NOT NULL , chapter TEXT NOT NULL , book TEXT NOT NULL );");

I think the most elegant way of doing so would be for you to isolate the properties of your rows which permit to identify them UNIQUELY. 我认为这样做的最优雅的方法是让您隔离行的属性,从而可以唯一地标识它们。 Lets say for instance that name + book make the line unique, then you could concatenate these strings into one identifier in your object, add a column to your database, put and index on it and search on this index when you want to insert new elements. 比方说,例如,名称和书使该行唯一,那么您可以将这些字符串连接到对象中的一个标识符中,在数据库中添加一列,在其上放置索引并在要插入新元素时搜索该索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM