0

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?

kmdreko
66.3k6 gold badges109 silver badges180 bronze badges
asked Sep 5, 2024 at 18:18

1 Answer 1

1

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.

answered Sep 5, 2024 at 18:27
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.