sqlite是一款轻型的数据库,一般在几KB到几百KB,占用资源很低,很适合在移动设备上使用。
你可以在App Store中找到是收费下载的,也可以在网上直接搜索sqlite Mac就能找到免费的版本下载。
在文章结尾提供有sqlite Mac下载连接,和这篇文章的码源下载
下载并安装之后打开如图所示:
我们选中Tables然后点击左下角的加号就会弹出添加Table的界面,然后输入Table名称table1,并点击右侧的加号添加列,如图所示:
接着点击Create创建就行了,然后我们就能看到刚刚建的table1了。左键双击table1会出现添加列的窗口,右键单击就会出现Query的按钮点击就会出现添加行的窗口,在添加行的窗口右下角点击加号就可以添加行的内容了,记得添加过之后要点击Save。然后关闭窗体你就能看到我们建好的表格数据了。
然后我们需要建成如下图所示的两个Table:
好了数据库的使用就到这里了,下面我们需要把它添加到项目中并对其进行读写操作。
首先我们建一个用于和数据库交互的类:sqliteHelper,还有两个跟数据库中表格相对应的类:Datagamestage、Dataresearch。
下面我们在sqliteHelper的头文件中声明四个类方法:
//取单项
+(Datagamestage *)getItem:(int)gameid FileName:(NSString *)fileName;
//修改单项
+(BOOL)updateItem:(Datagamestage *)item FileName:(NSString *)fileName;
//取列表
+(NSMutableArray *)getList:(int)gameid FileName:(NSString *)fileName;
//修改列表
+(BOOL)updateList:(NSMutableArray*)list FileName:(NSString *)fileName;
这里我们主要对数据库最常用的读取和修改操作进行讲解,这四个方法是对单行和多行的的数据进行的操作,我们来讲一下取单项的方法:
首先我们需要实例化一个与数据库表格相对应的类:
Datagamestage *item = [[[Datagamestagealloc] init] autorelease];
然后进行数据库操作的第一步获取资源路径,在这里我使用的是相对路径,因为我们做的项目肯定要在不同的地方使用,只有使用相对路径才能每次都获取到正确的文件路径:
// 1.获取资源路径
NSString *resourcePath = [[NSBundlemainBundle] resourcePath];
NSString *path = [resourcePath stringByAppendingPathComponent:DB_NAME];
resourcePath是我们的项目资源打包路径,我们需要把制作好的数据库文件放到Resources文件夹中去并引用到项目中,并且需要把数据库名称在文件顶部进行宏定义:
#define DB_NAME @"database.sqlite"
接着我们需要判断一下文件路径是否正确:
NSFileHandle *fileManager = [NSFileHandlefileHandleForReadingAtPath:path];
if (fileManager)
{
// 接着要在这里添加
}
如果正确,我们开始数据库操作第二步打开数据库,这里我们需要在文件顶部引入sqlite3.h:#import <sqlite3.h>:
sqlite3 *database;
// 判断数据库是否打开
if(sqlite3_open([pathUTF8String],&database) != sqlITE_OK)
{
sqlite3_close(database);
}
else
{
// 接着要在这里添加
}
如果打开成功我们就要开始第三步准备sql语句:
// 3.准备sql语句
sqlite3_stmt *statement;
NSMutableString *sqlStr = [NSMutableStringstring];
[sqlStr appendFormat:@"select coin,mana_stone,stage,cur_nu,final,numberone,cur_fire,cur_ice,cur_light,gameID from %@",fileName];
[sqlStr appendFormat:@" where gameID = %i",gameid];
这里简单说一下有关数据库的语法操作,最主要的数据库操作就是增、删、改、查
,这里我们使用的是select(查),也可以说是读取,语法主干是select from where 。from前面是列名(如果是*则表示查询所有的列),后面是表名,where后面跟的是行id(主要是限定范围,如果没有则表示全部)。
// 4.执行sql语句
if(sqlite3_prepare_v2(database,[sqlStrUTF8String],-1,&statement,nil) != sqlITE_OK)
{
NSLog(@"Error to getItem from table %@",fileName);
}
else
{
// 接着要在这里添加
}
如果执行成功那么我们就需要把查询到的数据赋值给item(接收数据库数据):
while(sqlite3_step(statement) ==sqlITE_ROW)
{
//item.gameid = (int)sqlite3_column_int(statement,1);
item.data_coin = (int)sqlite3_column_int(statement,0);
item.data_mana_stone = (int)sqlite3_column_int(statement,1);
item.data_stage = (int)sqlite3_column_int(statement,2);
char *data_name = (char*)sqlite3_column_text(statement,3);
if (data_name != nil)
item.cur_nu_name = [NSStringstringWithUTF8String:data_name];
else
item.cur_nu_name =@"";
item.data_final_lock = (int)sqlite3_column_int(statement,4);
item.numberone = (int)sqlite3_column_int(statement,5);
char *cur_fire = (char*)sqlite3_column_text(statement,6);
if (cur_fire != nil)
item.cur_fire = [NSStringstringWithUTF8String:cur_fire];
else
item.cur_fire =@"";
char *cur_ice = (char*)sqlite3_column_text(statement,7);
if (cur_ice != nil)
item.cur_ice = [NSStringstringWithUTF8String:cur_ice];
else
item.cur_ice =@"";
char *cur_light = (char*)sqlite3_column_text(statement,8);
if (cur_light != nil)
item.cur_light = [NSStringstringWithUTF8String:cur_light];
else
item.cur_light =@"";
item.gameid = (int)sqlite3_column_int(statement,9);
}
当我们的数据读取操作结束之后,还需要释放sql资源,和关闭数据库:
// 5.释放sql资源
sqlite3_finalize(statement);
sqlite3_close(database);
还有在最后要返回item,这样我们的数据库读取操作就结束了。