IOS嵌入式数据库SQLite3使用简析
1.Sqlite3数据类型及存储类 每个存放在sqlite数据库中(或者由这个数据库引擎操作)的值都有下面中的一个存储类: (1)NULL,值是NULL (2)INTEGER,值是有符号整形,根据值的大小以1,2,3,4,6或8字节存放 (3)REAL,值是浮点型值,以8字节IEEE浮点数存放 (4)TEXT,值是文本字符串,使用数据库编码(UTF-8,UTF-16BE或者UTF-16LE)存放 (5)BLOB,只是一个数据块,完全按照输入存放(即没有准换) 注:①Sqlite没有单独的布尔存储类型,它使用INTEGER作为存储类 型,0为false,1为true ②Sqlite没有另外为存储日期和时间设定一个存储类集,内置的sqlite日期和时间函数能够将日期和时间以TEXT,REAL或INTEGER形式存放 TEXT作为IS08601字符串("YYYY-MM-DD HH:MM:SS.SSS") REAL从格林威治时间11月24日,4174 B.C中午以来的天数 INTEGER从1970-01-01 00:00:00 UTC以来的秒数 2.Sqlite3函数功能简介 1)基本函数及结构体 sqlite3 *pdb//数据库句柄,跟文件句柄FILE很类似 sqlite3_stmt *stmt //这个相当于ODBC的Command对象,用于保存编译好的SQL语 句 sqlite3_open() //打开数据库 sqlite3_exec() //执行非查询的sql语句 sqlite3_prepare() //准备sql语句,执行select语句或者要使用parameter bind 时 , 用这个函数(封装了sqlite3_exec). sqlite3_step() //在调用sqlite3_prepare后,使用这个函数在记录集中移动。 sqlite3_close() //关闭数据库文件 2)绑定函数 int sqlite3_bind_null(sqlite3_stmt*, int); int sqlite3_bind_int(sqlite3_stmt*, int, int); int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*)); int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*)); 3)取值函数 sqlite3_column_text(),取text类型的数据 sqlite3_column_blob(),取blob类型的数据 sqlite3_column_int(),取int类型的数据 3.Sqlite3使用步骤 1) 首先获取iPhone上Sqlite 3的数据库文件的地址 2) 打开Sqlite 3的数据库文件 3) 定义SQL文 4) 邦定执行SQL所需要的参数 5) 执行SQL文,并获取结果 6) 释放资源 7) 关闭Sqlite 3数据库。 4.Sqlite3数据操作 由于整个例子代码比较繁琐,这里只列出数据库操作的部分代码作为参考: 1.添加开发包libsqlite3.0.dylib 首先是设置项目文件,在项目中添加iPhone版的sqlite3的数据库的开发包,在项目下的Frameworks点击右键,然后选择libsqlite3.0.dylib文件。 libsqlite3.0.dylib 文件地址: /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.2.sdk/usr/lib/libsqlite3.0.dylib 2.获取sqlite3的数据库文件地址: //数据库路径 -(NSString*)databasePath { NSArray*path=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); NSString*pathname=[pathobjectAtIndex:0]; return[pathnamestringByAppendingPathComponent:@”database.sqlite3”]; } 3.打开数据库 -(BOOL)openDatabase { if(sqlite3_open([[selfdatabasePath]UTF8String],&database)!=SQLITE_OK) { sqlite3_close(database); printf("failedtoopenthedatabase"); returnNO; } else { printf("openthedatabasesuccessfully"); returnYES; } } 4.创建表 //创建TimerTable表 -(BOOL)createTimerTable { if([selfopenDatabase]==YES) { char*erroMsg; NSString*createSQL=[NSStringstringWithFormat:@"CREATETABLEIFNOTEXISTS%@(timeridINTEGERPRIMARYKEYAUTOINCREMENT,timeINTEGER,remaintimeINTEGER,iconuriBLOB,vibrateINTEGER,statusINTEGER,messageTEXT)",TableName]; if(sqlite3_exec(database,[createSQLUTF8String],NULL,NULL,&erroMsg)!=SQLITE_OK) { sqlite3_close(database); printf("createtablefaild"); returnNO; } else { printf("tablewascreated"); returnYES; } } else returnNO; } 5.添加数据 //添加Timer -(BOOL)insertTimer:(TimerInfo*)timerInfo { boolisOpen=[selfopenDatabase]; if(isOpen!=YES) { returnNO; } sqlite3_stmt*statement; staticchar*insertTimerSql="INSERTINTOTimerTable(time,remaintime,iconuri,vibrate,status,message,type)VALUES(?,?,?,?,?,?)"; if(sqlite3_prepare_v2(database,insertTimerSql,-1,&statement,NULL)!=SQLITE_OK) { NSLog(@"Error:Failedtoinserttimer"); returnNO; } sqlite3_bind_int(statement,1,timerInfo.time);//timerInfo是一个封装了相关属性的实体类对象 sqlite3_bind_int(statement,2,timerInfo.remainTime); sqlite3_bind_text(statement,3,[timerInfo.iconuriUTF8String],-1,SQLITE_TRANSIENT); sqlite3_bind_int(statement,4,timerInfo.vibrate); sqlite3_bind_int(statement,5,timerInfo.status); sqlite3_bind_text(statement,6,[timerInfo.messageUTF8String],-1,SQLITE_TRANSIENT); intsuccess=sqlite3_step(statement); sqlite3_finalize(statement); if(success==SQLITE_ERROR) { NSLog(@"Error:failtoinsertintothedatabasewithmessage."); returnNO; } NSLog(@"insertedonetimer"); returnYES; } 6.查询数据 //查询数据库中所有的TimerInfo,返回一个包含所有TimerInfo的可变数组 -(NSMutableArray*)getAllTimers { NSMutableArray*arrayTimers=[[NSMutableArrayalloc]init]; NSString*queryStr=@"SELECT*FROMTimerTable"; sqlite3_stmt*statement; if(sqlite3_prepare_v2(database,[queryStrUTF8String],-1,&statement,NULL)!=SQLITE_OK) { printf("Failedtogetalltimers!\n"); } else { while(sqlite3_step(statement)==SQLITE_ROW) { TimerInfo*timerInfo=[[TimerInfoalloc]init]; timerInfo.timerId=sqlite3_column_int(statement,0); timerInfo.time=sqlite3_column_int(statement,1); timerInfo.remainTime=sqlite3_column_int(statement,2); timerInfo.vibrate=sqlite3_column_int(statement,4); timerInfo.status=sqlite3_column_int(statement,5); char*messageChar=sqlite3_column_text(statement,6); if(messageChar==NULL) timerInfo.message=nil; else timerInfo.message=[NSStringstringWithUTF8String:messageChar]; [arrayTimersaddObject:timerInfo]; [timerInforelease]; } } sqlite3_finalize(statement); NSLog(@"arrayTimersCount:%i",[arrayTimerscount]); returnarrayTimers; } 此外,还有数据库数据的更新及其删除操作就不再依依列出,它们之间只是执行的SQL文不同,其他部分的代码较为类似。 运行效果:(具体页面控件及事件:略) 注:由于我自己目前仍是IOS开发的初学者,在这里只是做下小结,其中难免有说的不对的地方,所以希望大家多多给予指正。 本文转自HDDevTeam 51CTO博客,原文链接:http://blog.51cto.com/hddev/863169,如需转载请自行联系原作者