denodrivers/mysql

executing a query with multiple `create table...;` statements does not work

jonasfrey opened this issue · 2 comments

var s_query_create_table1 = `
CREATE TABLE users (
    id int(11) NOT NULL AUTO_INCREMENT,
    name varchar(100) NOT NULL,
    created_at timestamp not null default current_timestamp,
    PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
`
var s_query_create_table2 = `
CREATE TABLE users2 (
    id int(11) NOT NULL AUTO_INCREMENT,
    name varchar(100) NOT NULL,
    created_at timestamp not null default current_timestamp,
    PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
`

// executing the querys one by one works

 result = await client.execute(s_query_create_table1)
 result = await client.execute(s_query_create_table2)
// executing both querys at the same time does not work 

result = await client.execute(`${s_query_create_table1};${s_query_create_table2}`)

the error is:
INFO close connection error: Uncaught (in promise) Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '; CREATE TABLE users2 ( id int(11) NOT NULL AUTO_INCREMENT, name varchar' at line 7 throw new Error(error.message);

The error you're encountering is because the MySQL client for Deno does not support executing multiple queries at once. To create both tables, you need to execute the queries one by one, as you've already done successfully.

To create the tables, you can use the following code:

var s_query_create_table1 = `
CREATE TABLE users (
    id int(11) NOT NULL AUTO_INCREMENT,
    name varchar(100) NOT NULL,
    created_at timestamp not null default current_timestamp,
    PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
`;

var s_query_create_table2 = `
CREATE TABLE users2 (
    id int(11) NOT NULL AUTO_INCREMENT,
    name varchar(100) NOT NULL,
    created_at timestamp not null default current_timestamp,
    PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
`;

// Execute the queries one by one
result = await client.execute(s_query_create_table1);
result = await client.execute(s_query_create_table2);

As you can see, executing the queries one by one works, but combining them with a semicolon will result in a syntax error because the MySQL client for Deno does not support multi-query execution. If you need to execute multiple queries, you'll need to run them separately using the execute function for each query.

thanks for the response, then an appropriate error message such as executing multple queries is not supported would be great