Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to properly use parameters with "ATTACH DATABASE ?" #429

Open
tim-vk opened this issue Jun 8, 2023 · 1 comment
Open

How to properly use parameters with "ATTACH DATABASE ?" #429

tim-vk opened this issue Jun 8, 2023 · 1 comment

Comments

@tim-vk
Copy link

tim-vk commented Jun 8, 2023

I'm trying to attach a database and for the life of me: i cannot get it to work.

My first issue: Is it correct that you're not allowed to have quotation (either single or double) around a parameter variable? (?).

The following throws error 25 (SQLITE_RANGE) on query.bind:

SQLite::Statement query(db, R"(ATTACH DATABASE "?")");
query.bind(1, "path/to/database.db"); // Error 25

To solve that I tried moving the quotation marks to the string itself. e.g.:

SQLite::Statement query(db, R"(ATTACH DATABASE ?)");
query.bind(1, "\"path/to/database.db\"");
query.exec(); // Error 14

However, this throws error 14 (SQLITE_CANTOPEN).

If i don't use variables at all it works perfectly!:

// Works!
SQLite::Statement query(db, R"(ATTACH DATABASE "path/to/database.db")");
query.exec(); // I'm a happy developer.

Are these some sqlite/sqlitecpp quirks i'm running into or is this just misuse on my side?

@kaptenhonek
Copy link

kaptenhonek commented Jun 26, 2023

Do you really need to use quotation marks when you're using a prepared statement?
Have you tried the following:

SQLite::Statement query(db, R"(ATTACH DATABASE ?)");
query.bind(1, "path/to/database.db");
query.exec();

That is, without using extra quotation marks when you bind the parameter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants