创建数据库
2012-02-27 14:32 java环境变量 阅读(228) 评论(0) 编辑 收藏 举报- (BOOL)openDataBase
{
NSArray *paths = NSSearchPathForDirectoriesInDomains ( NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex : 0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"MyDB.sql"];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL find = [fileManager fileExistsAtPath : path];
//找到数据库文件mydb.sql
if (find)
{
if(sqlite3_open([path UTF8String], &dataBase) != SQLITE_OK)
{
sqlite3_close(dataBase);
NSLog(@"Error: open database file.");
return NO;
}
return YES;
}
if(sqlite3_open([path UTF8String], &dataBase) == SQLITE_OK)
{
if (![self createTable:dataBase])//创建表
{
sqlite3_close(dataBase);
NSLog(@"创建表失败");
return NO;
}
return YES;
}
else
{
sqlite3_close(dataBase);
NSLog(@"Error: open database file.");
return NO;
}
return NO;
}
-(BOOL) createTable : (sqlite3 *) database
{
char *error;
NSString *createSQLite = @"create table if not exists LOVE(image_ID integer primary key,image BLOB)";//primary key 可不要漏掉
if(sqlite3_exec(database,[createSQLite UTF8String],NULL,NULL,&error)!=SQLITE_OK)
{
sqlite3_close(database);//出错误了??那查看一下是否链接好数据库了··
return NO;
}
return YES;
}