sqlite3_prepare_v2失败(Xcode的SQLite 3)

我很想连接的SQLite,但不能。 有人能帮我吗? 我没有看到任何错误在这里,我search谷歌和这里的这个问题的高低,但我找不到任何方法来解决这个问题。

我在这里按照教程

不要帮助我,我会为这个错误疯狂。 =(

[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"); } 

日志只读“打开”,然后“语句失败”

这是我如何做到的。 (编译我的数据库类的一块)
也显示你的SQL错误。

 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); 

错误在这行代码中。

 if (sqlite3_prepare_v2(contactDB, query_stmt, 1, &statement, NULL) == SQLITE_OK) { 

1是错的。 从SQLite文档:

 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. 

所以你迫使SQLite只读取语句中的第一个字节。 当然这是无效的。

确保你已经在你的项目中完成了这些要点。

  1. validation文件列在目标的“ 构build阶段 ”下的“ 复制软件包资源 ”下面,而不是在具有自定义目标或子path的“ 复制文件 ”下面。

  2. 确保你没有任何path与你的sqlite文件冲突。我的意思是文档directory/database path/是正确的。

  3. 而且我已经实施了像sqlite的查询

    SELECT * FROM someTable WHERE status LIKE 'someString' AND BankName LIKE 'someString'而不是
    SELECT * FROM someTable WHERE status='someString' AND BankName='someString'

  4. 确保你没有包含你的sqlite文件在任何其他应用程序

  5. 确保你没有重复的sqlite文件具有相同的path。

经过很多的努力,这对我很有帮助,我希望它能帮助一些人。 请投票,如果有帮助。