2012-02-01 37 views
2

我要使用同一个数据库对象的委托文件自定义创建DATABSE头文件的&控制器文件,其中包含的类型SQLITE3 &你方法 像OpenDB & CREATETABLE & InserData对象。SQLite数据库应用

IM,现在刚刚初学iPhone编程的话,请帮我

回答

1

应用程序委托文件就像是一个全球性的文件,只需导入应用程序委托你自定义创建的数据库的头文件,并创建对象的自定义创建的数据库的头文件,假设文件名是dbOperations.h

在应用程序委托

进口,并创造了objec吨,然后,做财产,综合,这就是全部。

在你只需要导入您的“appDelegate.h”文件,并使其对象视图做负载像

任何视图控制器

objAppdelegate = [[UIApplication的sharedApplication]代表];

,你可以通过

objAppdelegate.objdbOperations访问你所有的方法和dbOperations.h文件的成员。

无论你放在appdelegate中,对于整个应用程序,所有viewControllers和所有类都将保持共享和通用。

你的createDatabase方法应该是这样的,并且当你的应用程序没有完成appDelegate文件的加载方法时调用这个方法。

-(void)checkAndCreateDatabase 
{ 
    appDelegate = (SexarobicsAppDelegate *)[[UIApplication sharedApplication]delegate]; 
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDir =[documentPaths objectAtIndex:0]; 
    NSString *databasePath = [documentsDir stringByAppendingPathComponent:@"database.sqlite"]; 
    //NSLog(@"%@", databasePath); 

    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    if(![fileManager fileExistsAtPath:databasePath]) 
    { 
     NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"database.sqlite"]; 
     [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil]; 
    } 

    //Open DB Connection 
    if(sqlite3_open([databasePath UTF8String], &database) != SQLITE_OK) 
     sqlite3_close(database); 
    return; 
} 

and firing sql query like this fashion,in above code @“database.sqlite”是你的db文件名。

-(void)getData 
{ 
    selectStmt = nil; 

    // fire query and perform those related operations 

    // Release the compiled statement from memory 
    sqlite3_finalize(selectStmt); 
    selectStmt = nil; 
} 
+1

Thnak you @Hadley for your support。 但我的项目建立和运行,但我得到三个警告和 即使连接到数据库无法打开。 – 2012-02-02 12:29:24

+1

Mr. @ Hadley 我在上面的项目中实际做了什么,看到我的下一个问题。 – 2012-02-02 12:37:37

+0

可能你没有在你的create database方法中写入open database语句。您需要在执行任何数据库操作之前打开数据库(不确定选择查询的情况,但如果您在选择操作中也打开数据库,则会更好)。 – HarshIT 2012-02-02 12:45:53