SQlite_3

SQlite_3,第1张

概述简单易懂的demo// 1. UserDB.h#import <Foundation/Foundation.h>@interface UserDB : NSObject- (void)createTable;- (void)insertTable;- (void)selectTable;- (void)deleteTable;@end// UserDB.m#i
简单易懂的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 = @"[email protected]";    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
总结

以上是内存溢出为你收集整理的SQlite_3全部内容,希望文章能够帮你解决SQlite_3所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/1169411.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)

保存