IOS SQLite database operation. The steps are:
First join the SQLite development Library Libsqlite3.dylib,
Create a new or open database,
Create a data table,
Insert Data,
Querying data and printing
1. Create a new project Sqlitedemo, add a library using SQLite libsqlite3.dylib
2, the method of SQLite
Sqlite3 *db, database handle, similar to file handle files
Sqlite3_stmt *stmt, an ODBC-equivalent command object that holds compiled SQL statements
Sqlite3_open (), open the database when it is created without a database.
Sqlite3_exec (), execute non-query SQL statement
Sqlite3_step (), use this function to move through the recordset after calling Sqlite3_prepare.
Sqlite3_close (), close the database file
There is also a series of functions that are used to fetch data from a recordset field, such as
Sqlite3_column_text (), take the text type of data.
Sqlite3_column_blob (), fetching blob-type data
Sqlite3_column_int (), take int type of data
3. Get the sandbox directory and create or open the database.
The ViewController.h header file adds a member variable and contains a header file Sqlite3.h
[CPP]View Plaincopy
- #import <UIKit/UIKit.h>
- #import <sqlite3.h>
- @interface Viewcontroller:uiviewcontroller
- {
- Sqlite3 *db;
- }
- @end
Define macros in. m files, and use them later
[CPP]View Plaincopy
- #define DBNAME @ "Personinfo.sqlite"
- #define NAME @ "Name"
- #define Age @ ' age '
- #define ADDRESS @ "Address"
- #define TABLENAME @ "PERSONINFO"
[CPP]View Plaincopy
- Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);
- NSString *documents = [Paths objectatindex:0];
- NSString *database_path = [documents Stringbyappendingpathcomponent:dbname];
- if (Sqlite3_open ([Database_path utf8string], &db)! = SQLITE_OK) {
- Sqlite3_close (DB);
- NSLog (@"database open failed");
- }
Sqlite3_open, if the data does not exist, it is created. Run. This is in the sandbox directory can see the database file (how to open the emulator sandbox directory please refer to: iOS learning iOS sandbox (sandbox) mechanism and file Operations (a))
4. Create a data table
Create a separate method for executing SQL statements, passing in SQL statements, executing SQL statements
[CPP]View Plaincopy
- -(void) Execsql: (NSString *) SQL
- {
- Char *err;
- if (sqlite3_exec (DB, [SQL Utf8string], NULL, NULL, &ERR)! = SQLITE_OK) {
- Sqlite3_close (DB);
- NSLog (@"database operation data failed!");
- }
- }
Create a statement personinfo a data table
[CPP]View Plaincopy
- NSString *sqlcreatetable = @"CREATE TABLE IF not EXISTS PERSONINFO (ID INTEGER PRIMARY KEY autoincrement, name TEXT, a GE INTEGER, address TEXT) ";
- [Self execsql:sqlcreatetable];
Run the program, and the data table is created. How do you know the data sheet was created? Let's open the database file with the SQLite Manager plugin tool in Firefox. You can install this plugin in Firefox. Open it
All four fields appear in the table.
5. Insert Data:
[CPP]View Plaincopy
- NSString *SQL1 = [NSString stringWithFormat:
- @"INSERT into '%@ ' ('%@ ', '%@ ', '%@ ') VALUES ('%@ ', '%@ ', '%@ ')",
- TABLENAME, NAME, age, ADDRESS, @ "Zhang San", @ "at", @"Xicheng"];
- NSString *SQL2 = [NSString stringWithFormat:
- @"INSERT into '%@ ' ('%@ ', '%@ ', '%@ ') VALUES ('%@ ', '%@ ', '%@ ')",
- TABLENAME, NAME, age, ADDRESS, @ "old six", @"a", @"Dongcheng District"];
- [Self EXECSQL:SQL1];
- [Self EXECSQL:SQL2];
Run the program, insert two data, and use Firefox's SQLite tool to view
6. Querying the database and printing the data
[CPP]View Plaincopy
- NSString *sqlquery = @"SELECT * from PERSONINFO";
- SQLITE3_STMT * statement;
- if (SQLITE3_PREPARE_V2 (DB, [SQLQuery utf8string],-1, &statement, nil) = = SQLITE_OK) {
- While (sqlite3_step (statement) = = Sqlite_row) {
- char *name = (char*) sqlite3_column_text (statement, 1);
- NSString *nsnamestr = [[NSString alloc]initwithutf8string:name];
- int age = Sqlite3_column_int (statement, 2);
- char *address = (char*) sqlite3_column_text (statement, 3);
- NSString *nsaddressstr = [[NSString alloc]initwithutf8string:address];
- NSLog (@"name:%@ age:%d address:%@", Nsnamestr,age, NSADDRESSSTR);
- }
- }
- Sqlite3_close (DB);
Printing results:
[CPP]View Plaincopy
- 2012-06-29 13:25:32.205 sqlitdemo[3587:f803] Name: Zhang San age:23 address: Xicheng District
- 2012-06-29 13:25:32.206 sqlitdemo[3587:f803] name: Old six age:20 address: Dongcheng District
Finally, close the database.
Example code: http://download.csdn.net/detail/totogo2010/4400911
iOS learning SQLite create DATABASE, table, insert view data