Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Rusqlite only creates one table and forgets rest of the command

hello my dear friends,

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

thank you!

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading