2012-01-27 43 views
1

我想将3段数据添加到SQLite3数据库。在教程的帮助下,我快到了。以下是我的代码。从Xcode内向SQLite3数据库添加数据

我收到了我期望从NSLogs得到的结果。我发现数据库没有问题,我编译声明发送到数据库没有问题,但当应用程序到达sqlite3_prepare_v2(database, sqlstatement, -1, &compliedstatement, NULL)==SQLITE_OK我期待数据被添加到DB,但它不是,我也期待显示一个UIAlertView以告诉用户ERROR OS SUCCESS。我希望你能看到我没有的东西。

- (IBAction)addData:(id)sender { 
    NSString *n = [[NSString alloc] initWithString:[name text]]; 
    NSString *a = [[NSString alloc] initWithString:[age text]]; 
    NSString *c = [[NSString alloc] initWithString:[color text]]; 
    NSLog(@"%@ - %@ - %@",n,a,c); 
    [self insertDataName:n Age:a Color:c]; 
} 

这个NSLog如预期的那样返回文本属性。

- (IBAction)hide:(id)sender { 
    [name resignFirstResponder]; 
    [age resignFirstResponder]; 
    [color resignFirstResponder]; 

} 



- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    DBName = @"mydatabase.sqlite"; 
    NSArray *documentsPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDir = [documentsPaths objectAtIndex:0]; 
    DBPath = [documentsDir stringByAppendingPathComponent:DBName]; 

} 


-(void)checkAndCreateDB { 
    BOOL Success; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    Success = [fileManager fileExistsAtPath:DBPath]; 

    if(Success) { 
     NSLog(@"DB Found");  
     return; 
    } 

    NSLog(@"DB Not Found"); 
    NSString *databasePathFromApp = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:DBName]; 
    [fileManager copyItemAtPath:databasePathFromApp toPath:DBPath error:nil]; 

} 

NSLog回报

-(void)insertData Name:(NSString*)n Age:(NSString*)a Color:(NSString*)c { 
    [self checkAndCreateDB]; 
    sqlite3 *database; 
    if (sqlite3_open([DBPath UTF8String],&database)==SQLITE_OK) { 
     NSString *statement; 
     sqlite3_stmt *compliedstatement; 
     statement = [[NSString alloc] initWithFormat:@"insert into table1 values ('%@', '%@', '%@')",n,a,c]; 
     const char *sqlstatement = [statement UTF8String]; 
     NSLog(@"%@",statement); 

     if (sqlite3_prepare_v2(database, sqlstatement, -1, &compliedstatement, NULL)==SQLITE_OK) { 

     if (SQLITE_DONE!=sqlite3_step(compliedstatement)) { 
      NSAssert1(0, @"Error by inserting '%s'", sqlite3_errmsg(database)); 
      UIAlertView *AlertOK = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"Error by inserting" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];  

      [AlertOK show]; 

     } 
     else { 
      UIAlertView *AlertOK = [[UIAlertView alloc] initWithTitle:@"Success!" message:@"Data successfully inserted" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];    
      [AlertOK show]; 
      } 
     } 
     sqlite3_finalize(compliedstatement); 

    } 
    sqlite3_close(database); 

} 

而最后NSLog上面显示insert into table1 values ('n', 'a', 'c')为我所期望的。

插入时出现错误,我无法弄清楚。

仅供参考数据库是使用SQLiteManager创建的。 SS下面:

SQLiteManager table1 SS

+0

你能更具体的到底发生了什么?你的陈述已经准备好,但是你面对的是什么样的错误?这是(SQLITE_DONE!= sqlite3_step(compliedstatement))执行?那么发生了什么? – Byte 2012-01-27 20:28:47

+0

我得到了'UIAlertView'成功,但是当我在SQLiteManager中打开我的数据库并从'table1'中选择*时,有0个条目。 – 2012-01-28 00:08:36

回答

1

好的,您是工作在模拟器或者的iDevice?在iDevice中,您无法创建外部SQL数据库(至少据我所知)。你必须动态创建它,因为沙盒。 “仅供参考,数据库是使用SQLiteManager创建的。”检查一下。

如果这不是问题尝试从 “准备......” 改变你的代码,以 “执行”

从...

if (sqlite3_prepare_v2(database, sqlstatement, -1, &compliedstatement, NULL)==SQLITE_OK) { 

    if (SQLITE_DONE!=sqlite3_step(compliedstatement)) { 
     NSAssert1(0, @"Error by inserting '%s'", sqlite3_errmsg(database)); 
     UIAlertView *AlertOK = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"Error by inserting" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];  

     [AlertOK show]; 

    } 
    else { 
     UIAlertView *AlertOK = [[UIAlertView alloc] initWithTitle:@"Success!" message:@"Data successfully inserted" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];    
     [AlertOK show]; 
     } 
    } 
    sqlite3_finalize(compliedstatement); 

要...

sqlite3_exec(database, [sqlstatement UTF8String], NULL, NULL, &error); 

做一个登录“错误”,因为这是非常重要的。

如果你想弹出一个UIAlert做到这一点

if (error !=nil) 
    //show alert for error 
else 
    //show alert for success 
+0

伟大的回答,这指出我在正确的方向。谢谢! – 2012-01-30 19:12:33