[英]how can we get the id of a row that we insert through code
I need to retrieve the id of the row inserted just now.我需要检索刚才插入的行的 id。 ie, i have a table for words and a table for meaning.
即,我有一个单词表和一个含义表。 i need the wordId of the word i insert in the table for words and that wordId is used for inserting the meaning in meaning table.
我需要我在单词表中插入的单词的 wordId,该 wordId 用于在含义表中插入含义。 Can anyone help me out??
谁能帮我吗??
I thought i could use trigger and tried the trigger:我以为我可以使用触发器并尝试使用触发器:
"CREATE TRIGGER IF NOT EXISTS word_insert_trigger AFTER INSERT ON tb_words BEGIN select NEW.word_id from tb_words; END;" “如果不存在,则在插入 tb_words 后创建触发器 word_insert_trigger BEGIN 从 tb_words 中选择 NEW.word_id;结束;”
like this.像这样。 i tried this in sqlite dbbrowser.
我在 sqlite dbbrowser 中试过这个。 but it didn't work out.
但它没有成功。
i need the row id when i insert a row like this :"insert into tb_words(word_name) values('test');"当我插入这样的行时,我需要行 ID:“插入到 tb_words(word_name) values('test');”
How can i do that without using "SELECT last_insert_rowid()"?如果不使用“SELECT last_insert_rowid()”,我该怎么做? like in the following link: How to retrieve the last autoincremented ID from a SQLite table?
就像在以下链接中一样: 如何从 SQLite 表中检索最后一个自动递增的 ID?
No need for a trigger.不需要触发器。 Use the SQliteDatabase insert method.
使用 SQLiteDatabase 插入方法。 It returns the id (as a long ) (more correctly it returns the rowid and assuming that the word_id column has been defined as an alias of the rowid column, then the returned value will be the value assigned to the word_id column).
它返回id (作为long )(更准确地说,它返回rowid并假设word_id列已被定义为rowid列的别名,那么返回的值将是分配给 word_id 列的值)。
An alias of the rowid column is defined if word_id INTEGER PRIMARY KEY
is coded (the AUTOINCREMENT key may be used BUT in generally should not be used).如果
word_id INTEGER PRIMARY KEY
被编码(可以使用 AUTOINCREMENT 键,但通常不应使用),则定义rowid列的别名。
You may wish to read SQLite AUTOINCREMENT and/or Rowid Tables您可能希望阅读SQLite AUTOINCREMENT和/或Rowid 表
Instead of something like :-而不是像这样的东西:-
db.execsql("insert into tb_words(word_name) values('test');");
You would use something like :-你会使用类似的东西:-
ContentValues cv = new ContentValues();
cv.put("word_name","test");
long word_id = db.insert("tb_words",null,cv);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.