加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

SQlite_3

发布时间:2020-12-12 19:38:16 所属栏目:百科 来源:网络整理
导读:简单易懂的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
简单易懂的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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读