Reputation: 435
I'm so trying to connect the sqlite but can't. Can someone help me? I don't see any error here at all and I've searched high and low for this problem on google and here but I can't find any way to fix this.
I'm following the tutorial here
Do help me, I'm going crazy over this error. =(
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
sqlite3 *contactDB; //declare pointer to database structurery
const char *dbPath = [databasePath UTF8String]; //convert NSString to UTF8String
//open database file. TRUE = success, FALSE = failed
if (sqlite3_open(dbPath, &contactDB)== SQLITE_OK){
NSLog(@"Opened");
sqlite3_stmt *statement;
NSString *querySQL = @"SELECT address, phone FROM contacts";
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(contactDB, query_stmt, 1, &statement, NULL) == SQLITE_OK) {
NSLog(@"Statement is OK");
}else{
NSLog(@"Statement FAILED");
}
}else{
NSLog(@"Failed");
}
The log only reads "Opened" then "Statement FAILED"
Upvotes: 1
Views: 14419
Reputation: 1263
Make sure these points you have done in your project.
Verify the file is listed under "Copy Bundle Resources" under your Target's "Build Phases" and NOT under "Copy Files" with a custom destination or sub-path.
Make sure you don't have any path conflicts with your sqlite file.I mean document directory/database path/
are correct.
And I have implemented sqlite query like
SELECT * FROM someTable WHERE status LIKE 'someString' AND BankName LIKE 'someString'
Instead of
SELECT * FROM someTable WHERE status='someString' AND BankName='someString'
Make sure you have not included your sqlite file in any other application
Make sure you don't have duplicate sqlite files with same path.
This is worked for me after lot of struggle, I hope it helps some one. Please up vote if it helps.
Upvotes: 0
Reputation: 9826
The error is in this line of code.
if (sqlite3_prepare_v2(contactDB, query_stmt, 1, &statement, NULL) == SQLITE_OK) {
The 1
is wrong. From the SQLite docs:
int sqlite3_prepare_v2(
sqlite3 *db, /* Database handle */
const char *zSql, /* SQL statement, UTF-8 encoded */
int nByte, /* Maximum length of zSql in bytes. */
sqlite3_stmt **ppStmt, /* OUT: Statement handle */
const char **pzTail /* OUT: Pointer to unused portion of zSql */
);
If the nByte argument is less than zero, then zSql is read up to the first zero
terminator. If nByte is non-negative, then it is the maximum number of bytes
read from zSql.
So you are forcing SQLite to only read the first byte from the statement. Of course this is invalid.
Upvotes: 1
Reputation: 4546
This is how I do it. (Compiled piece of my database class)
Shows you the sql error too.
sqlite3 *_database;
sqlite3_open([databasePath UTF8String], &_database);
NSString *sqlStatement = @"SELECT address, phone FROM contacts";
const char *sql = [sqlStatement UTF8String];
static sqlite3_stmt *compiledStatement;
int result = sqlite3_prepare_v2(_database, sql, -1, &compiledStatement, NULL);
if(result != SQLITE_OK) {
NSLog(@"Prepare-error #%i: %s", result, sqlite3_errmsg(_database));
}
result = sqlite3_step(compiledStatement);
if (result != SQLITE_DONE) {
NSLog(@"Step-error #%i for '%@': %s", result, sqlStatement, sqlite3_errmsg(_database));
}
sqlite3_finalize(compiledStatement);
Upvotes: 6