简体   繁体   English

我们如何获取通过代码插入的行的 id

[英]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.

相关问题 我们如何通过Java代码而不是xml在android中的表格布局的表格行中将一个或多个textview对齐到另一个下方 - How we can align one or more textviews one below to another in table row of table layout in android through java code not xml 我们如何通过android应用获取汽车行驶里程 - How can we get mileage of the car through android app 我们如何通过Android设备获得准确的位置? - How accurate location can we get through an android device? 我们如何获得“微调编号”未选中的项目? - How we get Spinner Id not Selected item? 我们可以在 Android 设备的本机反应中获得 face-id 吗? - Can we get face-id in react native for Android devices? 我们可以通过 react native 获取当前的壁纸图像吗? - Can we get current wallpaper image through react native? 当用户在视图上移动手指时,如何获取在Android的onTouchListener中触摸过的视图的ID? - How can we get the id of the view that was touched in onTouchListener in Android when the user is moving the finger on the views? 我们如何在Android中获取框架 - How can we get frames in Android 插入后如何获取行联系人ID? - How to get row contact id after insert? 我们如何从c2dm(Android)获得注册ID? - How we get registration id from c2dm (Android)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM