Third-party database used by databases FMDB and third-party fmdb

Source: Internet
Author: User

Third-party database used by databases FMDB and third-party fmdb

Download FMDB

 

1. introduce the sqlite3 toolbox and introduce header files in the class for database operations: because third-party software also uses the sqlite toolbox to operate the database, it only simplifies the operation, make the syntax closer to the OC syntax without using too many C syntax;

#import <sqlite3.h>

2. Add a third-party library to the project: drag the source file of FMDB directly into the project;

3. Access the database using a third-party database

Of course, for experts, it is quick to get started after learning about third-party libraries. for Tom, you can only go step by step.

3.1 specify the storage path of the database, which is usually in the Documents folder under the root directory of the sandbox. The file suffix is. sqlite: such as db_students.sqlite;

NSString * filePath = [NSHomeDirectory () stringByAppendingPathComponent: @ "Documents/db_student.sqlite"];

3.2 first create an FMDatabase object * _ db;

FMDatabase * _ db;

Initialize before use

_ Db = [[FMDatabase alloc] initWithPath: filePath];

Check its initialization method: there are no additional operations in the initialization method, except for specifying the database storage path. 0x00 is a hexadecimal address, nil (or NULL)

Create and open a database:

[_ Db open];

This code has two functions:

1) if the database does not exist, it is created and enabled;

2) If the database already exists, open the database;

You may remember: sqlite3_open (path, & _ db );

The two statements of code have the same effect, except that the former is closer to the OC syntax, and its essence is to operate the database through the latter.

3.3 create a table

If (! [_ Db tableExists: @ "tb_students"]) {[_ db executeUpdate: @ "create table tb_students (ID integer primary key not null unique, name text, age integer) "];} // call the method to determine whether the table already exists. If not, create a table.

The entire process is:

NSString * filePath = [NSHomeDirectory () stringByAppendingPathComponent: @ "statements/db_student.sqlite"]; _ db = [[FMDatabase alloc] initWithPath: filePath]; if ([_ db open]) {if (! [_ Db tableExists: @ "tb_students"]) {[_ db executeUpdate: @ "create table tb_students (ID integer primary key not null unique, name text, age integer) "] ;}} [_ db close]; // do not forget to close the database after the database operation is completed.

3.4 insert, delete, update, and query tables

Step 1: Open the database

Part 2: Database Operations

Part 3: Shut down the database

Note that when you insert, delete, or update a table, the calling method is-(BOOL) executeUpdate :( NSString *) SQL ,...;

Example:

-(Void) insertTable :( ZYStudent *) stu {if ([_ db open]) {[_ db executeUpdate: @ "insert into tb_students (name, age) values (?, ?) ", Stu. name, [NSNumber numberWithInt: stu. age];} // note the following: Replace ?, You cannot directly use data of the basic type. Instead, you need to convert the basic type to the object type, which complies with the OC syntax [_ db close];}

When querying a table, the method called is:-(FMResultSet *) executeQuery :( NSString *) SQL ,...;

The reason is probably also known: During the insert, delete, and update operations, the main change is the data table. When the data table is affected, the rows in the table (that is, one or several records ), the query operation is different. The purpose of the query operation is to obtain the data in the table, and the data storage should be obtained. In this way, a new concept is introduced: ResultSet ).

In the query operation, we can simply understand that the result set is used to receive the query data. Then we can extract the data from the result set and display it by some means.

- (void)selectTable{    NSMutableArray *array = [NSMutableArray array];    if ([_db open])    {        FMResultSet *rs = [_db executeQuery:@"select * from tb_students"];        while ([rs next])        {            ZYStudent *stu = [[[ZYStudent alloc] init] autorelease];            stu.ID = [rs intForColumnIndex:0];            stu.name = [rs stringForColumnIndex:1];            stu.age = [rs intForColumnIndex:2];            [array addObject:stu];        }    }    [_db close];    NSLog(@"________%@", array);}

 

Related Article

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.