Lan Yi iOS Learning SQL database

Source: Internet
Author: User

Import header file in current class

1. Open the Database

Create a path string for the database file

NSString *path = @ "/users/boris/public/Lan Yi third period/day31-sqlite/test.db";

Create a database pointer, assign the value to null first, and later he will be assigned a value.

Just in case the wild pointer appears, and C is null, and OBJC's null is nil.

Sqlite3 *db = NULL;

Open the database and assign an open database to the DB pointer.

The first parameter is the database file location, the second parameter is a pointer, a pointer to a pointer-type variable

The object is created with pointers to this pointer because the function return value has other effects and cannot be used to return a database variable. But there is a need for this function to "return" multiple values (execution state, database variable instance), so only one result (execution state) can be returned by the traditional return value, and then indirectly by the parameter "return" another result (database variable instance).

int res_open = Sqlite3_open (path. Utf8string, &db);

if (Res_open = = SQLITE_OK) {

NSLog (@ "Successfully open database");

1. Create a table

[Self executesqlite:db withsql:@ ' CREATE table if not exists t_newtable (ID integer primary key autoincrement, name text, a GE integer, Math real) "];

2. Inserting data

[Self executesqlite:db withsql:@ "insert to T_newtable (name, age, math) VALUES (' Tom ', 10, 100) '];

3. Query data

[Self quarydata:db];

} else {

NSLog (@ "Failed to open database, failure code:%d", res_open);

}

}

-(void) Executesqlite: (Sqlite3 *) db Withsql: (NSString *) SQL {

Creates a new C-language string variable pointer, ready to receive the error message later.

char *errmsg = NULL;

/**

* Execute specific SQL statements by calling the Sqlite3_exec () function

* The first parameter is: which database to execute? Pass a database variable in

* The second parameter is: What does it do? Pass in the C language string of an SQL statement

* The third and fourth are callbacks, which are not directly assigned null (note that the C language is not the same as "null" in the OBJC language, NULL, nil)

* The fifth parameter is: If an error message is generated, where do you want to save the error message? Passes in a string pointer.

* Return value: Is an integer that represents the result of the current function execution, such as execution success or execution failure.

*/

int res_exec = sqlite3_exec (db, SQL. Utf8string, NULL, NULL, &ERRMSG);

Verify that the current SQL statement is successful by judging the return value of Sqlite3_exec ()

Res_exec = = Sqlite_ok? /* NSLOG (@ "SQL statement execution succeeded!") */: NSLog (@ "SQL execution failed, Reason:%s", errmsg);

}

Lan Yi iOS Learning SQL database

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.