我使用以下代码在 Xcode 中创建数据库。它可以平稳地运行到NSFilemanager
代码,但之后它将终止到显示无法创建表的else
代码。status.text=@"failed to open/create database";
我导入sqlite3.h
并创建了sqlDatabase
引用变量sqlite3
,但它仍然不起作用。
-(void)databaseCreate
{
NSString *docsDir;
NSString *dbPath;
NSArray *dirPath;
dirPath=NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSLog(@"dirpath::%@",dirPath);
docsDir=[dirPath objectAtIndex:0];
NSLog(@"document directory::%@",docsDir);
dbPath=[[NSString alloc] initWithString:[docsDir stringByAppendingPathComponent:@"timerpro1.db"]];
NSLog(@"database path::%@",dbPath);
NSFileManager *fileManager=[NSFileManager defaultManager];
if([fileManager fileExistsAtPath: dbPath] == NO)
{
const char *databsPath=[dbPath UTF8String];
NSLog(@"treat filemanager");
if(sqlite3_open(databsPath,&sqlDatabase) == SQLITE_OK)
{
char *err;
NSLog(@"create inside");
const char *sql_stmt="CREATE TABLE IF NOT EXISTS PRJDATA(ID INTEGER PRIMERY KEY AUTOINCREMENT,PRJ_NAME TEXT,PRJ_DATE TEXT,TIME_POINT1 TEXT,TIME_POINT2 TEXT,TIME_POINT3 TEXT,POINT2_DIFF_MIN TEXT,POINT2_DIFF_SEC TEXT,POINT3_DIFF_MIN TEXT,POINT3_DIFF_SEC TEXT)";
if (sqlite3_exec(sqlDatabase, sql_stmt, NULL, NULL, &err)!=SQLITE_OK)
{
status.text=@"failed to create table";
}
sqlite3_close(sqlDatabase);
}
else
{
status.text=@"failed to open/create database";
}
}
[fileManager release];
}