Sqlite数据库应用程序

发布于 2025-01-01 02:25:42 字数 130 浏览 2 评论 0原文

我想在委托文件和代理文件中使用相同的数据库对象自定义创建的数据库头文件的控制器文件,其中包含 Sqlite3 类型的对象三种方法 像 OpenDB 和创建表&插入数据。

我现在只是 iPhone 编程的初学者,所以请帮助我

I want to use same database object at Delegate file & Controller File of Custom Created databse header file which Contains object of type Sqlite3 & thee methods
like OpenDB & CreateTable & InserData.

i m right now just beginner in iphone programming so, pls Help me

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

前事休说 2025-01-08 02:25:42

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

在应用程序委托中

导入它并创建它的对象,然后,执行属性,合成,仅此而已。

在任何视图控制器中,您只需要导入“appDelegate.h”文件并使其视图中的对象确实加载,如

objAppdelegate = [[UIApplication sharedApplication]delegate];

访问 dbOperations.h 文件的所有方法和成员

您可以通过objAppdelegate.objdbOperations

。你在 appdelegate 中放入的任何内容都将保持对整个 app 、所有 viewController 和所有类的共享和通用。

您的 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; 
}

并像这样触发sql查询,在上面的代码中@“database.sqlite”是你的数据库文件名。

-(void)getData
{
    selectStmt = nil;

     // fire query and perform those related operations

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

app delegate file is like a global file , you just import the your Custom created database header file in app delegate , and create the object of Custom created database header file , assuming the file name is dbOperations.h

in app delegate

import it and create its object, then , do property , synthesize and that's all.

in any view controller you just need to import your "appDelegate.h" file and make its object in view did load like

objAppdelegate = [[UIApplication sharedApplication]delegate];

and you may access your all methods and members of dbOperations.h file by

objAppdelegate.objdbOperations .

whatever you put in appdelegate will remain shared and common to entire app , all viewControllers and all classes.

your createDatabase method should be like this, and call this method when your application did finished loading method of appDelegate file.

-(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" is your db file name.

-(void)getData
{
    selectStmt = nil;

     // fire query and perform those related operations

    // Release the compiled statement from memory
    sqlite3_finalize(selectStmt);
    selectStmt = nil;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文