Ios 我的sqlite方法有些奇怪

Ios 我的sqlite方法有些奇怪,ios,xcode,sqlite,Ios,Xcode,Sqlite,我想做的是: 用户看到在my db中填充了现有记录的tableview 好的,我终于解决了这个问题。。。 我有一种方法可以将数据库从捆绑包复制到文档文件夹,但我没有在任何地方调用它。因此,您可以通过选择所有行并签入代码来检查药物是否已经存在于数据库中(当您找到它时,您甚至不停止查找)?你为什么不使用数据库来找出它是否已经存在?你是对的特洛伊木马的敌人。。。我刚更改了密码。 - (void)InsertMedicine:(NSString *) med { //Check to see if

我想做的是:


  • 用户看到在my db中填充了现有记录的tableview 好的,我终于解决了这个问题。。。
    我有一种方法可以将数据库从捆绑包复制到文档文件夹,但我没有在任何地方调用它。

    因此,您可以通过选择所有行并签入代码来检查药物是否已经存在于数据库中(当您找到它时,您甚至不停止查找)?你为什么不使用数据库来找出它是否已经存在?你是对的特洛伊木马的敌人。。。我刚更改了密码。
    - (void)InsertMedicine:(NSString *) med {
    
    //Check to see if the new medicine already exists
    fileMgr = [NSFileManager defaultManager];
    sqlite3_stmt *stmt=nil;
    sqlite3 *dbase;
    
    const char *sql = "SELECT * FROM listamedicine";
    
    BOOL response= NO;
    
    NSString *database = [self.GetDocumentDirectory stringByAppendingPathComponent:@"db.sqlite"];
    sqlite3_open([database UTF8String], &dbase);
    sqlite3_prepare_v2(dbase, sql, -1, &stmt, NULL);
    while(sqlite3_step(stmt) == SQLITE_ROW) {
        if ([med caseInsensitiveCompare:[NSString stringWithUTF8String:(char *)sqlite3_column_text(stmt, 1)]] == NSOrderedSame) {
            response=YES;
        }
    }
    sqlite3_finalize(stmt);
    if (!response) {
        //Insert the new medicine
        sqlite3_stmt *stmt=nil;
        NSString *stmsql = [NSString stringWithFormat:@"INSERT INTO listamedicine (nomemedicina) VALUES (\"%@\")", med];
        const char *sql2 = [stmsql UTF8String];
    
        sqlite3_prepare_v2(dbase, sql2, -1, &stmt, NULL);
    
        sqlite3_step(stmt);
    } else {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Errore" 
                                                        message:@"La medicina che stai cercando di inserire esiste già." 
                                                       delegate:nil 
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
    }
    sqlite3_finalize(stmt);
    sqlite3_close(dbase);
    
    - (NSMutableArray *)listMedicine {
    fileMgr = [NSFileManager defaultManager];
    sqlite3_stmt *stmt=nil;
    sqlite3 *mybd=NULL;
    
    
    const char *sql = "SELECT * FROM listamedicine";
    
    
    NSMutableArray *listaMedicine = [[NSMutableArray alloc]init];
    
    //Open db
    NSString *database = [self.GetDocumentDirectory stringByAppendingPathComponent:@"db.sqlite"];
    sqlite3_open([database UTF8String], &mybd);
    sqlite3_prepare_v2(mybd, sql, -1, &stmt, NULL);
    while(sqlite3_step(stmt) == SQLITE_ROW) {
        Medicina *myMedicine = [[Medicina alloc]init];
        myMedicine.dataId=[[NSNumber numberWithInt:(int)sqlite3_column_int(stmt, 0)] intValue];
        myMedicine.nome=[NSString stringWithUTF8String:(char *)sqlite3_column_text(stmt, 1)];
    
        [listaMedicine addObject:myMedicine];
    
    
    }
    sqlite3_finalize(stmt);
    sqlite3_close(mybd); 
    return listaMedicine;
    
    static NSString *CellIdentifier = @"CellaMedicina";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellaMedicina"];
    }
    
    //Get the object from the array.
    DbOperations *med = [[DbOperations alloc] init];
    self.medicine = [med listMedicine];
    Medicina *medicina = [self.medicine objectAtIndex:indexPath.row];
    
    cell.textLabel.text = medicina.nome;
    
    // Set up the cell
    
    return cell;