我有点麻烦 使这个工作。
这是我要执行的代码 :
[self openDB];
sqlite3_stmt *statement;
NSString *sql2 = [NSString stringWithFormat:@"INSERT INTO CheckList (CLName, DateAdd, Active, Costum, Percentage, UserId) VALUES ( ola232332332324 , 2012-02-03 , 1, 1, NULL, 1)"];
if(sqlite3_prepare_v2(db, [sql2 UTF8String], -1, &statement, NULL) == SQLITE_OK)
{
alert1 = [[UIAlertView alloc]
initWithTitle:@"Grats" message:@"The query was executed!" delegate:self cancelButtonTitle:@"Continue" otherButtonTitles: nil];
[alert1 show];
[alert1 release];
}
else {
NSAssert(0, @"Failed to execute");
}
sqlite3_finalize(statement);
这是我用来调用DB的代码:
-(NSString *) filePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:@"prosegur.sqlite"];
}
-(void) openDB {
sqlite3_open([[self filePath] UTF8String], &db);
if (sqlite3_open([[self filePath] UTF8String], &db) != SQLITE_OK){
NSAssert(0, @"Failed to open db");
}
}
Edit: 试图使用调频数据库,这里是代码 :
FMDatabase *database = [FMDatabase databaseWithPath:@"prosegur.sqlite"];
if([database open]){
NSLog(@"yes");
[database executeUpdate:@"INSERT INTO CheckList (CLName, DateAdd, Active, Costum, UserId) VALUES ( cenas , 2012-02-0 , 1, 1, 1)"];}
else
{
NSLog(@"not");
}
通过 [数据库打开] 校验, 但它不执行查询 。
EDIT2: 已经做了建议,我开始认为它建立了一个数据库,在数据库内执行查询,但由于它不存在任何表格,它忽略了这一事实。
最新代码如下:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [[paths objectAtIndex:0] retain];
NSString *path = [[docsPath stringByAppendingPathComponent:@"prosegur.sqlite"] retain];
FMDatabase *database = [FMDatabase databaseWithPath:path];
[database open];
if([database open])
{
NSLog(@"yes");
[database executeUpdate:@"INSERT INTO CheckList (CLName) VALUES (?)", @"cenas"];
}
else
{
NSLog(@"no");
}
[database close];