【问题标题】:the insert method not working插入方法不起作用
【发布时间】:2012-08-05 03:55:28
【问题描述】:

我正在研究 sqlite 数据库,试图插入数据库,但它无法正常工作。我搜索了一些帖子,还写了 reset 和 finalize 语句。


(void)addLimittypeForeign
{
  // Create insert statement for the person

  if(sqlite3_open([dbPath UTF8String],&database) == SQLITE_OK)
  {
    NSString *querySQL = [NSString stringWithFormat: 
         @"insert into Limittabel (limitid,limitAmt,Profileid) Values (?,?,?)"];

    char *errmsg=nil;

    if(sqlite3_exec(database, [querySQL UTF8String], NULL, NULL, &errmsg)
         != SQLITE_OK )
    {
      NSLog(@"YES THE DATA HAS BEEN WRITTEN SUCCESSFULLY");
    }
  }
  sqlite3_close(database);
}

请帮帮我。谢谢

【问题讨论】:

  • “它不能正常工作”到底是什么意思?哪些有效,哪些无效?

标签: iphone objective-c ipad ios5 nsstring


【解决方案1】:

使用此代码插入数据库....根据您的要求更改代码...对我来说很好...

-(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);
}

【讨论】:

  • -(void)addLimitType:(DepositCash *)depositobj { [depositobj addLimittypeForeign]; [arrLimitType addObject:depositobj]; }
  • 这种方法在Appdelegate中使用
  • 把它放在 appdelegate.m.. NSArray *documentPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentDir = [documentPath objectAtIndex:0]; databasePath = [documentDir stringByAppendingPathComponent:@"QuotesApp.sqlite"]; [self checkAndCreateDatabase];
  • -(void)checkAndCreateDatabase { BOOL 成功; NSFileManager *fileManager = [NSFileManager defaultManager];成功 = [fileManager fileExistsAtPath:databasePath];如果(成功)返回; NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"QuotesApp.sqlite"]; [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil]; [文件管理器发布]; }
  • 并在 appdelegate.h NSString *databasePath;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-26
  • 1970-01-01
  • 2018-06-04
  • 2016-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多