last-insert-id

Reliable or not PDO lastInsertId() when using transactions

核能气质少年 提交于 2020-01-30 07:00:07
问题 I use PDO transaction try { DB::$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); DB::$db->beginTransaction(); $db->prepare( insert query ); $db->execute(); $last_insert_id = $db->lastInsertId(); ... ... Multiple concurrent requests are expected on this script. Question: is it possible that lastInsertId() return incorrect value for the user, who actually inserted the row? (by "incorrect value" i mean: id that is inserted by some other user). 回答1: You're safe. The ID you get will

Reliable or not PDO lastInsertId() when using transactions

狂风中的少年 提交于 2020-01-30 07:00:05
问题 I use PDO transaction try { DB::$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); DB::$db->beginTransaction(); $db->prepare( insert query ); $db->execute(); $last_insert_id = $db->lastInsertId(); ... ... Multiple concurrent requests are expected on this script. Question: is it possible that lastInsertId() return incorrect value for the user, who actually inserted the row? (by "incorrect value" i mean: id that is inserted by some other user). 回答1: You're safe. The ID you get will

Qt's odbc driver does not support LastInsertId with mysql, whats the workaround?

守給你的承諾、 提交于 2020-01-06 07:58:10
问题 Im using Qt 4.8.3 and the MySQL ODBC 3.51 Driver. The driver does not seem to support LastInsertId which I test like this: db.driver()->hasFeature(QSqlDriver::LastInsertId) I need it to find the auto increment id I just inserted query.exec("INSERT INTO myTable (MyVal, Created) VALUES ('1', NOW())"); auto insertId = query.lastInsertId().toString(); What is the best workaround? EDIT: there will be concurrent access to this table from multiple clients. 回答1: According to MySQL documentation, you

PDO Get Multiple Insert Ids

六眼飞鱼酱① 提交于 2020-01-02 04:06:28
问题 Running the following query using PDO (Actually, I use prepared statements but same problem) INSERT INTO MyTable(MyField) VALUES('Row1'), ('Row2') How can I get the Ids for the records relating to Row1 and Row2 ? $db->lastInsertId() literally returns the last single Id. Is it sufficient to take this last Id, subtract the # of records and assume that range covers all my records? can there be gaps/jumps. Is this query guaranteed to be atomic? 回答1: If you're using MyISAM tables then because of

Spring Batch Could not obtain last_insert_id(); nested exception is java.sql.SQLException: Lock wait timeout exceeded;

徘徊边缘 提交于 2019-12-25 09:59:13
问题 I am using spring batch 3.0.5.RELEASE on MySQL database. I have a job which reads from multiple tables and process the records and mark their status after completion. The job data is designed in such a way that multiple instances of the same job can run without stepping on each others toes..(They have there own data set to work with) I have following job repository configuration. protected JobRepository createJobRepository() throws Exception { JobRepositoryFactoryBean factory = new

SQL Insert Row and copy inserted Auto-Increment id to another column

久未见 提交于 2019-12-23 03:07:31
问题 I am using PHP and MySQL. Goal: Insert a new row into a table with an auto-increment id column. While inserting this new row, I would like to copy the new inserted row's auto-increment id # into another column in the same row/entry. Upon creation, the new entry/row should have the auto-incremented id column and another column with the same number. My biggest concern: I need all of this to happen correctly and reliably, even while other transactions from other connections could be taking place

How to get inserted id using Spring Jdbctemplate.update(String sql, obj…args)

和自甴很熟 提交于 2019-12-22 06:07:09
问题 I'm using Jdbctemplate and I need the inserted id of a query. I read that I have to build a particular PreparedStatement and use GeneratedKeyHolder object. The problem is that in my application all inserts method uses this JdbcTemplate update method: getJdbcTemplate().update(SQL_INSERT,param1,param2,param3,...); Is there another way to get the inserted id without refactoring all daos? 回答1: Looking at the documentation for NamedParameterJdbcTemplate and JdbcTemplate You have two choices: use

When I INSERT multiple rows into a MySQL table, will the ids be increment by 1 everytime?

懵懂的女人 提交于 2019-12-21 11:34:13
问题 if I have a query like the following: INSERT INTO table (col1,col2,col3) VALUES ('col1_value_1', 'col2_value_1', 'col3_value_1'), ('col1_value_2', 'col2_value_2', 'col3_value_2'), ('col1_value_3', 'col2_value_3', 'col3_value_3'); Suppose that I have a table where the last id PRIMARY_KEY AUTO_INCREMENT value is 56 , then will this insert query always create 3 records with ids 57, 58, 59 . Is this operation atomic? Or, if another query writes on the same table, could the ids not increment

Getting last record from mysql

☆樱花仙子☆ 提交于 2019-12-17 07:41:15
问题 I am using mysql and facing some problem. I want to retrieve last row that is inserted. << Below are details >> Below is how I created table. create table maxID (myID varchar(4)) I inserted four values in it as below insert into maxID values ('A001') insert into maxID values ('A002') insert into maxID values ('A004') insert into maxID values ('A003') When I execute select myID, last_insert_id() as NewID from maxID , I get output as below myId NewID A001 0 A002 0 A004 0 A003 0 When I tried

How to retrieve all last inserted rows IDs in mysql-php? [closed]

笑着哭i 提交于 2019-12-12 09:39:06
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 6 years ago . Is there any efficient way to retrieve all last inserted rows auto incremented IDs from mysql database using php? Suppose I don't know how many rows are there to be inserted in a batch but I need all of their IDs