2013-03-14 71 views
0

我是新来使用Mac 10.7.3 iphone programming.Iam在我不能够通过textfield.Can存储在数据库中,我存储在数据库里面的数据资料的任何身体告诉我为什么它的database.What里面不添加的数据是错误在此代码无法在数据库中的数据存储在iPhone

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

     } 
    } 

感谢 阿斯拉姆

+0

你在库中检查 - >模拟器 - >应用程序 - >应用程序 - >文档 - > sqlite数据库吗?检查是否创建表和字段。? – 2013-03-14 09:57:24

+0

很可能你没有报告任何错误。 – 2013-03-14 11:23:34

+0

错误或不coming.In控制台其显示该信息不是给重播添加 – Mohammed 2013-03-14 11:33:04

回答

0

首先,当应用负载,然后复制到数据库应用程序文件夹

应用程序代理文件的代码::

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

它可能会帮助到你。
谢谢。

+0

感谢。[应用DBPATH]这里它呈现出一些错误 – Mohammed 2013-03-14 11:15:42

+0

u能告诉我,为什么它的数据库 – Mohammed 2013-03-14 11:16:26

+0

内不存储数据做ü检查其上创建数据库在模拟器文件夹中。与您在应用程序中创建的相同吗? – 2013-03-14 11:20:14