<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                數據庫增刪改查的基本sql語法略........ ?直接進入SQLite3的使用。 SQLite3常用函數: 1、打開數據庫連接 ~~~ int sqlite3_open( const char *filename,??/* 數據庫文件名(UTF-8)*/ sqlite3 **ppDb /* OUT: SQLite 數據庫句柄 */ ); ~~~ 2、關閉數據庫 ~~~ int sqlite3_close(sqlite3*); //參數數據庫句柄。 ~~~ 3、sqlite3_exec()可以執行任何SQL語句,比如創表、更新、插入和刪除操作。但是一般不用它執行查詢語句,因為它不會返回查詢到的數據 ~~~ int sqlite3_exec( sqlite3*, /* 已經打開的數據庫句柄 */ const char *sql, ?/* 要執行的Sql語句 ?可以包括事務("BEGIN TRANSACTION")、回滾("ROLLBACK")和提交("COMMIT")*/ sqlite_callback, ?/* 回調函數*/ void *, ?/*傳遞給回調函數的參數*/ char **errmsg /* 保存錯誤信息*/ ); ~~~ 4、sql語句準備函數 ~~~ int sqlite3_prepare_v2( ? sqlite3 *db, ? ? ? ? ? ?/* 已經打開的數據庫句柄 */ ? const char *zSql, ? ? ? /* 要執行的Sql語句 ?, UTF-8 encoded */ ? int nByte, ? ? ? ? ? ? ?/* 如果nByte小于0,則函數取出zSql中從開始到第一個0終止符的內容;如果nByte不是負的,那么它就是這個函數能從zSql中讀取的字節數的最大值。如果nBytes非負,zSql在第一次遇見’/000/或’u000’的時候終止 */ ? sqlite3_stmt **ppStmt, ?/*上面提到zSql在遇見終止符或者是達到設定的nByte之后結束,假如zSql還有剩余的內容,那么這些剩余的內容被存放到pZTail中,不包括終止符*/ ? const char **pzTail ? ? /* 能夠使用sqlite3_step()執行的編譯好的準備語句的指針,如果錯誤發生,它被置為NULL,如假如輸入的文本不包括sql語句。調用過程必須負責在編譯好的sql語句完成使用后使用sqlite3_finalize()刪除它 */ ); ~~~ 5、 ~~~ sqlite3_bind_text( ? sqlite3_stmt*, ?/* 已經打開的數據庫句柄 */ ?int, ???/* 占位符的位置,第一個占位符的位置是1,不是0 */ ?const char*, ??/* 占位符要綁定的值 */ ?int, ??/* 指在第3個參數中所傳遞數據的長度,對于C字符串,可以傳遞-1代替字符串的長 */ ?void(*)(void*) ?/* 可選的函數回調,一般用于在語句執行后完成內存清理工作*/ ?); ~~~ 6、sqlite_step():執行SQL語句,返回SQLITE_DONE代表成功執行完畢 7、sqlite3_free(char **errmsg)功能:釋放存放錯誤信息的內存空間 8、sqlite_finalize():銷毀sqlite3_stmt *對象 9、 ~~~ int sqlite3_get_table執行Sql查詢,執行一次查詢Sql 并且返回得到一個記錄集。 int sqlite3_get_table( ? sqlite3*, ? ? ? ? ? ? ?/* 已經打開的數據庫句柄 */ ? const char *sql, ? ? ? /* 要執行的Sql語句*/ ? char ***resultp, ? ? ? /* 保存返回記錄集的指針,包含列名及烈屬性值。行優先,從0起計數下標*/ ? int *nrow, ? ? ? ? ? ? /*返回記錄行數數*/ ? int *ncolumn, ? ? ? ? ?/* 返回記錄列數*/ ? char **errmsg ? ? ? ? ?/* 返回錯誤信息*/ ? ) ~~~ 10、void sqlite3_free_table(char **result);?釋放當前查詢的記錄集所占用的內存 占位插入例子: ~~~ char *sql = "insert into t_person(name, age) values(?, ?);"; sqlite3_stmt *stmt; if (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) == SQLITE_OK) { sqlite3_bind_text(stmt, 1, "小明", -1, NULL); sqlite3_bind_int(stmt, 2, 27); } if (sqlite3_step(stmt) != SQLITE_DONE) { NSLog(@"插入數據錯誤"); } sqlite3_finalize(stmt); ~~~ 查詢例子: ~~~ const char *sql = "select name,price from t_shop"; sqlite3_stmt *stmt = NULL; int status = sqlite3_prepare_v2(self.db, sql , -1, &stmt, NULL); if (status == SQLITE_OK) { // 準備成功 -- SQL語句正確 while (sqlite3_step(stmt) == SQLITE_ROW) {// 成功取出一條數據 const char *name = (const char *)sqlite3_column_text(stmt, 0); const char *price = (const char *)sqlite3_column_text(stmt, 1); NSString name = [NSString stringWithUTF8String:name]; NSString price = [NSString stringWithUTF8String:price]; } } ~~~ 2.實戰案例:輸入商品、價格,添加插入一條記錄顯示在table中,也可以查詢。 ![](https://box.kancloud.cn/2016-03-07_56dd400d20872.jpg) 1.項目引入libsqlite3 ![](https://box.kancloud.cn/2016-03-07_56dd400dd91bc.jpg) 2.代碼: ~~~ // // ViewController.m // sqlite // // #import "ViewController.h" #import <sqlite3.h> #import "ZXHShop.h" @interface ViewController ()<UITableViewDataSource,UISearchBarDelegate> @property (weak, nonatomic) IBOutlet UITextField *nameField; @property (weak, nonatomic) IBOutlet UITextField *priceFIeld; @property (assign,nonatomic) sqlite3 *db; @property (weak, nonatomic) IBOutlet UITableView *tableVIew; @property (strong,nonatomic) NSMutableArray *shops; - (IBAction)insertShop; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //初始化數據庫 [self setupDB]; [self setupData]; // 增加搜索框 UISearchBar *searchBar = [[UISearchBar alloc] init]; searchBar.frame = CGRectMake(0, 0, 320, 44); searchBar.delegate = self; self.tableVIew.tableHeaderView = searchBar; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(NSMutableArray *)shops{ if (!_shops) { _shops = [[NSMutableArray alloc] init]; } return _shops; } //初始化數據庫 -(void)setupDB{ //沙盒路徑 NSString *filename = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject] stringByAppendingPathComponent:@"shops.sqlite"]; NSLog(@"%@",filename); // 如果數據庫文件不存在, 系統會自動創建文件自動初始化數據 // sqlite3_open([path UTF8String], &db); 參入:一:存放地址 二:數據庫實例 int status = sqlite3_open(filename.UTF8String, &_db); //返回SQLITE_OK代碼打開成功 if (status == SQLITE_OK) {//打開成功 //創建表 const char *sql= "create table if not exists t_shop(id integer primary key,name text not null,price real)"; char *errmsg = NULL; sqlite3_exec(self.db, sql, NULL, NULL, &errmsg); if (errmsg) { NSLog(@"創建表失敗!%s",errmsg); } }else{ NSLog(@"打開數據庫失敗!"); } } //新增商品 - (IBAction)insertShop { NSString *sql = [NSString stringWithFormat:@"insert into t_shop(name,price) values('%@',%f)",self.nameField.text,self.priceFIeld.text.doubleValue]; char *errmsg = NULL; sqlite3_exec(self.db, sql.UTF8String, NULL, NULL, &errmsg); if (errmsg) { NSLog(@"添加商品失敗!"); }else{ [self alert]; ZXHShop *shop = [[ZXHShop alloc] init]; shop.name = self.nameField.text; shop.price = self.priceFIeld.text; [self.shops addObject:shop]; self.nameField.text = @""; self.priceFIeld.text = @""; [self.tableVIew reloadData]; } } /** 查詢數據 */ -(void)setupData{ const char *sql = "select name,price from t_shop"; sqlite3_stmt *stmt = NULL; int status = sqlite3_prepare_v2(self.db, sql , -1, &stmt, NULL); if (status == SQLITE_OK) { // 準備成功 -- SQL語句正確 while (sqlite3_step(stmt) == SQLITE_ROW) {// 成功取出一條數據 const char *name = (const char *)sqlite3_column_text(stmt, 0); const char *price = (const char *)sqlite3_column_text(stmt, 1); ZXHShop *shop = [[ZXHShop alloc] init]; shop.name = [NSString stringWithUTF8String:name]; shop.price = [NSString stringWithUTF8String:price]; [self.shops addObject:shop]; } } } -(void)alert{ UIAlertController *alert = [UIAlertController alertControllerWithTitle:NULL message:@"新增成功" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *close = [UIAlertAction actionWithTitle:@"關閉" style:UIAlertActionStyleCancel handler:nil]; [alert addAction:close]; [self presentViewController:alert animated:YES completion:nil]; } #pragma mark table數據源方法 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.shops.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *ID = @"shop"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (!cell) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID]; cell.backgroundColor = [UIColor grayColor]; } ZXHShop *shop = self.shops[indexPath.row]; cell.textLabel.text = shop.name; cell.detailTextLabel.text = shop.price; cell.detailTextLabel.textColor = [UIColor redColor]; return cell; } #pragma mark searchBarDelegate -(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{ [self.shops removeAllObjects]; NSString *sql = [NSString stringWithFormat:@"select name,price from t_shop where name like '%%%@%%' or price like '%%%@%%'",searchText,searchText]; sqlite3_stmt *stmt = NULL; int status = sqlite3_prepare_v2(self.db, sql.UTF8String, -1, &stmt, NULL); if (status == SQLITE_OK) { while (sqlite3_step(stmt) == SQLITE_ROW) { const char *name = (const char *)sqlite3_column_text(stmt, 0); const char *price = (const char *)sqlite3_column_text(stmt, 1); ZXHShop *shop = [[ZXHShop alloc]init]; shop.name = [NSString stringWithUTF8String:name]; shop.price = [NSString stringWithUTF8String:price]; [self.shops addObject:shop]; } } [self.tableVIew reloadData]; } @end ~~~ --------文章至此!
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看