iOS SQLite插入并选择不起作用?

时间:2012-06-18 12:05:43

标签: ios sqlite

我正在尝试读取/写入SQLite数据库,但我一直在收到错误。我已经验证了db存在于... iPhone模拟器/应用程序/ 5.1 / app#/ Documents / kipSQLDB.db

我已经从终端进行了INSERT和SELECT。

但是从应用程序中它只是出错了。 (附加问题 - 是否有一种方法可以让sqlite在mysql中获取信息错误消息,如mysql_error?当我在应用程序中出错时,我只得到一些符号。

这是我的代码:

·H

#import <Foundation/Foundation.h>
#import <sqlite3.h>

@interface SQLiteController : NSObject { 

//file management
NSFileManager *fileManager;
NSString *documentsDirectory;

//sqlite data
sqlite3* databaseHandle;

}

- (void) initSQLiteDB; 

- (void) insertData : (NSString* ) fName : (NSString* ) lName : (NSString* ) companyName; 

- (NSArray* ) getData;

@end

的.m

- (void) insertData : (NSString* ) fName : (NSString* ) lName : (NSString* ) companyName { 

NSString* insertStatement = [NSString stringWithFormat:@"INSERT INTO nameList (userFName, userLName, userCompany) VALUES (\"%@\", \"%@\", \"%@\")", fName, lName, companyName];
NSLog(@"%@", insertStatement);

char *error;
if ( sqlite3_exec(databaseHandle, [insertStatement UTF8String], NULL, NULL, &error) == SQLITE_OK) {

    int recordID = sqlite3_last_insert_rowid(databaseHandle);
    NSLog(@"A record was inserted into the database %@ with the id of %i", databaseHandle, recordID);
} else { 
    NSLog(@"Error: %s", error);
}
}

- (NSArray* ) getData {

NSMutableArray* dataArray = [[NSMutableArray alloc] init];

NSString* getStatement = @"SELECT * FROM nameList";

sqlite3_stmt *statement;
if (sqlite3_prepare_v2(databaseHandle, [getStatement UTF8String], -1, &statement, NULL) == SQLITE_OK){

    // Iterate over all returned rows
    while (sqlite3_step(statement) == SQLITE_ROW) {

        int recordID = sqlite3_column_int(statement, 0);
        NSString* fNameFromDB = [NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 1)];
        NSLog(@"%@", fNameFromDB);
    }
} else { 
    NSLog(@"No soup for you!");
}

return 0;

}
@end

3 个答案:

答案 0 :(得分:1)

使用Try catch格式并捕获sql的错误类型

答案 1 :(得分:1)

试试这个,我认为它会帮助你解决一些问题

 - (void) insertData : (NSString* ) fName : (NSString* ) lName : (NSString* ) companyName { 

    if(insertStatement == nil)
    {

    NSString* insertStatement = [NSString stringWithFormat:@"INSERT INTO nameList (userFName, userLName, userCompany) VALUES (\"%@\", \"%@\", \"%@\")", fName, lName, companyName];
    NSLog(@"%@", insertStatement);
    if(sqlite3_prepare_v2(database, insertSql, -1, &insertStmt, NULL) != SQLITE_OK)
            NSAssert1(0, @"Error while creating insert statement. '%s'", sqlite3_errmsg(database));
    }


    sqlite3_bind_text(insertStatement, 1, [Gunameq UTF8String], -1, SQLITE_TRANSIENT);
    sqlite3_bind_text(insertStatement, 2, [Gpassq UTF8String], -1, SQLITE_TRANSIENT);
    if(SQLITE_DONE != sqlite3_step(insertStmt))
        NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
    else
        NSLog("Inserted");
    //Reset the add statement.
    sqlite3_reset(insertStatement);
    insertStatement= nil;      

    - (NSArray* ) getData {

    NSMutableArray* dataArray = [[NSMutableArray alloc] init];

    NSString* getStatement = @"SELECT * FROM nameList";

    sqlite3_stmt *statement;
    if (sqlite3_prepare_v2(databaseHandle, [getStatement UTF8String], -1, &statement, NULL) == SQLITE_OK){

        // Iterate over all returned rows
        while (sqlite3_step(statement) == SQLITE_ROW) {

            int recordID = sqlite3_column_int(statement, 0);
            NSString* fNameFromDB = [NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 1)];
            NSLog(@"%@", fNameFromDB);
        }
    } else { 
        NSLog(@"No soup for you!");
    }

    return 0;

    }
    @end

答案 2 :(得分:-1)

SQLite可能有点棘手。对它的所有访问必须来自同一个线程。对于每个准备声明,必须有最终声明。

这里有一个使用SQLite的示例项目,您可以参考:https://github.com/AaronBratcher/ABSQLite

它有一个用于以更传统的数据库方式访问SQLite的类,我觉得它更容易。