Simple usage of Sqlite 1. First add the libsqlite3.dylib library. When you add a library, you will find a libsqlite3.dylib and a libsqlite3.0.dylib. In this way, you will doubt the two differences. Here we will introduce: in fact, libsqlite3.dylib is a link, which points to libsqlite3.0.dylib. Libsql
Simple usage of Sqlite 1. First add the libsqlite3.dylib library. When you add a library, you will find a libsqlite3.dylib and a libsqlite3.0.dylib. In this way, you will doubt the two differences. Here we will introduce: in fact, libsqlite3.dylib is a link, which points to libsqlite3.0.dylib. Libsql
Simple usage of Sqlite
1. Add the libsqlite3.dylib Library first. When you add a library, you will find a libsqlite3.dylib and a libsqlite3.0.dylib. In this way, you will doubt the two differences. Here we will introduce: in fact, libsqlite3.dylib is a link, which points to libsqlite3.0.dylib. Libsqlite3.dylib always points to the latest sqlite dynamic library.
2. Introduce the framework in view (two methods ).
(1) # import (2) # import "/usr/include/sqlite3.h"
Sqlite3 * database;
3. The sqlite database is a file database and is stored in the file system. Create a database under Documents ).
NSArray * documentsPath = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
NSString * databaseFilePath = [[documentsPathobjectAtIndex: 0] stringByAppendingPathComponent: @ "mydb"];
4. Open the database
If (sqlite3_open ([databaseFilePathUTF8String], & database) = SQLITE_ OK ){
NSLog (@ "open sqlite db OK .");
}
5. Table creation statements
Char * errorMsg;
Constchar * createSql = "create table if not exists persons (id integer primary key autoincrement, name text )";
If (sqlite3_exec (database, createSql, NULL, NULL, & errorMsg) = SQLITE_ OK ){
NSLog (@ "create OK ");
}
6. insert records into the table
Const char * insertSql = "insert into persons (name) values ('hangsan ')";
If (sqlite3_exec (database, insertSql, NULL, NULL, & errorMsg) = SQLITE_ OK ){
NSLog (@ "insert OK .");
} Else {
NSLog (@ "error: % s", errorMsg );
Sqlite3_free (errorMsg );
}
7. query results
Const char * selectSql = "select id, name from persons ";
Sqlite3_stmt * statement;
If (sqlite3_prepare_v2 (database, selectSql,-1, & statement, nil) = SQLITE_ OK ){
NSLog (@ "select OK ");
While (sqlite3_step (statement) = SQLITE_ROW ){
Int _ id = sqlite3_column_int (statement, 0 );
Char * name = (char *) sqlite3_column_text (statement, 1 );
NSString * nsAddressStr = [[NSStringalloc] initwithuf8string: name];
NSLog (@ "id: % I name: % @", _ id, nsAddressStr );
}
}
8. Shut down the database
Sqlite3_close (database );
Sqlite3 * database; database handle
Sqlite3_stmt is used to save compiled SQL statements.
Sqlite3_open () Open the database and create the database when there is no
Sqlite3_exec () executes non-query SQL statements
Sqlite3_prepare () Prepare the SQL statement and execute the select statement
After sqlite3_step () calls sqlite3_prepare, use this function to move in the record set
Sqlite3_finalize () releases SQL Resources
Sqlite3_close () closes the database
Sqlite3_bind_int (stmt, 1,1); // bind the first int Parameter
Sqlite3_bind_text (stmt, 2 ,[? UTF8String],-1, SQLITE_TRANSIENT); // bind the second string parameter
Retrieve fields in a record set
Sqlite3_column_int (): int type data
Sqlite3_column_text () to retrieve text data
Sqlite3_column_blob () Fetch blob Data
# Define SQLITE_ OK 0/* Successful result */
/* Beginning-of-error-codes */
# Define SQLITE_ERROR 1/* SQL error or missing database */
# Define SQLITE_INTERNAL 2/* Internal logic error in SQLite */
# Define SQLITE_PERM 3/* Access permission denied */
# Define SQLITE_ABORT 4/* Callback routine requested an abort */
# Define SQLITE_BUSY 5/* The database file is locked */
# Define SQLITE_LOCKED 6/* A table in the database is locked */
# Define SQLITE_NOMEM 7/* A malloc () failed */
# Define SQLITE_READONLY 8/* Attempt to write a readonly database */
# Define SQLITE_INTERRUPT 9/* Operation terminated by sqlite3_interrupt ()*/
# Define SQLITE_IOERR 10/* Some kind of disk I/O error occurred */
# Define sqlite_0000upt 11/* The database disk image is malformed */
# Define SQLITE_NOTFOUND 12/* not used. Table or record not found */
# Define SQLITE_FULL 13/* Insertion failed because database is full */
# Define SQLITE_CANTOPEN 14/* Unable to open the database file */
# Define SQLITE_PROTOCOL 15/* not used. Database lock protocol error */
# Define SQLITE_EMPTY 16/* Database is empty */
# Define SQLITE_SCHEMA 17/* The database schema changed */
# Define SQLITE_TOOBIG 18/* String or BLOB exceeds size limit */
# Define SQLITE_CONSTRAINT 19/* Abort due to constraint violation */
# Define SQLITE_MISMATCH 20/* Data type mismatch */
# Define SQLITE_MISUSE 21/* Library used incorrectly */
# Define SQLITE_NOLFS 22/* Uses OS features not supported on host */
# Define SQLITE_AUTH 23/* Authorization denied */
# Define SQLITE_FORMAT 24/* Auxiliary database format error */
# Define SQLITE_RANGE 25/* 2nd parameter to sqlite3_bind out of range */
# Define SQLITE_NOTADB 26/* File opened that is not a database file */
# Define SQLITE_ROW 100/* sqlite3_step () has another row ready */
# Define SQLITE_DONE 101/* sqlite3_step () has finished executing */
When the sqlite3_prepare_v2 () function is called, it is a structure (sqlite3_stmt) that compiles the SQL statement into sqlite ).
This struct contains information about the SQL statements to be executed.
Normally, 0 is returned. In other cases, other values are returned.
I will take 1 as an example to briefly introduce how to solve the error:
# Define SQLITE_ERROR 1/* SQL error or missing database */