2012-06-15 100 views
-4

我試圖在iPhone中的sqlite數據庫中插入自定義對象可以告訴我如何做到這一點。如果可能給我的源代碼,所以我可以很容易地理解。而在此先感謝在Sqlite3數據庫中插入自定義對象

+0

插入行? – Krunal

+1

你的意思是在另一個數據庫的表中插入一個數據庫?我不認爲這是可能的。你可以插入字符串,數組,字典,甚至圖像。我從來沒有看到任何人插入數據庫。 – Dzy

回答

2

使用此代碼插入是數據庫....改變代碼,根據您的要求...工作對我罰款......在DB

-(void) insertDataIntoDatabase 
{ 
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); 

    NSString *documentsDir = [documentPaths objectAtIndex:0]; 

    databasePath = [documentsDir stringByAppendingPathComponent:@"dataBaseName.sqlite"]; 

    NSLog(@"%@",sender); 

    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) 
    { 
     NSLog(@"%@",[quoteDic objectForKey:@"bookmark"]); 
     NSLog(@"%d",[[quoteDic objectForKey:@"quoteId"] intValue]); 
     NSString *sqlStatement=[NSString stringWithFormat:@"insert into tableName (frndName,frndDescription,frndAddress,frndMobile,frndEmail,frndCollege,frndCompany) Values('%@','%@','%@','%@','%@','%@','%@')",[frndName text],[frndDescription text],[frndAddress text],[frndMobile text],[frndEmail text],[frndCollege text],[frndCompany text]]; 

     sqlite3_stmt *compiledStatement; 
     if(sqlite3_prepare_v2(database, [sqlStatement UTF8String], -1, &compiledStatement, NULL) == SQLITE_OK) 
     { 
     } 
     if(sqlite3_step(compiledStatement) != SQLITE_DONE) 
     { 

      //sqlite3_close(database); 
      NSAssert1(0, @"Error while Updating data. '%s'", sqlite3_errmsg(database)); 
      sqlite3_finalize(compiledStatement); 
     } 
     else 
     { 
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Update!!" 
                  message:@"Your record Updated" 
                  delegate:nil 
                cancelButtonTitle:@"Thankyou" 
                otherButtonTitles:nil]; 
      [alert show]; 
      [alert release]; 

     } 

     sqlite3_finalize(compiledStatement); 
    } 
    sqlite3_close(database); 
}