Create a database statement
-(void) Creatdata
{
Sqlite3 *sqlite = nil;
NSString *filepath = [Nshomedirectory () stringbyappendingpathcomponent:@ "Documents/data.file"];
Open Database
int result = Sqlite3_open ([FilePath utf8string], &sqlite);
if (Result!=sqlite_ok) {
NSLog (@ "Create failed!!! ");
return;
}
To create a table's SQL statement
NSString *sql = @ "CREATE TABLE IF not EXISTS usertable (userName text PRIMARY KEY, password text,email text)";
Execute SQL statement
Char *error;
result = Sqlite3_exec (SQLite, [SQL Utf8string], NULL, NULL, &ERROR);
if (Result! = SQLITE_OK) {
NSLog (@ "Failed to create database:%s", error);
return;
}
Plug in a piece of data
INSERT OR REPLACE into usertable (username,password,email) VALUES (?,?,?);
Update One piece of data
UPDATE usertable Set password = ' where userName = ';
Querying data
SELECT UserName, password,eamil from usertable where userName = ';
Delete data
DELETE from usertable WHERE username = ';
Close the database
Sqlite3_close (SQLite);
}
**************************
-(void) EditData
{
Sqlite3 *sqlite = nil;
Handle statement
Sqlite3_stmt *stmt =nil;
Database
NSString *filepath = [Nshomedirectory () stringbyappendingpathcomponent:@ "Documents/data.file"];
int result = Sqlite3_open ([FilePath utf8string], &sqlite);
if (Result!=sqlite_ok) {
NSLog (@ "Open database failed!!! ");
return;
}
Creating SQL statements
NSString *sql = @ "INSERT into usertable (username,password,email) VALUES (?,?,?)";
Compiling SQL statements
SQLITE3_PREPARE_V2 (SQLite, [SQL Utf8string],-1, &stmt, NULL);
NSString *username = @ "Zhang San";
NSString *password = @ "123456";
NSString *email = @ "MXYD.QQ";
Binding Populate SQL statements
Sqlite3_bind_text (stmt, 1, [UserName utf8string],-1, NULL);
Sqlite3_bind_text (stmt, 2, [Password utf8string],-1, NULL);
Sqlite3_bind_text (stmt, 3, [email utf8string],-1, NULL);
SQL Edit Statement
Execute SQL statement
result = Sqlite3_step (stmt);
if (result = = Sqlite_error | | result = = sqlite_misuse) {
NSLog (@ "Compiling database Error!!!" ");
Return
}
Close Handle Statement
Sqlite3_finalize (stmt);
Close the database
Sqlite3_close (SQLite);
NSLog (@ "Data inserted successfully!!! ");
}
Creation and editing of SQLite