SQLite常用語句

來源:互聯網
上載者:User

標籤:pat   search   資訊   led   路徑   lib   final   bind   string   

// 錯誤儲存路徑

- (NSString *)dataFilePath{

    NSString *dbPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];

    NSString *db_absoluteString = [dbPath stringByAppendingPathComponent:@"personal.db"]; // 找到personal.db資料庫

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

    return db_absoluteString; // 返回資料庫的路徑

}

 //插入資料

- (NSString *)insertDBFilePath{

    NSString *dbPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];

    NSString *db_absoluteString = [dbPath stringByAppendingPathComponent:@"cmread.sqlite"];

    // NSLog(@"db_absoluteString: %@",db_absoluteString);

        return db_absoluteString;

}

 

//獲得所有個人資訊

- (NSMutableArray *)getStoredPersons{

    // 開啟資料庫

    // UTFString格式

   //  資料檔案路徑

    if(sqlite3_open([[self dataFilePath] UTF8String],&database) !=SQLITE_OK){

        sqlite3_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;

}

// 插入個人資訊

- (NSInteger)insertPersonl:(NSMutableArray *)array{

    BOOL isInsertSuccess = NO;

   // 打不開的話,就關閉

    if(sqlite3_open([[self insertDBFilePath] UTF8String],&database) !=SQLITE_OK){

        sqlite3_close(database);

        return 1;

    }

    char *insert = "insert into zAccount(zaccountname,zlastlogintime,zlogincount) values(?,?,?);";// 要記住分號

 

    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{

            //刪除原來的資料庫

            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(@"刪除原來的資料庫");

    }

    

    return 0;

}

SQLite常用語句

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.