Hi. this function create a database "books.db", but not the table history.
Why ?
fn db_connection() -> Result<()> {
// .databases
let conn = Connection::open("/home/pichi/books.db")?;
conn.execute(
"CREATE TABLE if not exists history (
id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
original_title TEXT NOT NULL,
)",
(), // empty list of parameters.
)?;
let a = "aaaa".to_string();
let b = "bbbb".to_string();
conn.execute(
"INSERT INTO history (title, original_title) VALUES (?1, ?2)",
(&a, &b),
)?;
Ok(())
}
Here's what I suspect happened: At some point you had a working table creation, but then you made some edits and broke it. However, because the database is persistent it still has the table so you assume that part is okay, but it isn't.
So your immediate problem is that your CREATE TABLE is broken:
CREATE TABLE if not exists history (
id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
original_title TEXT NOT NULL, <-- remove the trailing comma
)
And the reason I suspect this is what happened is because no matter how many times I've done this myself, I still keep doing it.
It's always a good idea to remove the database file from time to time, because it'll allow you to see if you borked the table creation.
Also, if you unwrap on the table creation you'll see that it's croaking due to a syntax error.
Edit: I misread the original post; I thought it said that the table was created successfully. Generally speaking, when I'm writing database code, I use unwrap() because you really want the application to explode on SQL syntax errors.
Hi, i apreciate your help.
Another problem. when i try format a string to execute a query:
let conn = Connection::open(/home/pichi/book_db);
let var_table: &str = "history";
let query = format!("CREATE TABLE {var_table} (
id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
original_title TEXT NOT NULL,
aka TEXT NOT NULL,
year TEXT NOT NULL)");
conn.execute(&query, ())?;
OUTPUT:
Error! unknown database var_table in CREATE TABLE var_table (
id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
original_title TEXT NOT NULL,
aka TEXT NOT NULL,
year TEXT NOT NULL) at offset 13
This code cannot be compiled as-is due to at least one syntax error, so it's possible that in your real code there's another error, not reproduced here.