注意:在创建数据库时:要在工程中添加
.h文件
---------------------------------------------------------------
#import <Foundation/Foundation.h>
#import "sqlite3.h"
@interface User : NSObject
{
sqlite3 * linik;
NSString * path;
}
//添加四个数据
-(NSInteger )addNewGoods:(NSString*)addGoodname withPrice:(NSString *)addwithPrice andStorage : (NSString * )addGoodsStorage;
-(NSMutableArray *) showNewGoodsInformation;
@end
---------------------------------------------------------------
.m文件
#import "User.h"
@implementation User
-(id)init{
self =[super init];
//根目录
path =@"//Users/dc017/desktop/MicroMall.db";
//打开数据库
sqlite3_open([path UTF8String], &linik);
//创建表
NSString * createGoodsTable=@"create table if not exists goods(goods_id integer primary key autoincrement,goods_name varchar(40),goods_Price varchar(7),goods_storage varchar(8) )";
//执行
sqlite3_exec(linik, [createGoodsTable UTF8String], nil, nil, nil);
return self;
}
//插数据
-(NSInteger )addNewGoods:(NSString*)addGoodname withPrice:(NSString *)addwithPrice andStorage : (NSString * )addGoodsStorage{
//辅助
sqlite3_stmt * state;
NSString * goodsId;
//创建表
NSString * insertGoods=[NSString stringWithFormat:@"insert into goods(goods_name,goods_Price,goods_storage) values("%@","%@","%@")",addGoodname,addwithPrice,addGoodsStorage];
//如果每一个表都插入成功 SQLITE_OK代表都插进了
if (sqlite3_exec(linik, [insertGoods UTF8String], nil, nil, nil)==SQLITE_OK) {
//查找新添加商品Id
NSString * selectNewGoods=[NSString stringWithFormat:@"select goods_id from goods where goods_name="%@" ",addGoodname];
//预处理
sqlite3_prepare_v2(linik, [selectNewGoods UTF8String], -1, &state, nil);
// SQLITE_ROW代表每一行插进去了
while (sqlite3_step(state)==SQLITE_ROW) {
goodsId =[NSString stringWithUTF8String:( char *)sqlite3_column_text(state, 0)];
}
}
NSLog(@"jdsfhk");
return [goodsId integerValue];
}
//从数据库中提取字段
-(NSMutableArray *)showNewGoodsInformation{
//辅助的
sqlite3_stmt *state;
//接收从数据库中拿出来的值
NSString * goodsId;
NSString * goodsName;
NSString * goodsPrice;
NSString * goodsStorge;
//将值用可变数组接收
NSMutableArray * muarray=[[NSMutableArray alloc]initWithCapacity:10];
NSDictionary * dic;
//查看商品表
NSString * selectGoods=[[NSString alloc]initWithFormat:@"select * from goods order by goods_id desc;"];//order by goods_id desc 降序
//预编译
sqlite3_prepare_v2(linik, [selectGoods UTF8String], -1, &state, nil);
//取出下一行0,1,2,3列字段所有的值
while (sqlite3_step(state) == SQLITE_ROW) {
//取出地一列的所有值
goodsId =[[NSString alloc]initWithCString:(char *)sqlite3_column_text(state, 0) encoding:NSUTF8StringEncoding];
//取出第二列的所有值
goodsName = [[NSString alloc]initWithCString:(char *)sqlite3_column_text(state, 1) encoding:NSUTF8StringEncoding];
//取出第三列的所有值
goodsPrice =[[NSString alloc]initWithCString:(char *)sqlite3_column_text(state, 2) encoding:NSUTF8StringEncoding];
//取出第四列的所有值
goodsStorge =[[NSString alloc]initWithCString:(char *)sqlite3_column_text(state, 3) encoding:NSUTF8StringEncoding];
//先用字典包装
dic=@{ @"goodsId":goodsId,
@"goodsName":goodsName,
@"goodsPrice":goodsPrice,
@"goodsStorge":goodsStorge,
};
//再用数组包装
[muarray addObject:dic];
}
return muarray;
}
@end