如何在iphone和模拟器中检查数据库是否有新行插入

时间:2011-04-19 07:11:59

标签: iphone objective-c

嗨朋友请帮助一些我想在我的数据库中看到我的新行是否添加我是怎么看到的

请一些人给我演示代码或者在我的错误中找到我的代码

这是我的appdeleget类方法

-(void)checkAndCreateDB {
    NSString* databaseName = @"demo.sqlite";
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    NSString *databasePath=[documentsDir stringByAppendingPathComponent:@"demo.sqlite"];
    databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    success = [fileManager fileExistsAtPath:databasePath];
    if(success) return;
    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
}


-(void)insertData{

    sqlite3 *database;
    sqlite3_stmt *statement=nil;
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    NSString *databasePath=[documentsDir stringByAppendingPathComponent:@"demo.sqlite"];
    if (sqlite3_open([databasePath UTF8String], &database) != SQLITE_OK)
    {
        sqlite3_close(database); NSAssert(0, @"Failed to open database");
    }
    NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO User (uname,id,password,message) VALUES (\"%@\",\"%d\", \"%@\", \"%@\")",Gmessage,1 ,Glocation,Gjourney];
    if(sqlite3_prepare_v2(database, [insertSQL UTF8String], -1, &statement, NULL)!= SQLITE_OK)
    {
        sqlite3_bind_text(statement, 1, [Gjourney UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(statement, 2, [Glocation UTF8String], -1, SQLITE_TRANSIENT);
        sqlite3_bind_text(statement, 3, [Gmessage UTF8String], -1, SQLITE_TRANSIENT);
        if (SQLITE_DONE !=sqlite3_step(statement)) 
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Add Record" message:@"Contact Added" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];    
            [alert show];
            [alert release];
            alert=nil;

        }
        else 
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"record" message:@"record not created" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];   
            [alert show];
            [alert release];
            alert=nil;
        }   

        sqlite3_reset(statement);

    }
    sqlite3_finalize(statement);    

}

这是我的控制器类按钮代码

-(IBAction)Start:(id)sender{

    Gjourney=mTextJourney.text;
    Glocation=mTextLocation.text;
    Gmessage=mDescription.text;
    AddListAppDelegate *appDelegate =(AddListAppDelegate *)[[UIApplication sharedApplication]delegate];
    [appDelegate insertData];
}

1 个答案:

答案 0 :(得分:0)

您可以使用sqlite3_last_insert_rowid函数检查是否添加了行,只要您没有从多个线程访问数据库等。