SQLite additions and deletions to search

Source: Internet
Author: User
Tags sqlite stmt

SQLite is an C open source library written in a language that implements a self-contained SQL关系型数据库引擎 , SQLite large amount of data that can be manipulated using storage, and as a relational database we can create multiple tables in a database to solve a large number of data duplication problems. And the SQLite library is optimized for use on mobile devices.


Because SQLiteThe interface uses CWritten, and Objective-CIs CSo that you can use it directly in your project SQLite
Wrote a little demo,git:https://github.com/yangchengzh/pachagingsqlite.
The following is the key part of the code static Sqlite3 *db;
-(Sqlite3 *) opendb
{
Indicates that the database has been opened
if (db!=nil) {
return DB;
}
Nsstring*doc = [Nssearchpathfordirectoriesindomains (Nsdocumentdirectory,nsuserdomainmask, YES) lastObject];
Nsstring*filename = [docstringbyappendingpathcomponent:@ "Perple.sqlite"];

Converts an OC string to a C-language string
Const CHAR*CFILENAME = filename.utf8string;
Open the database file (the function will automatically create the database file if the database file does not exist)
int result =sqlite3_open (cFileName, &db);
if (result== sqlite_ok) {
NSLog (@ "database is open");
} else{
NSLog (@ "Database open failed");
}
RETURNDB;
}

-(void) closedb
{
int result =sqlite3_close (db);
if (result== sqlite_ok) {
NSLog (@ "Database closed");
db = nil;
} else{
NSLog (@ "Database shutdown failed");
}
}

-(void) creattable
{
db = [SELFOPENDB];
Nsstring*sql = @ "CREATE table IF not EXISTS perpletable (number integerprimary key isn't null, name text NOT NULL, gender Tex T not NULL, Ageinteger not null) ";

int result =sqlite3_exec (db, SQL. Utf8string, NULL, NULL, NULL);
if (result== sqlite_ok) {
NSLog (@ "CREATE table succeeded");
} else{
NSLog (@ "Failed to create TABLE");
}
[Selfclosedb];
}

-(void) Insertwithmodel: (Personmodel *) model
{
db = [SELFOPENDB];
Nsstring*sql = [NSString stringwithformat:@ "INSERT into perpletable (number,name, gender, age) VALUES ('%ld ', '%@ ', '%@ ', ' %ld ') ", Model.number,model.name, Model.gender, Model.age];
int result =sqlite3_exec (db, SQL. Utf8string, NULL, NULL, NULL);
if (result== sqlite_ok) {
NSLog (@ "add success");
} else{
NSLog (@ "Add failed");
}
[Selfclosedb];
}

-(void) Delatewithage: (Nsinteger) Age
{
db = [SELFOPENDB];
Nsstring*sql = [NSString stringwithformat:@ "Delete from perpletable whereage = '%ld '", age];
int result =sqlite3_exec (db, SQL. Utf8string, NULL, NULL, NULL);
if (result== sqlite_ok) {
NSLog (@ "Delete succeeded");
} else{
NSLog (@ "Delete table failed");
}
[Selfclosedb];
}

-(void) Delatewithname: (NSString *) name
{
db = [SELFOPENDB];
Nsstring*sql = [NSString stringwithformat:@ "Delete from perpletable wherename = '%@ '", name];
int result =sqlite3_exec (db, SQL. Utf8string, NULL, NULL, NULL);
if (result== sqlite_ok) {
NSLog (@ "Delete succeeded");
} else{
NSLog (@ "Delete table failed");
}
[Selfclosedb];
}

-(void) Updatewithname: (NSString *) name Byage: (Nsinteger) Age
{
db = [SELFOPENDB];
Nsstring*sql = [NSString stringwithformat:@ "Update perpletable set name = '%@ ' where age = '%ld '", name, age];
int result =sqlite3_exec (db, SQL. Utf8string, NULL, NULL, NULL);
if (result== sqlite_ok) {
NSLog (@ "Update data success");
} else{
NSLog (@ "Failed to update data");
}
[Selfclosedb];
}

-(void) Selertall
{
1. Open the Database
db = [SELFOPENDB];
2. Write SQL statements
Nsstring*sql = @ "SELECT * from perpletable";
3. Create a follow pointer
sqlite3_stmt*stmt = nil;
4. Execute the statement
int result =SQLITE3_PREPARE_V2 (db, SQL. Utf8string,-1, &stmt, NULL);
5. Determine if the statement is correct
if (result== sqlite_ok) {
NSLog (@ "Query success");
6. Execute the query
while (Sqlite3_step (stmt) = = Sqlite_row) {
7. Satisfy the condition read data
int number = Sqlite3_column_int (stmt, 0);
Const unsigned char *name = Sqlite3_column_text (stmt, 1);
Const unsigned char *gender = Sqlite3_column_text (stmt, 2);
int age = Sqlite3_column_int (stmt, 3);
NSLog (@ "name =%s, Number =%d, gender =%s, age =%d", Name,number, gender, age);
}

} else{
NSLog (@ "Query failed");
}
8. Release the pointer
Sqlite3_finalize (stmt);
9. Close the database
[Selfclosedb];
}

-(void) Selertwithage: (Nsinteger) Age
{
db = [SELFOPENDB];
Nsstring*sql = [NSString stringwithformat:@ "select * from perpletable whereage =%ld", age];
sqlite3_stmt*stmt = nil;
int result =SQLITE3_PREPARE_V2 (db, SQL. Utf8string,-1, &stmt, NULL);
if (result== sqlite_ok) {
NSLog (@ "Query success");
while (Sqlite3_step (stmt) = = Sqlite_row) {
Const unsigned char *name = Sqlite3_column_text (stmt, 1);
Const unsigned char *gender = Sqlite3_column_text (stmt, 2);
int number = Sqlite3_column_int (stmt, 0);
int age = Sqlite3_column_int (stmt, 3);
NSLog (@ "name =%s, Number =%d, gender =%s, age =%d", Name,number, gender, age);
}

} else{
NSLog (@ "Query failed");
}
Sqlite3_finalize (stmt);
[Selfclosedb];
}

SQLite additions and deletions to search

Related Article

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.