Search code examples
databasesqliterustrusqlite

Using the rusqlite crate, can I pass an array as a parameter for a clause containing 'IN'?


I am using the rusqlite crate in Rust to perform SQL queries on an SQLite database. I have a query that needs to filter rows based on a list of values using the IN clause. I would like to know if there is a way to pass an array or a vector as a parameter for the IN clause directly, instead of creating placeholders for each element in the list.

For example, let's say I have the following query:

SELECT * FROM articles WHERE url IN (?);

I would like to pass a Rust vector as a parameter for the ? placeholder, like this:

let urls: Vec<String> = vec![
    "example1.com".to_string(),
    "example2.com".to_string(),
];

let result: Vec<String> = easy_query!(query, params![urls], Vec<String>)?;

Is there a more straightforward way to achieve this without having to create placeholders for each element in the array or vector and passing them as separate parameters?

Thank you in advance for your help.

Note: The easy_query! macro shown in the example is not part of the rusqlite crate. It is a custom syntactic sugar I'm using to simplify the query process with less verbosity. The question is still focused on passing an array or a vector as a parameter for the IN clause directly in rusqlite.


Solution

  • For SQLite, there is the carray extension. And for rusqlite, there is the rarray extension.