SQlite_3

前端之家收集整理的这篇文章主要介绍了SQlite_3前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
简单易懂的demo
// 1. UserDB.h

#import <Foundation/Foundation.h>

@interface UserDB : NSObject

- (void)createTable;

- (void)insertTable;

- (void)selectTable;

- (void)deleteTable;

@end


// UserDB.m
#import "UserDB.h"
#import <sqlite3.h>

@implementation UserDB

- (void)createTable {
    
    sqlite3 *sqlite = nil;
    NSString *filePath = [self localPath];
    // 1- 打开数据库
    int result =  sqlite3_open([filePath UTF8String],&sqlite);
    if (result != sqlITE_OK) {
        NSLog(@"打开数据库失败");
        return;
    }
    
    // 2- 创建表的sql语句
    NSString *sql = @"CREATE TABLE IF NOT EXISTS User (username TEXT primary key,password TEXT,email TEXT)";
    
    // 3- 执行sql语句
    char *error = nil;
    result = sqlite3_exec(sqlite,[sql UTF8String],NULL,&error);
    if (result != sqlITE_OK) {
        NSLog(@"创建表失败");
        return;
    }
    
    // 4- 关闭数据库
    sqlite3_close(sqlite);
    NSLog(@"创建表成功");
    
    
    
}

- (void)insertTable {
    
    sqlite3 *sqlite = nil;
    NSString *filePath = [self localPath];
    // 1- 打开数据库
    int result = sqlite3_open([filePath UTF8String],&sqlite);
    if (result != sqlITE_OK) {
        NSLog(@"打开数据库失败");
        return;
    }
    // 2- 创建数据库语句 (占位)
    NSString *sql = @"INSERT INTO User(username,password,email) VALUES (?,?,?)";
    // 编译
    sqlite3_stmt *stmt = nil;
    result = sqlite3_prepare_v2(sqlite,-1,&stmt,NULL);
    if (result != sqlITE_OK) {
        NSLog(@"准备失败");
        return;
    }
    // 3- 填充和填充数据
    NSString *username = @"Charles";
    NSString *password = @"123";
    NSString *email = @"75517668@qq.com";
    sqlite3_bind_text(stmt,1,[username UTF8String],NULL);
    sqlite3_bind_text(stmt,2,[password UTF8String],3,[email UTF8String],NULL);
    
    // 4- 执行sql语句
    result = sqlite3_step(stmt);
    if (result == sqlITE_ERROR || result == sqlITE_MISUSE) {
        NSLog(@"执行sql语句失败");
        return;
    }
    // 5- 关闭数据库句柄 和 关闭数据库
    sqlite3_finalize(stmt);
    sqlite3_close(sqlite);
    NSLog(@"数据插入成功");
    
}

- (void)selectTable {
    sqlite3 *sqlite = nil;
    NSString *filePath = [self localPath];
    
    // 1- 打开数据库
    int result = sqlite3_open([filePath UTF8String],&sqlite);
    if (result != sqlITE_OK) {
        NSLog(@"数据库打开失败");
        return;
    }
    // 2- 创建sql语句
    NSString *sql = @"select username,email from user where username=?";
    
    // 3- 编译 和 绑定
    sqlite3_stmt *stmt = nil;
    sqlite3_prepare_v2(sqlite,NULL);
    NSString *username = @"Charles";
    sqlite3_bind_text(stmt,NULL);
    
    result = sqlite3_step(stmt);
    
    if (result == sqlITE_ERROR || result == sqlITE_MISUSE) {
        NSLog(@"执行sql语句失败");
        return ;
    }
    // 4- 返回遍历的每一行
    while (result == sqlITE_ROW) {
        char *username = (char *)sqlite3_column_text(stmt,0);
        char *password = (char *)sqlite3_column_text(stmt,1);
        char *email = (char *)sqlite3_column_text(stmt,2);
        
        NSString *uname = [self encoding:username];
        NSString *pword = [self encoding:password];
        NSString *mail = [self encoding:email];
        
        NSLog(@"用户名:%@ 密码:%@ 邮箱:%@",uname,pword,mail);
        result = sqlite3_step(stmt);
    }
    
    // 5- 关闭句柄 和 关闭数据库
    sqlite3_finalize(stmt);
    sqlite3_close(sqlite);
    
    
    
    
}

- (void)deleteTable {
    sqlite3 *sqlite = nil;
    // 1- 打开数据库
    NSString *filePath = [self localPath];
    int result = sqlite3_open([filePath UTF8String],&sqlite);
    if (result != sqlITE_OK) {
        NSLog(@"打开数据库失败");
        return ;
    }
    // 2-sql语句
    NSString *sql = @"delete from user where username=?";
    sqlite3_stmt *stmt = nil;
    
    // 3- 准备和绑定
    sqlite3_prepare_v2(sqlite,NULL);
    
    // 4-执行句柄
    result = sqlite3_step(stmt);
    if (result == sqlITE_ERROR || result == sqlITE_MISUSE) {
        NSLog(@"删除失败");
        return ;
    }
    
    // 5-关闭句柄 和  关闭数据库
    sqlite3_finalize(stmt);
    sqlite3_close(sqlite);
    
    
}

- (NSString *)encoding:(char *)cString {
    return [NSString stringWithCString:cString encoding:NSUTF8StringEncoding];
}

- (NSString *)localPath {
    
    
    // 创建路径
    NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) objectAtIndex:0];
    filePath = [filePath stringByAppendingPathComponent:@"data.sqlite"];
    return filePath;
}

@end
原文链接:https://www.f2er.com/sqlite/199294.html

猜你在找的Sqlite相关文章