为什么应用程序在从同一个表中多次从sqlite检索数据时崩溃

时间:2014-05-08 10:48:32

标签: ios sqlite

我在使用sqlite检索数据时遇到问题。 我有一张名为bill的表(bill_id,bill_name,.......).. 我第一次检索字段时没有收到任何错误,  如果第二次从同一个表中检索数据,则会发出以下错误

由于未捕获的异常终止应用' NSInvalidArgumentException',原因:' * + [NSString stringWithUTF8String:]:NULL cString'

为什么会遇到此异常?

是db的打开和关闭的问题?为了你的信息我保持数据库打开..

请帮帮我..

这是我检索所有字段的代码

- (NSMutableArray *)getBillsList

sqlite3_open([path UTF8String], &database);
int success;
NSMutableArray * getBillsList = [[NSMutableArray alloc]init];

NSString * sql = @"select * from bill";

sqlite3_stmt * getStatement;

if (sqlite3_prepare_v2(database, [sql UTF8String], -1, &getStatement, NULL) != SQLITE_OK)
{
    NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
}
while (sqlite3_step(getStatement) == SQLITE_ROW)
{
    Bills  * bill = [[Bills alloc]init];


        bill.group_name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(getStatement, 1)];


        bill.payee_name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(getStatement, 2)];


        bill.photo_file = [NSString stringWithUTF8String:(char *)sqlite3_column_text(getStatement, 3)];


        bill.amount = sqlite3_column_double(getStatement, 4);


        bill.type = [NSString stringWithUTF8String:(char *)sqlite3_column_text(getStatement, 5)];

        bill.due_date = [NSString stringWithUTF8String:(char *)sqlite3_column_text(getStatement, 6)];

        bill.note = [NSString stringWithUTF8String:(char *)sqlite3_column_text(getStatement, 7)];

        bill.acno = [NSString stringWithUTF8String:(char *)sqlite3_column_text(getStatement, 8)];


        bill.remainder_notification = sqlite3_column_int(getStatement, 9);


        bill.payment_type = [NSString stringWithUTF8String:(char *)sqlite3_column_text(getStatement, 10)];

    [getBillsList addObject:bill];
}

success = sqlite3_finalize(getStatement);

if (success == SQLITE_ERROR)
{
    NSAssert1(0, @"Error: failed to retrieve from the database with message '%s'.", sqlite3_errmsg(database));
    //return NO;
}
else
{
    printf("Successfully retrieved data from database");
}
sqlite3_close(database);
return getBillsList;

,这仅用于检索一个字段group_name

- (NSMutableArray *)getAllGroups

    sqlite3_open([path UTF8String], &database);
int success;
NSMutableArray * getBillsList = [[NSMutableArray alloc]init];

NSString * sql = @"select group_name from bill";

sqlite3_stmt * getStatement;

if (sqlite3_prepare_v2(database, [sql UTF8String], -1, &getStatement, NULL) != SQLITE_OK)
{
    NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
}

while (sqlite3_step(getStatement) == SQLITE_ROW)
{

//这里得到了上述异常

    NSString * str = [NSString stringWithUTF8String:(char *)sqlite3_column_text(getStatement, 1)];

    [getBillsList addObject:str];
}

success = sqlite3_finalize(getStatement);

if (success == SQLITE_ERROR)
{
    NSAssert1(0, @"Error: failed to retrieve from the database with message '%s'.", sqlite3_errmsg(database));
    //return NO;
}
else
{
    printf("Successfully retrieved data from database");
}
sqlite3_close(database);
return getBillsList;

1 个答案:

答案 0 :(得分:-1)

请放一些图片或代码......

我假设你的问题并给出解决方案 为保存和获取数据创建自定义类.....对于Bill_id,也将此代码放在appdelegate.m

if (![[NSFileManager defaultManager] isReadableFileAtPath: dbPath])
{

    if ([[NSFileManager defaultManager] copyItemAtPath: yourOriginalpath toPath: dbPath error: NULL] != YES)
        NSAssert2(0, @"Fail to copy database from %@ to %@", yourOriginalpath, dbPath);
}