当前位置:Gxlcms > 数据库问题 > SQLite关系数据库

SQLite关系数据库

时间:2021-07-01 10:21:17 帮助过:6人阅读

----------------------   SQLite 关系数据库    1.导入: 数据库-build phases -link binary with libraries -+libsqlite3.dylib   2.#import <sqlite3.h>   3.@interface ZYViewController () {     sqlite3 *_db; } @end   ------------------------- 4.打开数据库 (使用数据库打开,使用完毕,要关闭;) ------------------------- // sqlite3_open 给予一个文件名,检测存在,不存在则创建文件并打开,存在则打开。[返回值是int]           //第一个:文件名称,要转成 c语言  第二个:数据库的实例   - (BOOL)openDb {     NSString *fileName = [ NSHomeDirectory() stringByAppendingString:@"/Documents/db.sqlite3" ];         if ( sqlite3_open(fileName.UTF8String , &_db) == SQLITE_OK) {         NSLog(@" 数据库打开成功 ");         return YES ;         } else {         NSLog(@" 数据库打开失败 ");         return NO ;     } } ------------------------- 5.创建表 ------------------------- //创建一个表,如果不存在, 创建字段,分别是啥类型 //sqlite3_exec  1:句柄,2:sql 语句 3:回调函数 4:null 5:参数错误信息   -(void)createTable {     if ([ self openDb ] == NO) {         return;     }         const char *creatSql = "create table if not exists oneTable (id integer primary key,name text)";         if ( sqlite3_exec(_db , creatSql, NULL, NULL, NULL ) == SQLITE_OK) {         NSLog(@" -创建成功 ");     } else {         NSLog(@" -创建失败 ");     }         sqlite3_close( _db); }   ------------------------- 6.插入 ------------------------- 插入   //sqlite3_prepare_v2解析 sql语句,为插入数据做准备,转成内部能识别语句 //-1表示让系统来计算数据长度 id在第 0位置) //statemet声明   - (void)insertTable {     if ([ self openDb ] ==NO) {         return;     }       NSString *name = @"大牛" ;     NSInteger age = 23;         const char *theName = [name UTF8String ];         //sql语句     const char *insertTable = "insert into fristTable (name,age) values (-,-)";         //statemet声明     sqlite3_stmt *stmt;         if ( sqlite3_prepare_v2(_db , insertTable, -1, &stmt, NULL) == SQLITE_OK ) {        sqlite3_bind_text(stmt, 1 , theName, -1, NULL);        sqlite3_bind_int(stmt, 2 , age);    }        if ( sqlite3_step(stmt) == SQLITE_DONE ) {        NSLog(@" 插入成功");    }else    {        NSLog(@" 插入失败");    }      sqlite3_finalize(stmt);    sqlite3_close( _db); } ------------------------- 7.查询 ------------------------- //*代表查询去全部 //sqlite3_prepare_v2 1:句柄 2:sql 语句 3 sql长度(-1,系统计算) 4:取数据 5:一般写NUll   - (void)selectTable {     if ([ self openDb ] == NO) {         return;     }     const char *selectSql = "select * from fristTable" ; //  const char *selectSql = "select id,name,age from fristTable where id=1";     sqlite3_stmt *stmt;        sqlite3_prepare_v2(_db , selectSql, -1, &stmt, NULL);         while ( sqlite3_step(stmt) == SQLITE_ROW ) {         const unsigned   char *name  = sqlite3_column_text(stmt, 1 );         if (name != NULL ) {             NSString *getName = [NSString stringWithUTF8String:(const char *)name];             NSLog(@"name:%@" ,getName);                    }                 NSInteger theid = sqlite3_column_int (stmt, 0);         NSLog(@"id:%d" ,theid);                 NSInteger age = sqlite3_column_int (stmt, 2);         NSLog(@"age:%d" ,age);             }       sqlite3_finalize(stmt);     sqlite3_close( _db); }             

SQLite关系数据库

标签:

人气教程排行