I am working with NodeJS and MySQL to save a post and its tags via post_tag:
DROP TABLE IF EXISTS post_tag;
DROP TABLE IF EXISTS post;
DROP TABLE IF EXISTS tag;
DROP PROCEDURE IF EXISTS select_all_posts;
DROP PROCEDURE IF EXISTS insert_post;
CREATE TABLE post (
id INT NOT NULL AUTO_INCREMENT,
title VARCHAR(45) NULL,
body TEXT NULL,
PRIMARY KEY (id));
CREATE TABLE tag (
id INT NOT NULL AUTO_INCREMENT,
tag VARCHAR(45) NULL,
PRIMARY KEY (id));
CREATE TABLE post_tag (
post_id INT NOT NULL,
tag_id INT NOT NULL,
PRIMARY KEY (post_id, tag_id),
INDEX fk_post_tag_tag1_idx (tag_id ASC),
INDEX fk_post_tag_post_idx (post_id ASC),
CONSTRAINT fk_post_tag_post
FOREIGN KEY (post_id)
REFERENCES post (id),
CONSTRAINT fk_post_tag_tag1
FOREIGN KEY (tag_id)
REFERENCES tag (id));
INSERT INTO post (id, title, body) VALUES (1, 'post 1', "Post body");
INSERT INTO post (id, title, body) VALUES (2, 'post 2', "Post body");
INSERT INTO tag (id, tag) VALUES (1, 'tag 1');
INSERT INTO tag (id, tag) VALUES (2, 'tag 2');
INSERT INTO post_tag (post_id, tag_id) VALUES (1, 1);
INSERT INTO post_tag (post_id, tag_id) VALUES (1, 2);
INSERT INTO post_tag (post_id, tag_id) VALUES (2, 1);
I created a stored procedure to insert new posts:
-- Stored procedure to insert post and tags
DELIMITER $$
CREATE PROCEDURE insert_post(
IN title VARCHAR(45),
IN body TEXT
)
BEGIN
INSERT INTO post (title, body) VALUES (title, body);
END $$
DELIMITER ;
So in node I can do:
var mysql = require("mysql2");
var connection = mysql.createConnection({
host: "127.0.0.1",
user: "test_database",
password: "test_database",
database: "test_database",
});
connection.connect();
const postTitle = JSON.stringify("Post 3");
const postBody = JSON.stringify("This is post 3 body");
const insertPostQuery = "CALL insert_post(" + postTitle + "," + postBody + " )";
connection.query(insertPostQuery, (error, results) => {
console.log(JSON.stringify(results, null, 4));
});
connection.end();
This returns:
{
"fieldCount": 0,
"affectedRows": 1,
"insertId": 0,
"info": "",
"serverStatus": 2,
"warningStatus": 0
}
Now I want to retrieve the last inserted id from the stored procedure, so I modify my stored procedure to add an OUT last_inserted_id INT
param, and then assign it after the INSERT:
-- Stored procedure to insert post and tags
DELIMITER $$
CREATE PROCEDURE insert_post(
IN title VARCHAR(45),
IN body TEXT,
OUT last_inserted_id INT
)
BEGIN
INSERT INTO post (title, body) VALUES (title, body);
SET last_inserted_id = LAST_INSERT_ID();
END $$
DELIMITER ;
But when executing the query in node this returns undefined
instead of the json with the last_inserted_id
value.
What I'm doing wrong?
Can you provide an example?
CREATE FUNCTION insert_post_fn(
in_title VARCHAR(45),
in_body TEXT
)
RETURNS BIGINT
MODIFIES SQL DATA
BEGIN
INSERT INTO post (title, body) VALUES (in_title, in_body);
RETURN LAST_INSERT_ID();
END
How to use:
SELECT insert_post_fn('Post title', 'This is post body') AS `LAST_INSERT_ID`;
CREATE PROCEDURE insert_post_proc(
IN in_title VARCHAR(45),
IN in_body TEXT,
OUT out_id BIGINT
)
BEGIN
INSERT INTO post (title, body) VALUES (in_title, in_body);
SET out_id = LAST_INSERT_ID();
END
How to use:
CALL insert_post_proc('Post title', 'This is post body', @id);
SELECT @id AS `LAST_INSERT_ID`;
-- PS. Variable name used is random
-- PPS. Both statements must be executed
-- in the same connection and without reconnect
CREATE PROCEDURE insert_post_proc(
IN in_title VARCHAR(45),
IN in_body TEXT
)
BEGIN
INSERT INTO post (title, body) VALUES (in_title, in_body);
SELECT LAST_INSERT_ID() AS `LAST_INSERT_ID`;
END
How to use:
CALL insert_post_proc('Post title', 'This is post body');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.