zoukankan      html  css  js  c++  java
  • IOS学习笔记-- SQLite的应用

      1 //
      2 //  HMViewController.m
      3 //  02-SQLite的应用
      4 //
      5 //  Created by apple on 14-7-24.
      6 //  Copyright (c) 2014年 heima. All rights reserved.
      7 //
      8 
      9 #import "HMViewController.h"
     10 #import <sqlite3.h>
     11 
     12 @interface HMViewController ()
     13 - (IBAction)insert;
     14 - (IBAction)update;
     15 - (IBAction)delete;
     16 - (IBAction)select;
     17 // db就是数据库的象征,如果要进行CRUD,得操作db这个实例
     18 @property (nonatomic, assign) sqlite3 *db;
     19 @end
     20 
     21 @implementation HMViewController
     22 
     23 - (void)viewDidLoad
     24 {
     25     [super viewDidLoad];
     26     
     27     // 获得数据库文件的路径
     28     NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
     29     NSString *filename = [doc stringByAppendingPathComponent:@"students.sqlite"];
     30     // 将OC字符串 转成 C语言字符串
     31     const char *cfilename = filename.UTF8String;
     32     // 1.打开数据库(如果数据库文件不存在,sqlite3_open函数会自动创建数据库文件)
     33     int result = sqlite3_open(cfilename, &_db);
     34     if (result == SQLITE_OK) { // 打开成功
     35         NSLog(@"成功打开数据库");
     36         
     37         // 2.创表
     38         const char *sql = "CREATE TABLE IF NOT EXISTS t_student (id integer PRIMARY KEY AUTOINCREMENT, name text NOT NULL, age integer NOT NULL);";
     39         char *erroMsg = NULL;
     40         result = sqlite3_exec(self.db, sql, NULL, NULL, &erroMsg);
     41         if (result == SQLITE_OK) {
     42             NSLog(@"成功创表");
     43         } else {
     44             //            printf("创表失败--%s--%s-%d", erroMsg, __FILE__, __LINE__);
     45             NSLog(@"创表失败--%s--%@-%d", erroMsg, [NSString stringWithUTF8String:__FILE__], __LINE__);
     46         }
     47     } else {
     48         NSLog(@"打开数据库失败");
     49     }
     50 }
     51 
     52 - (IBAction)insert {
     53     for (int i = 0; i<20; i++) {
     54         // 1.拼接SQL语句
     55         NSString *name = [NSString stringWithFormat:@"Jack-%d", arc4random_uniform(100)];
     56         int age = arc4random_uniform(20) + 30;
     57         NSString *sql = [NSString stringWithFormat:@"INSERT INTO t_student (name, age) VALUES ('%@', %d);", name, age];
     58         
     59         // 2.执行SQL语句
     60         char *erroMsg = NULL;
     61         sqlite3_exec(self.db, sql.UTF8String, NULL, NULL, &erroMsg);
     62         if (erroMsg) {
     63             NSLog(@"插入数据失败--%s", erroMsg);
     64         } else {
     65             NSLog(@"成功插入数据");
     66         }
     67     }
     68 }
     69 
     70 - (IBAction)update {
     71     //与insert只有sql语句不同
     72 }
     73 
     74 - (IBAction)delete {
     75     //与insert只有sql语句不同
     76 }
     77 
     78 - (IBAction)select {
     79     const char *sql = "SELECT id, name, age FROM t_student WHERE age <= 30;";
     80     // 进行查询前的准备工作
     81     // -1 代表系统会自动计算SQL语句的长度
     82     // sqlite3_stmt:用来取数据
     83     sqlite3_stmt *stmt = NULL;
     84     if (sqlite3_prepare_v2(self.db, sql, -1, &stmt, NULL) == SQLITE_OK) { // SQL语句没有问题
     85         NSLog(@"查询语句没有问题");
     86         
     87         // 每调一次sqlite3_step函数,stmt就会指向下一条记录
     88         while (sqlite3_step(stmt) == SQLITE_ROW) { // 找到一条记录
     89             // 取出数据
     90             
     91             // 取出第0列字段的值(int类型的值)
     92             int ID = sqlite3_column_int(stmt, 0);
     93             
     94             // 取出第1列字段的值(tex类型的值)
     95             const unsigned char *name = sqlite3_column_text(stmt, 1);
     96             
     97             // 取出第2列字段的值(int类型的值)
     98             int age = sqlite3_column_int(stmt, 2);
     99             
    100             NSLog(@"%d %s %d", ID, name, age);
    101         }
    102     } else {
    103         NSLog(@"查询语句有问题");
    104     }
    105 }
    106 @end
  • 相关阅读:
    使用Python对MySQL数据库操作
    使用Python对SQLite数据库操作
    使用Python对Access读写操作
    使用Python对Excel进行读写操作
    C# Serialization performance in System.Runtime.Serialization.Formatters.Binary.BinaryFormatter,Newtonsoft.Json.JsonConvert and System.Text.Json.JsonSerializer.Serialize
    C# calculate disk size
    C# rename files
    C# copy files from source directory to destination file and rename repeated files and does not override
    C# event
    C# redis StackExchange
  • 原文地址:https://www.cnblogs.com/wentianblog/p/3866592.html
Copyright © 2011-2022 走看看