SQLite Common Statements

Source: Internet
Author: User
Tags sqlite stmt

Error store path

-(NSString *) datafilepath{

NSString *dbpath = [Nssearchpathfordirectoriesindomains (nslibrarydirectory, Nsuserdomainmask, YES) lastObject];

NSString *db_absolutestring = [DbPath stringbyappendingpathcomponent:@ "personal.db"]; Locate the PERSONAL.DB database

NSLog (@ "db_absolutestring:%@", db_absolutestring);

return db_absolutestring; //Returns the path to the database

}

Inserting data

-(NSString *) insertdbfilepath{

NSString *dbpath = [Nssearchpathfordirectoriesindomains (nslibrarydirectory, Nsuserdomainmask, YES) lastObject];

NSString *db_absolutestring = [DbPath stringbyappendingpathcomponent:@ "Cmread.sqlite"];

NSLog (@ "db_absolutestring:%@", db_absolutestring);

return db_absolutestring;

}

Get all your personal information

-(Nsmutablearray *) getstoredpersons{

Open Database

utfstring format

Data file path

if (Sqlite3_open ([[SelfDataFilePath] utf8string],&database)!=SQLITE_OK) {

Sqlite3_close (database); //Close Database

NSLog (@ "Open database failed!");

return nil;

}

Nsmutablearray *array = [[Nsmutablearray alloc] init];

NSString *query = [[NSString alloc] Initwithformat: @ "select zaccountname,zlastlogintime,zlogincount from Zloginrecord Order by Zlastlogintime desc"];

Sqlite3_stmt *statement;

if (sqlite3_prepare_v2 (database, [query utf8string],-1,&statement,nil) = = SQLITE_OK) {

while (sqlite3_step (statement) = = Sqlite_row) {

Cmloginrecord *loginrecord = [[Cmloginrecord alloc] init];

Char *loginrecordkey = (char *) sqlite3_column_text (statement, 0);

if (Loginrecordkey = = NULL) {

Loginrecord.accountname = nil;

}else {

NSString *_loginrecord = [[NSString alloc] initwithutf8string:loginrecordkey];

Loginrecord.accountname = _loginrecord;

}

Char *lastlogintimekey = (char *) sqlite3_column_text (statement, 1);

if (Lastlogintimekey = = NULL) {

Loginrecord.lastlogintime = nil;

}else {

NSString *_lastlogintime = [[NSString alloc] initwithutf8string:lastlogintimekey];

Loginrecord.lastlogintime = _lastlogintime;

}

int logincount = sqlite3_column_int(statement, 2);

Loginrecord.logincount = @ (Logincount);

[Array Addobject:loginrecord];

}

Sqlite3_finalize (statement);

}

Sqlite3_close (database);

return array;

}

Insert Personal Information

-(Nsinteger) insertpersonl:(Nsmutablearray *) array{

BOOL isinsertsuccess = NO;

If you can't open it, close it.

if (Sqlite3_open ([[Self Insertdbfilepath] utf8string],&database)!=SQLITE_OK) {

Sqlite3_close (database);

return 1;

}

Char *insert = "insert into Zaccount (zaccountname,zlastlogintime,zlogincount) values (?,?,?);"; /To remember the semicolon

For (Cmloginrecord *loginrecord in array) {

Sqlite3_stmt *stmt;

if (SQLITE3_PREPARE_V2 (database,insert,-1,&stmt,nil) = = SQLITE_OK) {

Sqlite3_bind_text (stmt, 1, [Loginrecord.accountname utf8string],-1,null);

Sqlite3_bind_text (stmt, 2, [Loginrecord.lastlogintime utf8string],-1,null);

Sqlite3_bind_int (stmt, 3, [Loginrecord.logincount intvalue]);

}

if (Sqlite3_step (stmt)! = Sqlite_done) {

NSLog (@ "error insert data into Zloginrecord Error!");

}else{

Delete the original database

isinsertsuccess = YES;

}

Sqlite3_finalize (stmt);

}

Sqlite3_close (database);

if (isinsertsuccess) {

NSString *path = [self datafilepath];

Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];

if ([FileManager Fileexistsatpath:path]) {

[FileManager Removeitematpath:path Error:nil];

}

NSLog (@ "Delete the original database");

}

return 0;

}

SQLite Common Statements

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.