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

使用sqlite3操作

发布时间:2020-12-12 20:10:19 所属栏目:百科 来源:网络整理
导读:在ios上使用sqlite3实现创建数据库,插入或更新数据记录,删除记录,查询记录功能。 创建数据表: create table sudent { integer id primary key, text name, text age, } 首先要引入 libsqlite3.dylib //////.h文件 #import Foundation/Foundation.h #impo

在ios上使用sqlite3实现创建数据库,插入或更新数据记录,删除记录,查询记录功能。

创建数据表:

create table sudent {

integer id primary key,

text name,

text age,

}


首先要引入 libsqlite3.dylib

//////.h文件

#import <Foundation/Foundation.h>

#import "sqlite3.h"

@interface myDBRecord : NSObject

+(NSString *)dataFilePath;

+(BOOL)openDataBase;

+(void)createTable;

+(void)insertRecord:(NSString *)name Age:(NSString *)age ;

+(void)deleteAllRecord ;

+(NSMutableArray *)selectRecordWidthSQL:(NSString *)sql;

@end

/////.m文件

#import "myDBRecord.h"

@implementation myDBRecord

static sqlite3 *database;

//数据库目录

+(NSString *)dataFilePath {

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask,YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

return [documentsDirectory stringByAppendingPathComponent:@"MyDB.db"];

}

//打开数据库

+(BOOL)openDataBase{

if (sqlite3_open([[myDBRecord dataFilePath] UTF8String],&database)!=SQLITE_OK) {

sqlite3_close(database);

NSLog(@"打开数据表失败。");

return NO;

}

return YES;

}

// 创建student数据表

+(void)createTable{

if ([self openDataBase]) {

char *errorMsg;

char *studentTable = "create table if not exists student (id integer primary key,my_name text,my_age text);";

if (sqlite3_exec (database,studentTable,NULL,&errorMsg) != SQLITE_OK) {

sqlite3_close(database);

NSLog(@"创建数据表失败。%@",errorMsg);

}

}

sqlite3_close(database);

}

// 插入记录数

+(void)insertRecord:(NSString *)name Age:(NSString *)age {

char *errorMsg = NULL;

//

char *insertSql = "INSERT OR REPLACE INTO student (my_name,my_age) VALUES (?,?)";

sqlite3_stmt *stmt;

if (sqlite3_prepare_v2(database,insertSql,-1,&stmt,nil) == SQLITE_OK) {

sqlite3_bind_text(stmt,1,[name UTF8String],NULL);

sqlite3_bind_text(stmt,2,[age UTF8String],NULL);

}

if (sqlite3_step(stmt) != SQLITE_DONE) {

NSLog(@"插入记录数失败。%@",errorMsg);

}

sqlite3_finalize(stmt);

//

}

//删除记录数

+(void)deleteAllRecord {

//

char *errorMsg;

char *studentTable = "delete from student;";

if (sqlite3_exec (database,&errorMsg) != SQLITE_OK) {

sqlite3_close(database);

NSLog(@"删除记录数失败。%@",errorMsg);

}

//

}

//查询数据数 sql = @"select * from student"

+(NSMutableArray *)selectRecordWidthSQL:(NSString *)sql {

NSMutableArray *arr=[[NSMutableArray alloc] init];

//

sqlite3_stmt *stmt;

if(sqlite3_prepare_v2(database,[sql UTF8String],nil) == SQLITE_OK){

while (sqlite3_step(stmt) == SQLITE_ROW) {

char *chmy_name = (char *)sqlite3_column_text(stmt,2); //2个字段

char *chmy_age = (char *)sqlite3_column_text(stmt,3);

if (chmy_name!=nil) {

[arr addObject:[NSString stringWithUTF8String:chmy_name]];

}

if (chmy_age!=nil) {

[arr addObject:[NSString stringWithUTF8String:chmy_age]];

}

}

sqlite3_finalize(stmt);

}

//

return [arr autorelease];

}

@end

(编辑:李大同)

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

    推荐文章
      热点阅读