Reputation: 45
I'm currently trying to execute a query on a database of words. The database is organized such that I can access by first letter of the word, and by length of the word. However, I'm having trouble when I search using a letter and length:
//random letter sends an NSString with just a single letter from the English alphabet
NSString * randomLetter = [self randomLetter];
int characterCodeInASCII = [randomLetter characterAtIndex:0];
FMResultSet *rs = [db executeQuery:@"SELECT * FROM WORDS WHERE INDEXER=? AND LENGTH=?",characterCodeInASCII,length];
Unfortunately, the query isn't returning any values. I've tried this using a char as the input, as a string, and as an int.
I'm quite confused since I can do the same query using ruby:
list=db1.execute("SELECT * FROM WORDS WHERE INDEXER=110 AND LENGTH=7")
And get 453 results....
The sqlite table has the following columns:
>> db1.execute("SELECT * FROM sqlite_master")
=> [["table", "words", "words", 2, "CREATE TABLE words(word VARCHAR(15) PRIMARY KEY, indexer VARCHAR(1), length INTEGER)"], ["index", "sqlite_autoindex_words_1", "words", 3, nil]]
Any ideas on what I might be missing?
Thanks much!
Upvotes: 3
Views: 1010
Reputation: 243156
FMDB is expecting all of the arguments to the executeQuery:
method to be objects, and an int
isn't an object.
There are a couple of approaches you could take:
Box the int
in an NSNumber
. That would look like this:
NSString *randomLetter = ...;
NSNumber *characterCode = [NSNumber numberWithInt:[randomLetter characterAtIndex:0]];
NSNumber *length = [NSNumber numberWithInt:lengthAsInt];
FMResultSet *rs = [db executeQuery:@"SELECT * FROM WORDS WHERE INDEXER=? AND LENGTH=?", characterCode, length];
Use a different method, such as the executeQueryWithFormat:
method:
FMResultSet *rs = [db executeQueryWithFormat:@"SELECT * FROM WORDS WHERE INDEXER = %d AND LENGTH + %d", characterCodeInASCII, length];
Upvotes: 2