【iOS開發-104】SQLite使用:注意查詢時分步寫while會出現死迴圈

來源:互聯網
上載者:User

標籤:

注意點:

在SELECT查詢時,不能分步寫成如下形式,否則會出現死迴圈:

int stepResult=sqlite3_step(statement);while (stepResult==SQLITE_ROW) {}
只能合并成一句寫成:
while (sqlite3_step(statement)==SQLITE_ROW) {         }

因為,sqlite3_step的執行方式是如下:

** ^If the SQL statement being executed returns any data, then [SQLITE_ROW]

** is returned each time a new row of data is ready for processing by the

** caller. The values may be accessed using the [column access functions].

** sqlite3_step() is called again to retrieve the next row of data.


#import "ViewController.h"#import <sqlite3.h>@interface ViewController ()- (IBAction)insert:(id)sender;- (IBAction)delete:(id)sender;- (IBAction)update:(id)sender;- (IBAction)select:(id)sender;@end@implementation ViewControllerstatic sqlite3 *_db;- (void)viewDidLoad {    [self setupDatabaseAndTable];    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}-(void)setupDatabaseAndTable{    NSString *filename=[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"iOS.sql"];    NSLog(@"%@",filename);        int openDBResult=sqlite3_open(filename.UTF8String, &_db);        if (openDBResult==SQLITE_OK) {        //建立表        NSString *[email protected]"CREATE TABLE IF NOT EXISTS user (id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT,age INTEGER);";        char *error=nil;        int createTableResult=sqlite3_exec(_db, createTableSql.UTF8String, NULL, 0, &error);        if (createTableResult==SQLITE_OK) {            NSLog(@"建立表成功");        }else{            NSLog(@"建立表失敗");        }        NSLog(@"開啟資料庫成功");    }else{        NSLog(@"開啟資料庫失敗");    }}- (IBAction)insert:(id)sender {    for (int i=0; i<50; i++) {        NSString *name=[NSString stringWithFormat:@"jack%d",arc4random()%100];        int age=arc4random()%100;        NSString *insertTableSql=[NSString stringWithFormat:@"INSERT INTO user(name,age) VALUES('%@',%d);",name,age];        char *error=nil;        int insertResult=sqlite3_exec(_db, insertTableSql.UTF8String, NULL, 0, &error);        if (insertResult==SQLITE_OK) {            NSLog(@"%d %@ %d",i,name,age);        }else{            NSLog(@"插入資料失敗");        }    }}- (IBAction)delete:(id)sender {    NSString *[email protected]"DELETE FROM user WHERE id=1;";    char *error=nil;    int deleteResult=sqlite3_exec(_db, deleteRowSql.UTF8String, NULL, 0, &error);    if (deleteResult==SQLITE_OK) {        NSLog(@"刪除資料成功");    }else{        NSLog(@"刪除資料失敗");    }}- (IBAction)update:(id)sender {    NSString *[email protected]"UPDATE user SET age=130 WHERE id=3;";    char *error=nil;    int updateResult=sqlite3_exec(_db, updateRowSql.UTF8String, NULL, 0, &error);    if (updateResult==SQLITE_OK) {        NSLog(@"修改資料庫成功");    }else{        NSLog(@"修改資料庫失敗");    }}- (IBAction)select:(id)sender {    NSString *[email protected]"SELECT id,name,age FROM user WHERE id=5;";    sqlite3_stmt *statement;    int stmtResult=sqlite3_prepare_v2(_db, selectRowSql.UTF8String, -1, &statement, nil);    if (stmtResult==SQLITE_OK) {        NSLog(@"stmtResult成功");        //以下語句分步寫會出現死迴圈        while (sqlite3_step(statement)==SQLITE_ROW) {            NSLog(@"stepResult成功");            int uid=sqlite3_column_int(statement, 0);            const unsigned char *uname=sqlite3_column_text(statement, 1);            int uage=sqlite3_column_int(statement, 2);            NSLog(@"%d %s %d",uid,uname,uage);        }    }else{        NSLog(@"stmtResult失敗");    }}@end


【iOS開發-104】SQLite使用:注意查詢時分步寫while會出現死迴圈

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.