4

I implemented Sqlite in my project by adding #import <sqlite3.h> in my header.h file and libsqlite3.dylib.

How can I pass an array as parameter to my query, here is what I thought:

var arrayId = [1,2] // array with interested Id 
var query:NSString = "Select id from Product where id IN \(arrayId)" // I want to select Products with id that are in the array

Edit: Does it change if arrayId is NSArray ? Because I also need arrayId as NSArray.

then I proceed with open sqlite database, prepare query and so on.

Thank you in advance.

1
  • Since this post is a top ten search engine result for the same question, note that since 2016 there is a loadable table-valued function extension (not compiled into sqlite by default) called carray() that lets you bind an array as an inline table against which to join your query (instead of an 'IN' clause). Commented Jul 21, 2021 at 18:03

5 Answers 5

6

You can easily combine the array into a string with join function.

var arrayId = [1,2] // array with interested Id
var inExpression = ",".join(map(arrayId) { "\($0)"})
// inExpression = "1,2"
var query = "Select id from Product where id IN (\(inExpression))"
Sign up to request clarification or add additional context in comments.

1 Comment

Even NSArray can be used the same way.
2

Update for Swift3:

var arrayId = [1,2] // array with interested Id
var inExpression = arrayId.flatMap{ String($0) }.joined(separator: ",")
// inExpression = "1,2"
var query = "SELECT id FROM Product WHERE id IN (\(inExpression))"

Comments

0

You need to accomplish two things: convert your array of Ints to Strings and then implode the array into a string, by joining them with commas (as you would want to do with an IN SQL statement).

Here's a rudimentary function that does just that:

func implode(ints: [Int]) -> String {
    // Convert to Strings
    let strs = ints.map { String($0) }
    // Join Strings with commas
    return ",".join(strs)
}

And then in use:

"WHERE id IN (\(implode(arrayId)))"

Comments

0

I'd probably use something like:

var arrayId = [1,2] // array with interested Id
var str = ",".join(arrayId.map { return "\($0)" })
var query = "SELECT id FROM Product WHERE id IN [\(str)]"

2 Comments

in this way my query is SELECT id FROM Product WHERE id IN [1,2], I execute this on SQLite Manager, there is an error: no such table: 1,2
Try it with parenthesis around the \(str) instead of brackets. I was guessing at the actual SQL syntax.
0

Using Swift's own string interpolation to create SQL statements can be risky (as with any language). The sqlite3 library provides parameter binding for this purpose:

if (statement.prepare("SELECT name FROM products WHERE id = ?") != .Ok) {
    // Deal with error here
}

// Bind the question mark to your value
statement.bindInt(1, value: 8766)

if (statement.step() == .Row) {
    let name = statement.getStringAt(1)
    // ...do something with your data from the statement
}

// Done.
statement.finalizeStatement()

EDIT:

For the comment below, you need () brackets, not []:

select id, body from test where id in (1,2);

not

select id, body from test where id in [1,2];

2 Comments

Your comment is valid, but doesn't really answer the question of how to pass in an Array of Int's as an IN parameter.
As @David said, it doesn't answer the question. Thank you anyway.

Your Answer

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.