Third-party libraries used by the database FMDB

Source: Internet
Author: User

Download FMDB

1. Introduction of the Sqlite3 Toolbox, the introduction of the header file in the class to be used for database operations: Because third-party software also uses the SQLite Toolbox to manipulate the database, but simplifies the operation, so that the syntax closer to the OC syntax, without the need to use too much C syntax;

#import <sqlite3.h>

2. Loading the third-party library into the project: The method is to drag and drop the FMDB source file into the project directly;

3. Accessing the database using a third-party library

Of course, for the master, the third-party library to understand, to get started is very fast, for small white, can only step by step.

3.1 Specify the storage path of the database, usually in the sandbox root directory under the Documents folder, the file suffix is. sqlite: Db_students.sqlite;

nsstring *filepath = [Nshomedirectory () stringByAppendingPathComponent:@ "documents/db_ Student.sqlite"];

3.2 First create a Fmdatabase object *_db;

fmdatabase *_db;

Initialize first before use

_db = [[Fmdatabase alloc] initwithpath:filepath];

Look at its initialization method: there is no extra action in the initialization method, except for specifying the path to the database store, there is no other action, 0x00 is a hexadecimal address that represents nil (or NULL)

To create and open a database:

[_db Open];

There are two functions of this code:

1) If the database does not exist, it is created and opened;

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

Perhaps you remember: sqlite3_open (path, &_db);

The two lines of code are the same, except that the former is closer to the OC syntax, and its essence is the operation of the database through the latter.

3.3 Creating 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 first, determine if the table already exists, or create the table if it does not exist

The whole process is:

NSString *filepath = [Nshomedirectory () stringByAppendingPathComponent:@"Documents/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 when you are done with the database

3.4 Inserting, deleting, updating, querying table operations

First step: Open the Database

Part II: Database operations

Part III: Closing the database

It is important to note that when inserting, deleting, updating a table, the method called is -(BOOL) Executeupdate: (nsstring*) SQL, ...;

See Example:

-(void) inserttable: (zystudent *) stu{    if  ([_db Open])    {        [_db Executeupdate:@ "insert into tb_students (name, age) VALUES (?,?) " , Stu.name, [NSNumber NumberWithInt:stu.age]];     // Here's what to note: Replace the SQL statement? , you cannot directly use the basic type of data, but instead need to convert the base type to the object type, which conforms to the OC syntax         [_db close];}

When querying a table, the method to invoke is: -(Fmresultset *) ExecuteQuery: (nsstring*) SQL, ...;

The reason is probably also know: Insert, delete, update operation, the main manifestation of the change is the data table, the affected time table of the row (that is, one or several records), and the query operation is different, the purpose of the query operation is to obtain the data in the table, then you should get the data storage, This new reference is made to a concept: result set (ResultSet).

In the query operation: can be simply understood as the result set is used to receive the query data, and then you can remove the data from the result set, through a certain means to display.

- (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);}

Third-party libraries used by the database FMDB

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.