Search code examples
sqlsqliterustrusqlite

Why does Rusqlite only create the first table when executing my init script?


I want to create a database using rusqlite, it should have multiple tables so I created a file create.sql that holds the SQL code to create the tables.

In my reproduced example, it looks like this:

create table TestTable1 (
    id integer primary key
);

create table TestTable2 (
    id integer primary key
);

Then I use Rust to run it, like this:

fn main() {
    let db = rusqlite::Connection::open("test.db").unwrap();

    let command = std::fs::read_to_string("create.sql").unwrap();

    db.execute(&command, []).unwrap();
}

But when I open the file in DB Browser, it only contains TestTable1. I also can't see it in the raw binary of the database, and I can't acces the table with rusqlite, so I'm pretty sure it's not being created.

So why is rusqlite quitting after creating the first table?


Solution

  • Use .execute_batch() instead.

    Just .execute() only executes a single statement (as documented). Calling .execute() with multiple statements will ignore the rest unless you opt-in to the "extra_check" feature flag. See this GitHub issue for more info.