在插入到iphone的sqlite数据库之前检查数据是否存在

时间:2012-01-04 06:44:55

标签: iphone ios database sqlite insert

我想将数据插入到sqlite中...虽然插入我的代码中的重复...我看到了与它相关的问题,我明白了这一点。任何人只是帮我用这段代码检查数据是否存在在插入数据库之前......

- (IBAction)AddtoFavourites:(id)sender {
    sqlite3 *database;
    dbName=@"dataTable.sqlite";
    NSArray *documentpath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentdir=[documentpath objectAtIndex:0];
    dbPath=[documentdir stringByAppendingPathComponent:dbName];
    if(sqlite3_open([dbPath UTF8String], &database)==SQLITE_OK){
//I want to check data it exists already into database....
       NSString *sqlStatement=[NSString stringWithFormat:@"insert into Persons(PersonName,CompanyName,ImgUrl)values(\"%@\",\"%@\",\"%@\")",model.personName,model.companyName,model.imgurl];
            const char *insertSQL=[sqlStatement UTF8String];
        char *errMsg;
        sqlite3_exec(database, insertSQL, NULL,NULL,&errMsg);
        NSLog(@"Add to Favourites");        
    }
}

1 个答案:

答案 0 :(得分:3)

首先对任何列使用select查询:

    const char *sqlStatement = "SELECT PersonName FROM Persons";

我猜你会把sqlStatement分配到compiledStatement

            while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
            // Read the data from the result row
            NSString *aName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];


            [dataArray addObject:aName];
            NSLog(@"%@",aName);
 }

检查一下