解决方法
使用FBDB Framework可以使sqlite数据库交互变得简单干净. FMDB是sqlite C接口的Objective-C包装器.
参考价值:
Sample Project With Storyboard
初始设置
像应用程序捆绑中的任何其他文件一样添加sqlite数据库,然后使用以下代码将数据库复制到文档目录,然后从文档目录中使用数据库
>首先下载FMDB framework
>提取框架现在从src / fmdb文件夹(而不是src / sample或src / extra文件夹)复制所有文件.
>在Xcode的左边列中点击你的项目.
>点击中间栏中的主要目标.
>单击“构建阶段”选项卡.
>展开“使用库链接二进制”旁边的箭头.
>点击“”按钮.
>搜索libsqlite3.0.dylib并双击它.
将您现有的数据库复制到doFinishLaunchingWithOptions中的应用程序文档中:并保持数据库路径通过应用程序.
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]; }