SQLite Basic Operation-----IOS (if there is a similarity, purely coincidental)

Source: Internet
Author: User

First, the common method

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

Second, create a table

Creating a table requires preprocessing and execution to create two procedures

When you perform the creation, choose multiple Sqlite3_step

Third, the relevant code

1. Open or create a database

sqlite3 *sqldatabase;

-(BOOL) Createoropen: (nsstring *) dbName

{

//Get user domain overlay information

Nsarray *paths = Nssearchpathfordirectoriesindomains(nsdocumentationdirectory, Nsuserdomainmask, YES);

nsstring *documentsdirectory = [paths objectatindex:0];

/**

*users/admin/library/application Support/iphone simulator/7.1/applications/8e23557e-aaa6-471a-aafe-e036bf1b7e4c/ Library/documentation

* determine if the user domain has a database dbnmae

*/

nsstring *path = [documentsdirectory stringbyappendingpathcomponent:dbname];

Nsfilemanager *filemanageer = [Nsfilemanager defaultmanager];

//If the database is in the user domain, returns Yes, otherwise returns NO

BOOL find = [Filemanageer fileexistsatpath:path];

if (find) //find is processed, if found, and open, then return Yes

{

//Open the database and return NO if open fails, otherwise return Yes

if(Sqlite3_open([path utf8string], &sqldatabase)! = Sqlite_ok)

{

Close the SQLDatabaseand actually release it

Sqlite3_close(SQLDatabase);

return NO;

}

return YES;

}

NSLog(@ "%d", Sqlite3_open([path utf8string], &sqldatabase));

//Create a database, create a return Yes, and open the database, otherwise return NO

if(Sqlite3_open([path utf8string], &sqldatabase) = =Sqlite_ok)

{

return YES;

}Else

{

Close the SQLDatabaseand actually release it

Sqlite3_close(SQLDatabase);

return NO;

}

return NO;

}

2. Inserting data

Inserting data into a table

-(void) Insertmbkey: (nsstring *) key

{

BOOL IsOK = NO;

sqlite3_stmt *statement;

static char *sql = "INSERT into user VALUES (' 1 ', ' Bill ', ' Henan ', ' ssss ', ' 2 ')";

int success = Sqlite3_prepare_v2 (sqldatabase, SQL,-1, &statement, NULL);

if (Success! =Sqlite_ok)

{

IsOK = NO;

}Else

{

Sqlite3_bind_text (statement, 1, [Key utf8string],-1, sqlite_transient);

Success = Sqlite3_step (statement);

sqlite3_finalize (statement);

}

if (success = =Sqlite_error)

{

IsOK = NO;

}Else

{

isok=YES;

}

return;

}

3. Query data

Querying data

-(void) GetList: (nsmutablearray *) keyslist

{

BOOL IsOK = NO;

sqlite3_stmt *statement;

static char *sql = "Select id,address from User";

int success = Sqlite3_prepare_v2 (sqldatabase, SQL,-1, &statement, NULL);

if (Success! =Sqlite_ok)

{

IsOK = NO;

}Else

{

//Query result set to traverse all records one by one, where the number corresponds to the time column value

While (sqlite3_step (statement) = =Sqlite_row) {

int kid = Sqlite3_column_int (statement, 0);

char *key = (char *)sqlite3_column_text (statement, 1);

UserInfo *usermodel = [[UserInfo alloc] init];

Usermodel. userId =kid;

if (key) {

Usermodel.useraddress = [NSString stringwithutf8string: key];

}

[Keyslist Addobject:usermodel];

sqlite3_finalize (statement);

}

NSLog (@ "%@", keyslist);

if (success==sqlite_error)

{

IsOK = NO;

}Else

{

IsOK = YES;

}

return;

}

}

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.