last-insert-id

Get all insert ids in a transaction

ぐ巨炮叔叔 提交于 2019-12-12 05:39:26
问题 I have two tables in a MySQL database: "messages" and "message_tags". The "messages" table has an auto increment column "message_id". In Java I want to add a batch of messages to the database using the java.sql package. I want to do this in one transaction to save queries. My SQL code should look something like this: START TRANSACTION INSERT INTO messages(`message`) VALUES ('message1'); INSERT INTO messages_tags(`message_id`, `tags`) VALUES (LAST_INSERT_ID(), 'tagfoo1'); INSERT INTO messages

Get last insert id from last inserted row

佐手、 提交于 2019-12-11 19:41:36
问题 I'm here again and I'm getting crazy! I have this stored procedure: CREATE DEFINER=`root`@`localhost` PROCEDURE `aggiungi_categoria`(IN `id_categoria_principale` INT, IN `nome_categoria` VARCHAR(150), IN `sigla_categoria` VARCHAR(10), IN `stato_categoria` INT) NOT DETERMINISTIC MODIFIES SQL DATA SQL SECURITY DEFINER begin select @rgt := rgt,@lft := lft from categorie where id = id_categoria_principale; if @rgt - @lft = 1 then UPDATE categorie SET rgt = rgt + 2 WHERE rgt > @lft; UPDATE

stored procedures and mysql_insert_id issue

巧了我就是萌 提交于 2019-12-11 11:39:14
问题 My problem is this: I Created a store procedure from php to mysql. Now before someone says something about that practice, The procedure works fine. The problem is this, when I call the procedure from php and I KNOW that a record has been entered, mysql_insert_id(), returns 0... Any ideas why? Oh, and yes, my id field is AUTO_INCREMENT and PRIMARY KEY p.s If there's an easy way to format my code in here, pls tell me. I put pre in html, but it doesn't seem to work very well. Ty in advance.

Twisted adbapi: runInteraction last_insert_id()

空扰寡人 提交于 2019-12-10 17:19:16
问题 class MySQL(object): def __init__(self): self.dbpool = adbapi.ConnectionPool( 'MySQLdb', db='dummy', user='root', passwd='', host = 'localhost', cp_reconnect = True, cursorclass=MySQLdb.cursors.DictCursor, charset='utf8', use_unicode=True ) def process(self, item): query = self.dbpool.runInteraction(self.conditionalInsert, item).addErrback(self.handle_error) return item def conditionalInsert(self, tx, item): tx.execute("INSERT INTO User (user_name) VALUES (%s)",(name)) tx.execute("SELECT LAST

LAST_INSERT_ID not working on UPDATE

半城伤御伤魂 提交于 2019-12-10 11:46:48
问题 if I use this SQL: UPDATE formulare SET EV_id=59, EV_status=5 WHERE EV_id=57 AND ID_uziv=12;SELECT LAST_INSERT_ID(); I will get 0 as last insert id. I'm using php mysqli_insert_id and here is said that: The mysqli_insert_id() function returns the ID generated by a query on a table with a column having the AUTO_INCREMENT attribute. If the last query wasn't an INSERT or UPDATE statement or if the modified table does not have a column with the AUTO_INCREMENT attribute, this function will return

SELECT LAST_INSERT_ID() Not working with BIGINT on MySQL 5.6.11

前提是你 提交于 2019-12-08 07:17:10
问题 I have a BIGINT field as an auto increment and primary key on a MySQL Innodb table, running MySQL Community Server 5.6.11. After calling a basic INSERT statement, and then calling SELECT LAST_INSERT_ID(), I'm always returned 0, even though the INSERT statement was sucessful. Any ideas why this might be happening. UPDATE: Here is my table definition CREATE TABLE `Booking` ( `BookingId` bigint(20) NOT NULL AUTO_INCREMENT, `HotelId` int(11) NOT NULL, `AgentId` int(11) NOT NULL DEFAULT '0',

Avoid increasing Auto_increment value?

不问归期 提交于 2019-12-08 04:37:30
问题 In MySQL, you can insert a row and update the 'last insert ID' at the same time. I'm using this trick to be able to insert items conditionally (uniqueness) and still get the unique ID in all cases. The code below works. The problem is that the ON DUPLICATE KEY statement also updates the Auto_increment value. Is it possible to avoid that? CREATE TABLE T( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, X VARCHAR(64) NOT NULL UNIQUE ) ENGINE = InnoDB DEFAULT CHARSET=utf8; INSERT INTO T(X) VALUES ('x

getting last inserted row id with PDO (not suitable result)

亡梦爱人 提交于 2019-12-08 02:04:28
问题 I have a problem with PDO::lastInsertId() method which doesn't return the id (primary key) of last inserted row, instead it returns another field which is a foreign key field. PHP code: $pdo = new PDO(...); $stmt = $pdo->prepare($sql); $stmt->bindParam(...); $stmt->bindParam(...); $stmt->execute(); $id = $pdo->lastInsertId(); // or $id = $pdo->lastInsertId('services_id_seq'); // I think 'services_id_seq' is not necessary in MySQL // both of them don't return the primary key of last inserted

why pdo->lastInsertId() return 0 when i call STORED PROCEDURE in mysql?

一个人想着一个人 提交于 2019-12-07 16:03:43
问题 i need return id of last inseted row in my database : i have a class named DatabaseHandler to use pdo <?php class DatabaseHandler { private static $_mHandler; private function __construct() {} private static function GetHandler() { if (!isset(self::$_mHandler)) { try { self::$_mHandler = new PDO(PDO_DSN, DB_USERNAME, DB_PASSWORD, array(PDO::ATTR_PERSISTENT => DB_PERSISTENCY)); self::$_mHandler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); self::$_mHandler->setAttribute(PDO::MYSQL

PDO - lastInsertId() for multiple insert query

走远了吗. 提交于 2019-12-06 22:12:06
问题 I can insert 2 pets into a table, and get their lastInsertId() for further processing one at a time (2 queries). I am wondering if there is a way to get two lastInsertIds() and assign them to variables if I am inserting 2 rows in 1 query: $query = "INSERT INTO pets (pet_name) VALUES (':coco'),(':jojo')"; $pet_insert = $dbh->prepare($query); $pet_insert->execute(array(':coco' => $coco,':jojo' => $jojo)); $New_PetID = $dbh->lastInsertId(); Is it possible to get the lastInsertId() for coco and