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