sqlite3 的简单使用

sqlite3 的简单使用_第1张图片
zbc.png

//
//  ViewController.m
//  sql
//
//  Created by 柏超曾 on 2017/8/31.
//  Copyright © 2017年 柏超曾. All rights reserved.
//

#import "ViewController.h"
#import 
#import "SQLiteManager.h"
@interface ViewController (){
    
    SQLiteManager *dbManager;
    
    sqlite3 *database;
    
}

//@property(strong ,nonatomic)  sqlite3 *database;
@property (weak, nonatomic) IBOutlet UITextView *testView;


@end

@implementation ViewController

- (IBAction)add:(id)sender {
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO list(name, sex) VALUES ('zbc', 'male');INSERT INTO list(name, sex) VALUES ('xxx', 'male');INSERT INTO list(name, sex) VALUES ('ddd', 'male');INSERT INTO list(name, sex) VALUES ('fff', 'male');INSERT INTO list(name, sex) VALUES ('ggg', 'male');INSERT INTO list(name, sex) VALUES ('hhh', 'male');INSERT INTO list(jjj, sex) VALUES ('kkk', 'male');INSERT INTO list(name, sex) VALUES ('lll', 'male');INSERT INTO list(name, sex) VALUES ('ggg', 'male');INSERT INTO list(name, sex) VALUES ('jjj', 'male');"];
    sqlite3_exec(database, sql.UTF8String, NULL, NULL, NULL);
}


- (IBAction)dele:(id)sender {
    
    /*删除数据*/
    
    NSString *sql = [NSString stringWithFormat:@"DELETE FROM list  WHERE id = 2;"];
    sqlite3_exec(database, sql.UTF8String, NULL, NULL, NULL);
}

- (IBAction)update:(id)sender {
    
    
    NSString *sql = [NSString stringWithFormat:@"UPDATE list SET sex = 'female' WHERE name = hhh;"];
    sqlite3_exec(database, sql.UTF8String, NULL, NULL, NULL);
}


- (IBAction)check:(id)sender {
    
    
    
    NSString *sql = [NSString stringWithFormat:@"SELECT * FROM list;"];
    sqlite3_exec(database, sql.UTF8String, NULL, NULL, NULL);
}


//1.生成路径
-(NSString *)path{
    
    NSArray *documentArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
    NSString *documentPath = [documentArr firstObject];
    // crylown.db 为数据库的名字
    NSString *path = [NSString stringWithFormat:@"%@/easy.db",documentPath];
    
    return path;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    
    //2.创建/打开数据库
//    sqlite3 *database;

    
    int databaseResult = sqlite3_open([[self path] UTF8String], &database);
    
    if (databaseResult != SQLITE_OK) {
        
        NSLog(@"创建/打开数据库失败,%d",databaseResult);
    }
    
    
    //3.创建表
    char *error;
    
    //    建表格式: create table if not exists 表名 (列名 类型,....)    注: 如需生成默认增加的id: id integer primary key autoincrement
    const char *createSQL = "create table if not exists list(id integer primary key autoincrement,name char,sex char)";
    
    int tableResult = sqlite3_exec(database, createSQL, NULL, NULL, &error);
    
    if (tableResult != SQLITE_OK) {
        
        NSLog(@"创建表失败:%s",error);
    }
    
    
    
    //4.添加数据
    
//    NSString *sql = [NSString stringWithFormat:@"INSERT INTO list(name, sex) VALUES ('zbc', 'male');INSERT INTO list(name, sex) VALUES ('xxx', 'male');INSERT INTO list(name, sex) VALUES ('ddd', 'male');INSERT INTO list(name, sex) VALUES ('fff', 'male');INSERT INTO list(name, sex) VALUES ('ggg', 'male');INSERT INTO list(name, sex) VALUES ('hhh', 'male');INSERT INTO list(jjj, sex) VALUES ('kkk', 'male');INSERT INTO list(name, sex) VALUES ('lll', 'male');INSERT INTO list(name, sex) VALUES ('ggg', 'male');INSERT INTO list(name, sex) VALUES ('jjj', 'male');"];
//    sqlite3_exec(database, sql.UTF8String, NULL, NULL, NULL);
    
}


- (IBAction) addTable {
    NSError *error = [dbManager doQuery:@"CREATE TABLE IF NOT EXISTS users (id integer primary key autoincrement, user text, password text);"];
    if (error != nil) {
        NSLog(@"Error: %@",[error localizedDescription]);
    }
    
    NSString *dump = [dbManager getDatabaseDump];
    
    _testView.text = dump;
    [_testView setNeedsDisplay];
    
}

- (IBAction) addUser {
    
    int user = arc4random() % 9999;
    int pass = arc4random() % 9999;
    
    
    NSString *sqlStr = [NSString stringWithFormat:@"insert into users (user, password) values ('%d','%d');",user, pass];
    NSError *error = [dbManager doQuery:sqlStr];
    if (error != nil) {
        NSLog(@"Error: %@",[error localizedDescription]);
    }
    
    NSString *dump = [dbManager getDatabaseDump];
    
    _testView.text = dump;
    [_testView setNeedsDisplay];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

#define SQLITE_OK           0   /* Successful result */
/* beginning-of-error-codes */
#define SQLITE_ERROR        1   /* SQL error or missing database */
#define SQLITE_INTERNAL     2   /* Internal logic error in SQLite */
#define SQLITE_PERM         3   /* Access permission denied */
#define SQLITE_ABORT        4   /* Callback routine requested an abort */
#define SQLITE_BUSY         5   /* The database file is locked */
#define SQLITE_LOCKED       6   /* A table in the database is locked */
#define SQLITE_NOMEM        7   /* A malloc() failed */
#define SQLITE_READONLY     8   /* Attempt to write a readonly database */
#define SQLITE_INTERRUPT    9   /* Operation terminated by sqlite3_interrupt()*/
#define SQLITE_IOERR       10   /* Some kind of disk I/O error occurred */
#define SQLITE_CORRUPT     11   /* The database disk image is malformed */
#define SQLITE_NOTFOUND    12   /* Unknown opcode in sqlite3_file_control() */
#define SQLITE_FULL        13   /* Insertion failed because database is full */
#define SQLITE_CANTOPEN    14   /* Unable to open the database file */
#define SQLITE_PROTOCOL    15   /* Database lock protocol error */
#define SQLITE_EMPTY       16   /* Database is empty */
#define SQLITE_SCHEMA      17   /* The database schema changed */
#define SQLITE_TOOBIG      18   /* String or BLOB exceeds size limit */
#define SQLITE_CONSTRAINT  19   /* Abort due to constraint violation */
#define SQLITE_MISMATCH    20   /* Data type mismatch */
#define SQLITE_MISUSE      21   /* Library used incorrectly */
#define SQLITE_NOLFS       22   /* Uses OS features not supported on host */
#define SQLITE_AUTH        23   /* Authorization denied */
#define SQLITE_FORMAT      24   /* Auxiliary database format error */
#define SQLITE_RANGE       25   /* 2nd parameter to sqlite3_bind out of range */
#define SQLITE_NOTADB      26   /* File opened that is not a database file */
#define SQLITE_NOTICE      27   /* Notifications from sqlite3_log() */
#define SQLITE_WARNING     28   /* Warnings from sqlite3_log() */
#define SQLITE_ROW         100  /* sqlite3_step() has another row ready */
#define SQLITE_DONE        101  /* sqlite3_step() has finished executing */
/* end-of-error-codes */

你可能感兴趣的:(sqlite3 的简单使用)