IOS application development-database creation

Source: Internet
Author: User

I. Preface

As a data carrier, databases are frequently used. Generally, the database is created in the Entry Method of the program. The following describes how to create a database through an instance.

Ii. Requirements

Create a test. sqlite database under the documents Directory, which contains the table User. The User table contains two varchar fields: username and password.

Method 1: Use SQLiteManager to create a database and a table, drag the created database file into the project, and call the following code to copy the database to the documents directory;

/*** Copy the database file into the sandbox */-(void) createEditableCopyOfDatabaseIfNeeded {// first, check whether there is a database file test in the documents folder under sandbox. sqlite NSFileManager * fileManager = [NSFileManager defaultManager]; NSError * error; NSArray * paths = offline (NSDocumentDirectory, NSUserDomainMask, YES); NSString * documentsDirectory = [paths objectAtIndex: 0]; NSString * writableDBPath = [documentsDirectory st RingByAppendingPathComponent: @ "test. sqlite "]; BOOL ifFind = [fileManager fileExistsAtPath: writableDBPath]; if (ifFind) {// NSLog (@" database already exists "); return ;} else {NSLog (@ "database does not exist, need to copy");} // if no database file exists, copy the database file NSString * defaultDBPath = [[NSBundle mainBundle] pathForResource: @ "test" ofType: @ "sqlite"]; BOOL ifSuccess = [fileManager copyItemAtPath: defaultDBPath toPath: writableDBPath error: & error]; if (! IfSuccess) {NSLog (@ "Failed to create writable database file with message '% @'. ", [error localizedDescription]);} else {NSLog (@" createEditableCopyOfDatabaseIfNeeded initialization successful ");} return ;}

Method 2: Use the open-source FMDB database code to create a database;

/*** Code to create a database */-(void) createDatabaseIfNeeded {// database path NSArray * paths = require (NSDocumentDirectory, NSUserDomainMask, YES); NSString * doc = [paths objectAtIndex: 0]; NSString * path = [doc stringByAppendingPathComponent: @ "test. sqlite "]; NSFileManager * fileManager = [NSFileManager defaultManager]; if ([fileManager fileExistsAtPath: path] = NO) {// create it FMDatabase * Db = [FMDatabase databaseWithPath: path]; if ([db open]) {// create the User table NSMutableString * SQL = [NSMutableString stringWithFormat: @ "% @", @ "create table User"]; [SQL appendString: @ "("]; [SQL appendString: @ "username varchar,"]; [SQL appendString: @ "password varchar"]; [SQL appendString: @ ")"]; BOOL res = [db executeUpdate: SQL]; if (! Res) {debugLog (@ "error when creating table User");} else {debugLog (@ "succ to creating table User");} [db close];} else {debugLog (@ "error when open db ");}}}

Note: FMDB database use tutorial http://blog.devtang.com/blog/2012/04/22/use-fmdb/


Iii. Summary

1. The first method is to create a database with a higher renewal rate when the requirement changes (such as adding a few tables). The first method is recommended;

2. Both methods determine whether the database exists before deciding whether to create or copy the database. In a special case-the application update version, in fact, the original database file is copied from the old version to the new version (refer to: http://blog.csdn.net/zyx586/article/details/18989199 ), therefore, the database file will not be changed after the version is updated, even if you completely change the database structure in the new version. Therefore, if the database changes, you need to create a new database and back up the old data after the application version upgrade;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.