SQLite3 Common 4 data types: text (text string), Integer (integer value), real (floating point value), BLOB (binary data (such as file)).
To use SQLite3 in iOS, first add the library file ‘libsqlite3.dylib‘
and import the primary header file#import<sqlite3.h>
Features of the SQL statement:1> is not case sensitive;
2> Each statement must be separated by a semicolon ; End
Commonly used in SQLKey Words:Pselect, INSERT, UPDATE, Delete, from, create, where, desc, order, by, Group, table, Alter, view, index, and so on
You cannot use keywords to name tables, fields in a database.
Types of SQL statements:1> data definition statement (ddl:data definition Language)
Including create and drop operations;
Creates a new table or deletes a table in the database (CREATE table or drop table).
2> Data manipulation statements (Dml:data manipulation Language)
Including INSERT, UPDATE, delete and other operations;
The 3 actions above are used to add, modify, and delete data from a table.
3> data query Statement (dql:data query Language)
can be used to query to obtain the data in the table;
The keyword Select is the most used operation for DQL (and all SQL);
Other dql commonly used keywords are where,order by,group by and having to create.
Create a table:create table if not exists t_student (id integer, name text, age inetger, score real) ;
Delete table:drop table if exists t_student;
Inserting data (insert):insert into t_student (name, age) values (‘JN‘, 22) ;
Note: The string contents in the database should be enclosed in single quotes.
Update data (Updata):pupdate t_student set name = ‘jack‘, age = 20 ;
Note: The above update will change the name of all records in the T_student table to Jack,age to 20;
Delete the data (delete):delete from t_student;
All records in the T_student table will be deleted.
If you want to update or delete only some of the fixed records, you must add some conditions to the DML statement. The example is as follows:
// 将t_student表中年龄大于10 并且 姓名不等于jack的记录,年龄都改为 5 update t_student set age = 5 where age > 10 and name != ‘jack’ ; // 删除t_student表中年龄小于等于10 或者 年龄大于30的记录 delete from t_student where age <= 10 or age > 30 ;
Query Statement (DQL):SELECT * from T_student where age > 10; Conditional query Condition statement:
PRIMARY KEY constraint:Each table must have a primary key to identify the uniqueness of the record.
What is the primary key:
The primary key (Primary key, referred to as PK), is used to uniquely identify a record.
For example, t_student can add an ID field as the primary key, which is equivalent to a person's ID.
The primary key can be a field or multiple fields.
FOREIGN KEY constraints:FOREIGN KEY constraints allow you to establish a connection between tables.
The general case of a foreign key is that a field in one table refers to the primary key field of another table.
Open, close the databaseTo create or open a database:
// path为:~/Documents/person.dbsqlite3 *db;int result = sqlite3_open([path UTF8String], &db);
Code parsing:
Sqlite3_open () will open the database based on the file path, and if it does not, a new database will be created. If result equals constant SQLITE_OK, the database is opened successfully.
SQLite *db: An open db instance.
The path to the database file must be passed in the C string, not nsstring.
Close database: sqlite3_close (db);
Executes an SQL statement that does not return a statement
char *errorMsg; // 用来存储错误信息char *sql = "create table if not exists t_person(id integer primary key autoincrement, name text, age integer);";int result = sqlite3_exec(db, sql, NULL, NULL, &errorMsg);
Code parsing:
Sqlite3_exec () can execute any SQL statements, such as Create, update, insert, and delete operations. However, it is generally not necessary to execute a query statement because it does not return the query to the data.
Sqlite3_exec () can also execute the statement:
1> Open transaction: begain transaction;
2> ROLLBACK TRANSACTION: Rollback
3> COMMIT TRANSACTION: Commit
SQLite Function Summary:1. Open the DatabaseIntSqlite3_open(ConstChar *filename,The file path of the database Sqlite3 **ppdbdatabase instance);2. Execute any SQL statementIntSqlite3_exec(sqlite3*,An open DB instanceConstChar *sql,SQL statements that need to be executedInt (*callback)(void*,Intchar**,char**),Callback after the SQL statement has completed executionvoid *,The 1th parameter of a callback functionChar **errmsgError message);3. Check the legality of the SQL statement (pre-query preparation)IntSqlite3_prepare_v2(Sqlite3 *db,DB instanceConstChar *zsql,SQL statements that need to be checkedint Nbyte,The maximum byte length of the SQL statement is sqlite3_stmt **ppstmt,SQLITE3_STMT instances to obtain database dataConstChar **pztail);4. Querying a row of dataIntSqlite3_step(sqlite3_stmt*);If a row of data is queried, the Sqlite_row is returned5. Use stmt to get the value of a field (the subscript of the field from0 start)DoubleSqlite3_column_double(sqlite3_stmt*,int icol);Floating point dataIntSqlite3_column_int(sqlite3_stmt*,int icol); //Integer data sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int icol); //long-integer data const void *Sqlite3_column_blob(sqlite3_stmt*, int icol); //Binary text data const unsigned char *sqlite3_column_text(sqlite3_stmt*, int icol); //String data