无法将数据存储在iphone的数据库中

时间:2013-03-14 09:47:16

标签: iphone ios

我是iphone编程新手。我使用Mac 10.7.3,因为我无法在数据库中存储数据我通过textfield将数据存储在数据库中。任何人都可以告诉我为什么它不在数据库中添加数据。这段代码中的错误是什么

 NSString *docsDir;
    NSArray *dirPaths;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
// Build the path to the database file
    databasePath =  [docsDir stringByAppendingPathComponent: @"test.db"];
    NSLog(@"%@",databasePath);
    NSFileManager *fn=[NSFileManager defaultManager];
    NSError *error;
    BOOL success=[fn fileExistsAtPath:databasePath];

    if(!success) {

NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"test.db"];
success = [fn copyItemAtPath:defaultDBPath toPath:databasePath error:&error];
    }
    //NSLog(@"sdasdassd%@",databasePath);
    NSLog(@"hai");
    sqlite3_stmt  *statement;

    const char *dbpath = [databasePath UTF8String];
            if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
        {

            NSLog(@"hai2");
 NSString *insertSQL = [NSString stringWithFormat: @"insert into path(imagepath,audiopath) values (\"%@\",\"%@\")",name.text,email.text];

            const char *insert_stmt = [insertSQL UTF8String];

            sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL);
            if (sqlite3_step(statement) == SQLITE_DONE)
            {
                    NSLog(@"details added");    

            } else {
                    NSLog(@"details not added");    
            }
            sqlite3_finalize(statement);
            sqlite3_close(contactDB);

        }
    }

由于 阿斯拉姆

1 个答案:

答案 0 :(得分:0)

首先,当应用程序加载时,将数据库复制到应用程序文件夹

App Delegate文件的Code ::

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

      NSString *docsDir;
      NSArray *dirPaths;

      // Get the documents directory
      dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
      docsDir = [dirPaths objectAtIndex:0];

      //Build the path to the database file
      databasePath =  [docsDir stringByAppendingPathComponent: @"test.db"];
      NSLog(@"%@",databasePath);

      NSFileManager *fn=[NSFileManager defaultManager];
      NSError *error;

      BOOL success=[fn fileExistsAtPath:databasePath];

      if(!success) {

          NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"test.db"];
          success = [fn copyItemAtPath:defaultDBPath toPath:databasePath error:&error];
      }
}

然后,在“保存”按钮上单击,
这里,[app dbpath]是从委托文件访问的NSString。
代码::

 sqlite3_stmt  *statement;

 const char *dbpath = [databasePath UTF8String];

 if (sqlite3_open([[app dbpath] UTF8String], &contactDB) == SQLITE_OK) {
 {
    NSString *insertSQL = [NSString stringWithFormat: @"insert into path(imagepath,audiopath) values (\"%@\",\"%@\")",name.text,email.text];

    const char *insert_stmt = [insertSQL UTF8String];

    if (sqlite3_prepare_v2(contactDB, insert_stmt, -1, &statement, NULL) == SQLITE_OK) {

        NSLog(@"details added");

    }

    else {
        NSLog(@"details not added");
    }

    sqlite3_finalize(statement);
    sqlite3_close(contactDB);
}

这可能对你有所帮助 感谢。