lastinsertid

Get last inserted value from sqlite database Android

a 夏天 提交于 2019-11-26 09:46:06
问题 I am trying to get the last inserted rowid from a sqlite database in Android. I have read a lot of posts about it, but can\'t get one to work. This is my method: public Cursor getLastId() { return mDb.query(DATABASE_TABLE, new String[] {KEY_WID}, KEY_WID + \"=\" + MAX(_id), null, null, null, null, null);} I have tried with MAX, but I must be using it wrong. Is there another way? 回答1: Well actually the SQLiteDatabase class has its own insert method which returns the id of the newly created row

MySQL LAST_INSERT_ID() used with multiple records INSERT statement

情到浓时终转凉″ 提交于 2019-11-26 08:56:46
问题 If I insert multiple records with a loop that executes a single record insert, the last insert id returned is, as expected, the last one. But if I do a multiple records insert statement: INSERT INTO people (name,age) VALUES (\'William\',25), (\'Bart\',15), (\'Mary\',12); Let\'s say the three above are the first records inserted in the table. After the insert statement I expected the last insert id to return 3, but it returned 1. The first insert id for the statement in question. So can

How to program a MySQL trigger to insert row into another table?

混江龙づ霸主 提交于 2019-11-26 04:24:37
问题 I\'m looking to create a MySQL trigger on a table. Essentially, I\'m creating an activity stream and need to log actions by users. When a user makes a comment, I want a database trigger on that table to fire and: Grab the ID of the last inserted row (the id of the comment row). perform an INSERT into an activities table, using data from the last inserted row. I\'ll essentially replicate this trigger for deleting comments. Questions I had: Is LAST_INSERT_ID() the best way to grab the id? How

mysqli last insert id

*爱你&永不变心* 提交于 2019-11-26 03:31:00
问题 I would like to associate the image with firstname, lastname...how can I retrieve the last rowand use it to insert to the other table? I tried $image = $mysqli->insert_id; then binding but it doesn\'t work. Can someone help me out? $image = $mysqli->insert_id;//this should come from table2 $stmt = $mysqli->prepare(\" insert into table1 (username, firstname, lastname, image) select ?,?,?,image from table2 t2 where username = ? and t2.id = ? \"); $stmt->bind_param(\'sssss\', $username, $fname,

PostgreSQL function for last inserted ID

跟風遠走 提交于 2019-11-26 00:17:34
问题 In PostgreSQL, how do I get the last id inserted into a table? In MS SQL there is SCOPE_IDENTITY(). Please do not advise me to use something like this: select max(id) from table 回答1: ( tl;dr : goto option 3: INSERT with RETURNING ) Recall that in postgresql there is no "id" concept for tables, just sequences (which are typically but not necessarily used as default values for surrogate primary keys, with the SERIAL pseudo-type). If you are interested in getting the id of a newly inserted row,