在iOS上使用和访问现有的SQLite数据库

前端之家收集整理的这篇文章主要介绍了在iOS上使用和访问现有的SQLite数据库前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在sqlite中有一个完全填充的数据库,我想在我的新应用程序中使用.这是相当大的,所以我想避免将其更改为另一种格式,如果可能的话.我如何使用这个数据库,它随我的应用程序一起提供?

编辑:如果我将文件放入我的“支持文件”目录中,例如,我该如何访问?如何参考?

解决方法

使用FBDB Framework可以使sqlite数据库交互变得简单干净. FMDB是sqlite C接口的Objective-C包装器.

参考价值:

FMDB Framework Docs

Sample Project With Storyboard

初始设置

像应用程序捆绑中的任何其他文件一样添加sqlite数据库,然后使用以下代码数据库复制到文档目录,然后从文档目录中使用数据库

>首先下载FMDB framework
>提取框架现在从src / fmdb文件夹(而不是src / sample或src / extra文件夹)复制所有文件.
>在Xcode的左边列中点击你的项目.
>点击中间栏中的主要目标.
>单击“构建阶段”选项卡.
>展开“使用库链接二进制”旁边的箭头.
>点击“”按钮.
>搜索libsqlite3.0.dylib并双击它.

将您现有的数据库复制到doFinishLaunchingWithOptions中的应用程序文档中:并保持数据库路径通过应用程序.

在AppDelegate中添加以下代码.

AppDelegate.m

#import "AppDelegate.h"

@implementation AppDelegate

// Application Start
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Function called to create a copy of the database if needed.
    [self createCopyOfDatabaseIfNeeded];

    return YES;
}

#pragma mark - Defined Functions

// Function to Create a writable copy of the bundled default database in the application Documents directory.
- (void)createCopyOfDatabaseIfNeeded {
    // First,test for existence.
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    // Database filename can have extension db/sqlite.
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *appDBPath = [documentsDirectory stringByAppendingPathComponent:@"database-name.sqlite"];

    success = [fileManager fileExistsAtPath:appDBPath];
    if (success) {
        return;
    }
    // The writable database does not exist,so copy the default to the appropriate location.
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"database-name.sqlite"];
    success = [fileManager copyItemAtPath:defaultDBPath toPath:appDBPath error:&error];
    NSAssert(success,@"Failed to create writable database file with message '%@'.",[error localizedDescription]);
}

YourViewController.m

选择查询

#import "FMDatabase.h"

- (void)getAllData {
    // Getting the database path.
    NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,YES);
    NSString *docsPath = [paths objectAtIndex:0];
    NSString *dbPath = [docsPath stringByAppendingPathComponent:@"database-name.sqlite"];

    FMDatabase *database = [FMDatabase databaseWithPath:dbPath];
    [database open];
    NSString *sqlSelectQuery = @"SELECT * FROM tablename";

    // Query result 
    FMResultSet *resultsWithNameLocation = [database executeQuery:sqlSelectQuery];
    while([resultsWithNameLocation next]) {
        NSString *strID = [NSString stringWithFormat:@"%d",[resultsWithNameLocation intForColumn:@"ID"]];
        NSString *strName = [NSString stringWithFormat:@"%@",[resultsWithNameLocation stringForColumn:@"Name"]];
        NSString *strLoc = [NSString stringWithFormat:@"%@",[resultsWithNameLocation stringForColumn:@"Location"]];

        // loading your data into the array,dictionaries.
        NSLog(@"ID = %d,Name = %@,Location = %@",strID,strName,strLoc);
    }
    [database close];   
}

插入查询

#import "FMDatabase.h"

- (void)insertData {

    // Getting the database path.
    NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,YES);
    NSString *docsPath = [paths objectAtIndex:0];
    NSString *dbPath = [docsPath stringByAppendingPathComponent:@"database-name.sqlite"];

    FMDatabase *database = [FMDatabase databaseWithPath:dbPath];
    [database open];    
    NSString *insertQuery = [NSString stringWithFormat:@"INSERT INTO user VALUES ('%@',%d)",@"Jobin Kurian",25];
    [database executeUpdate:insertQuery];   
    [database close];
}

更新查询

- (void)updateDate {

    // Getting the database path.
    NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,YES);
    NSString *docsPath = [paths objectAtIndex:0];
    NSString *dbPath = [docsPath stringByAppendingPathComponent:@"fmdb-sample.sqlite"];

    FMDatabase *database = [FMDatabase databaseWithPath:dbPath];
    [database open];    
    NSString *insertQuery = [NSString stringWithFormat:@"UPDATE users SET age = '%@' WHERE username = '%@'",@"23",@"colin" ];
    [database executeUpdate:insertQuery];
    [database close];
}

删除查询

#import "FMDatabase.h"

- (void)deleteData {

    // Getting the database path.
    NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,YES);
    NSString *docsPath = [paths objectAtIndex:0];
    NSString *dbPath = [docsPath stringByAppendingPathComponent:@"database-name.sqlite"];

    FMDatabase *database = [FMDatabase databaseWithPath:dbPath];
    [database open];
    NSString *deleteQuery = @"DELETE FROM user WHERE age = 25";
    [database executeUpdate:deleteQuery];   
    [database close];
}

添加功能

得到行数

确保包含FMDatabaseAdditions.h文件以使用intForQuery :.

#import "FMDatabase.h"
#import "FMDatabaseAdditions.h"

- (void)gettingRowCount {

    // Getting the database path.
    NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,YES);
    NSString *docsPath = [paths objectAtIndex:0];
    NSString *dbPath = [docsPath stringByAppendingPathComponent:@"database-name.sqlite"];

    FMDatabase *database = [FMDatabase databaseWithPath:dbPath];
    [database open];
    NSUInteger count = [database intForQuery:@"SELECT COUNT(field_name) FROM table_name"];
    [database close];
}
原文链接:https://www.f2er.com/iOS/337032.html

猜你在找的iOS相关文章