第六十六篇、OC_Sqlite数据库操作
#import <Foundation/Foundation.h>
#import <sqlite3.h>
#define kFilename @"data.sqlite" @interface SQLService:NSObject
{
NSMutableArray *FSQLExecutioResultsMutableArray; //SQL运行结果
NSString *FErrorString; //错误信息
NSString *FDatabaseAddressString; //数据库文件存放地址
} @property (nonatomic) sqlite3 *FDatabaseSqlite; - (void)SetDatabaseAddressString:(NSString *)ANewString;
- (NSString *)GetErrorString;
- (NSMutableArray *)GetSQLExecutioResultsMutableArray; //- (BOOL)ExecuteSQL:(NSString *)ASQLString;
- (BOOL)CreateTable:(NSString *)ASQLString;
- (BOOL)InsertData:(NSString *)ASQLString;
- (BOOL)DeleteData:(NSString *)ASQLString;
- (BOOL)UpdateData:(NSString *)ASQLString;
- (BOOL)SelectData:(NSString *)ASQLString;
- (BOOL)SelectAllData:(NSString *)ASQLString;
- (BOOL)DropTable:(NSString *)ASQLString;
@end
#import "SQLService.h"
#define kLibrary [NSHomeDirectory() stringByAppendingPathComponent:@"Library"]
@interface SQLService () - (void)SetErrorString:(NSString *)ANewString;
- (void)SetSQLExecutioResultsMutableArray:(NSMutableArray *)ANewMutableArray;
- (NSString *)GetDatabaseAddressString; - (NSString *)DataFilePath;
- (BOOL)OpenDataBase; - (BOOL)CreateTable:(NSString *)ASQLString;
@end @implementation SQLService
@synthesize FDatabaseSqlite; - (id)init
{
if (![self GetSQLExecutioResultsMutableArray]) {
NSMutableArray *ASQLExecutioResultsMutableArray = [[NSMutableArray alloc] initWithCapacity:];
[self SetSQLExecutioResultsMutableArray:ASQLExecutioResultsMutableArray];
[ASQLExecutioResultsMutableArray release];
}
if (![self GetErrorString]) {
NSString *AErrorString = [[NSString alloc] init];
[self SetErrorString:AErrorString];
[AErrorString release];
}
return self;
} - (void)dealloc
{
[FSQLExecutioResultsMutableArray release];
[super dealloc];
} - (void)SetDatabaseAddressString:(NSString *)ANewString
{
FDatabaseAddressString = [ANewString retain];
}
- (NSString *)GetDatabaseAddressString
{
return FDatabaseAddressString;
}
- (void)SetErrorString:(NSString *)ANewString
{
if (FErrorString != ANewString) {
[FErrorString release];
FErrorString = [ANewString retain];
}
}
- (NSString *)GetErrorString
{
return FErrorString;
}
- (void)SetSQLExecutioResultsMutableArray:(NSMutableArray *)ANewMutableArray
{
if (FSQLExecutioResultsMutableArray != ANewMutableArray) {
[FSQLExecutioResultsMutableArray release];
FSQLExecutioResultsMutableArray = [ANewMutableArray retain];
}
}
- (NSMutableArray *)GetSQLExecutioResultsMutableArray
{
return FSQLExecutioResultsMutableArray;
} //获取document目录并返回数据库目录
- (NSString *)DataFilePath
{
NSString *ADataFilePathString;
ADataFilePathString = [kLibrary stringByAppendingPathComponent:kFilename];
return ADataFilePathString;
}
//创建、打开数据库
- (BOOL)OpenDataBase
{
BOOL AIsOpenSuccessedBool;
//获取数据库路径
NSString *ADataFilePathString = [self DataFilePath];
//文件管理器
NSFileManager *AFileManager = [NSFileManager defaultManager];
//判断数据库是否存在
BOOL AIsDataBaseExistBool = [AFileManager fileExistsAtPath:ADataFilePathString]; //如果数据库存在,则用sqlite3_open直接打开(不要担心,如果数据库不存在sqlite3_open会自动创建)
if (AIsDataBaseExistBool) {
// NSLog(@"Database file have already existed.");
//打开数据库,这里的[path UTF8String]是将NSString转换为C字符串,因为SQLite3是采用可移植的C(而不是
//Objective-C)编写的,它不知道什么是NSString.
const char *ATempChar = [ADataFilePathString UTF8String];
if(sqlite3_open(ATempChar, &FDatabaseSqlite) != SQLITE_OK) { //如果打开数据库失败则关闭数据库
sqlite3_close(FDatabaseSqlite);
//NSLog(@"Error: open database file failed.");
[self SetErrorString:@"Error: open database file failed."];
AIsOpenSuccessedBool = NO;
}
else {
AIsOpenSuccessedBool = YES;
}
}
//如果发现数据库不存在则利用sqlite3_open创建数据库(上面已经提到过),与上面相同,路径要转换为C字符串
else if(sqlite3_open([ADataFilePathString UTF8String], &FDatabaseSqlite) == SQLITE_OK) {
AIsOpenSuccessedBool = YES;
}
else {
//如果创建并打开数据库失败则关闭数据库
sqlite3_close(FDatabaseSqlite);
//NSLog(@"Error: create and open database file failed.");
[self SetErrorString:@"Error: create and open database file failed."];
AIsOpenSuccessedBool = NO;
}
return AIsOpenSuccessedBool;
}
/*
- (BOOL)ExecuteSQL:(NSString *)ASQLString
{
BOOL AIsSuccessedBool;
if ([self OpenDataBase]) {
sqlite3_stmt *AStatementSqlite3_stmt;
NSString *AErrorString = nil;
NSMutableArray *ASQLExecutioResultsMutableArray = [self GetSQLExecutioResultsMutableArray];
[ASQLExecutioResultsMutableArray removeAllObjects];
const char *ASQLChar = [ASQLString UTF8String];
//sqlite3_prepare_v2 接口把一条SQL语句解析到statement结构里去. 使用该接口访问数据库是当前比较好的的一种方法
NSInteger AParseSQLReturnInteger = sqlite3_prepare_v2(FDatabaseSqlite, ASQLChar, -1, &AStatementSqlite3_stmt, nil);
//第一个参数跟前面一样,是个sqlite3 * 类型变量,
//第二个参数是一个 sql 语句。
//第三个参数我写的是-1,这个参数含义是前面 sql 语句的长度。如果小于0,sqlite会自动计算它的长度(把sql语句当成以\0结尾的字符串)。
//第四个参数是sqlite3_stmt 的指针的指针。解析以后的sql语句就放在这个结构里。
//第五个参数我也不知道是干什么的。为nil就可以了。
//如果这个函数执行成功(返回值是 SQLITE_OK 且 statement 不为NULL ),那么下面就可以开始插入二进制数据。 //如果SQL语句解析出错
if(AParseSQLReturnInteger != SQLITE_OK) {
//NSLog(@"Error: Parse SQL failed");
AIsSuccessedBool = NO;
AErrorString = [[NSString alloc] initWithFormat:@"Error: %s", sqlite3_errmsg(FDatabaseSqlite)];
[self SetErrorString:AErrorString];
[AErrorString release];
}
else {
NSString *AOperationString = [ASQLString substringToIndex:6];
if ([AOperationString caseInsensitiveCompare:@"create"] == NSOrderedSame) {
[self CreateTable:ASQLChar Statement:AStatementSqlite3_stmt];
}
else if ([AOperationString caseInsensitiveCompare:@"INSERT"] == NSOrderedSame) {
[self InsertData:ASQLChar Statement:AStatementSqlite3_stmt];
}
else if ([AOperationString caseInsensitiveCompare:@"delete"] == NSOrderedSame) {
[self DeleteData:ASQLChar Statement:AStatementSqlite3_stmt];
}
else if ([AOperationString caseInsensitiveCompare:@"update"] == NSOrderedSame) {
[self UpdateData:ASQLChar Statement:AStatementSqlite3_stmt];
}
else if ([AOperationString caseInsensitiveCompare:@"SELECT"] == NSOrderedSame) {
[self SelectData:ASQLChar Statement:AStatementSqlite3_stmt];
}
else if ([[AOperationString substringToIndex:4] caseInsensitiveCompare:@"drop"] == NSOrderedSame) {
[self DropTable:ASQLChar Statement:AStatementSqlite3_stmt];
}
AIsSuccessedBool = YES;
} //释放sqlite3_stmt
sqlite3_finalize(AStatementSqlite3_stmt);
//关闭数据库
sqlite3_close(FDatabaseSqlite);
}
return AIsSuccessedBool;
}
*/
- (BOOL)CreateTable:(NSString *)ASQLString
{
BOOL AIsSuccessedBool=NO;
if ([self OpenDataBase]) { // NSLog(@"CreateTable%@",ASQLString); sqlite3_stmt *AStatementSqlite3_stmt;
NSString *AErrorString = nil;
NSMutableArray *ASQLExecutioResultsMutableArray = [self GetSQLExecutioResultsMutableArray];
[ASQLExecutioResultsMutableArray removeAllObjects];
const char *ASQLChar = [ASQLString UTF8String]; //sqlite3_prepare_v2 接口把一条SQL语句解析到statement结构里去. 使用该接口访问数据库是当前比较好的的一种方法
NSInteger AParseSQLReturnInteger = sqlite3_prepare_v2(FDatabaseSqlite, ASQLChar, -, &AStatementSqlite3_stmt, nil); //如果SQL语句解析出错
if(AParseSQLReturnInteger != SQLITE_OK) {
//NSLog(@"Error: Parse SQL failed");
AIsSuccessedBool = NO;
AErrorString = [[NSString alloc] initWithFormat:@"Error: %s", sqlite3_errmsg(FDatabaseSqlite)];
[self SetErrorString:AErrorString];
[AErrorString release];
}
else {
//执行SQL语句
int AExecuteSQLReturnInt = sqlite3_step(AStatementSqlite3_stmt); if (AExecuteSQLReturnInt != SQLITE_DONE)
{
AErrorString = [[NSString alloc] initWithFormat:@"Create Table failed!"];
[self SetErrorString:AErrorString];
[AErrorString release];
AIsSuccessedBool = NO;
}
else {
[self SetErrorString:@"Create Table Successed"];
AIsSuccessedBool = YES;
}
}
//释放sqlite3_stmt
sqlite3_finalize(AStatementSqlite3_stmt);
//关闭数据库
sqlite3_close(FDatabaseSqlite);
}
return AIsSuccessedBool;
} - (BOOL)InsertData:(NSString *)ASQLString
{
BOOL AIsSuccessedBool=NO;
if ([self OpenDataBase]) {
sqlite3_stmt *AStatementSqlite3_stmt;
NSString *AErrorString = nil;
NSMutableArray *ASQLExecutioResultsMutableArray = [self GetSQLExecutioResultsMutableArray];
[ASQLExecutioResultsMutableArray removeAllObjects];
const char *ASQLChar = [ASQLString UTF8String];
//sqlite3_prepare_v2 接口把一条SQL语句解析到statement结构里去. 使用该接口访问数据库是当前比较好的的一种方法
NSInteger AParseSQLReturnInteger = sqlite3_prepare_v2(FDatabaseSqlite, ASQLChar, -, &AStatementSqlite3_stmt, nil); //如果SQL语句解析出错
if(AParseSQLReturnInteger != SQLITE_OK) {
//NSLog(@"Error: Parse SQL failed");
AIsSuccessedBool = NO;
AErrorString = [[NSString alloc] initWithFormat:@"Error: %s", sqlite3_errmsg(FDatabaseSqlite)];
[self SetErrorString:AErrorString];
[AErrorString release];
}
else {
//执行SQL语句
int AExecuteSQLReturnInt = sqlite3_step(AStatementSqlite3_stmt);
if (AExecuteSQLReturnInt == SQLITE_ERROR)
{
AErrorString = [[NSString alloc] initWithFormat:@"Insert Data failed!"];
[self SetErrorString:AErrorString];
[AErrorString release];
AIsSuccessedBool = NO;
}
else {
[self SetErrorString:@"Insert Data Successed"];
AIsSuccessedBool = YES;
}
}
//释放sqlite3_stmt
sqlite3_finalize(AStatementSqlite3_stmt);
//关闭数据库
sqlite3_close(FDatabaseSqlite);
}
return AIsSuccessedBool;
} - (BOOL)DeleteData:(NSString *)ASQLString
{
BOOL AIsSuccessedBool=NO;
if ([self OpenDataBase]) {
sqlite3_stmt *AStatementSqlite3_stmt;
NSString *AErrorString = nil;
NSMutableArray *ASQLExecutioResultsMutableArray = [self GetSQLExecutioResultsMutableArray];
[ASQLExecutioResultsMutableArray removeAllObjects];
const char *ASQLChar = [ASQLString UTF8String];
//sqlite3_prepare_v2 接口把一条SQL语句解析到statement结构里去. 使用该接口访问数据库是当前比较好的的一种方法
NSInteger AParseSQLReturnInteger = sqlite3_prepare_v2(FDatabaseSqlite, ASQLChar, -, &AStatementSqlite3_stmt, nil); //如果SQL语句解析出错
if(AParseSQLReturnInteger != SQLITE_OK) {
//NSLog(@"Error: Parse SQL failed");
AIsSuccessedBool = NO;
AErrorString = [[NSString alloc] initWithFormat:@"Error: %s", sqlite3_errmsg(FDatabaseSqlite)];
[self SetErrorString:AErrorString];
[AErrorString release];
}
else {
//执行SQL语句
int AExecuteSQLReturnInt = sqlite3_step(AStatementSqlite3_stmt); if (AExecuteSQLReturnInt == SQLITE_ERROR)
{
AErrorString = [[NSString alloc] initWithFormat:@"Delete Data failed!"];
[self SetErrorString:AErrorString];
[AErrorString release];
AIsSuccessedBool = NO;
}
else {
[self SetErrorString:@"Delete Data Successed"];
AIsSuccessedBool = YES;
}
}
//释放sqlite3_stmt
sqlite3_finalize(AStatementSqlite3_stmt);
//关闭数据库
sqlite3_close(FDatabaseSqlite);
}
return AIsSuccessedBool;
} - (BOOL)UpdateData:(NSString *)ASQLString
{
BOOL AIsSuccessedBool=NO;
if ([self OpenDataBase]) {
sqlite3_stmt *AStatementSqlite3_stmt;
NSString *AErrorString = nil;
NSMutableArray *ASQLExecutioResultsMutableArray = [self GetSQLExecutioResultsMutableArray];
[ASQLExecutioResultsMutableArray removeAllObjects];
const char *ASQLChar = [ASQLString UTF8String];
//sqlite3_prepare_v2 接口把一条SQL语句解析到statement结构里去. 使用该接口访问数据库是当前比较好的的一种方法
NSInteger AParseSQLReturnInteger = sqlite3_prepare_v2(FDatabaseSqlite, ASQLChar, -, &AStatementSqlite3_stmt, nil); //如果SQL语句解析出错
if(AParseSQLReturnInteger != SQLITE_OK) {
//NSLog(@"Error: Parse SQL failed");
AIsSuccessedBool = NO;
AErrorString = [[NSString alloc] initWithFormat:@"Error: %s", sqlite3_errmsg(FDatabaseSqlite)];
[self SetErrorString:AErrorString];
[AErrorString release];
}
else {
//执行SQL语句
int AExecuteSQLReturnInt = sqlite3_step(AStatementSqlite3_stmt); if (AExecuteSQLReturnInt == SQLITE_ERROR)
{
AErrorString = [[NSString alloc] initWithFormat:@"Update Data failed!"];
[self SetErrorString:AErrorString];
[AErrorString release];
AIsSuccessedBool = NO;
}
else {
[self SetErrorString:@"Update Data Successed"];
AIsSuccessedBool = YES;
}
}
//释放sqlite3_stmt
sqlite3_finalize(AStatementSqlite3_stmt);
//关闭数据库
sqlite3_close(FDatabaseSqlite);
}
return AIsSuccessedBool;
} - (BOOL)SelectData:(NSString *)ASQLString
{
BOOL AIsSuccessedBool=NO;
if ([self OpenDataBase]) {
sqlite3_stmt *AStatementSqlite3_stmt;
NSString *AErrorString = nil;
NSMutableArray *ASQLExecutioResultsMutableArray = [self GetSQLExecutioResultsMutableArray];
[ASQLExecutioResultsMutableArray removeAllObjects];
const char *ASQLChar = [ASQLString UTF8String];
//sqlite3_prepare_v2 接口把一条SQL语句解析到statement结构里去. 使用该接口访问数据库是当前比较好的的一种方法
NSInteger AParseSQLReturnInteger = sqlite3_prepare_v2(FDatabaseSqlite, ASQLChar, -, &AStatementSqlite3_stmt, nil); //如果SQL语句解析出错
if(AParseSQLReturnInteger != SQLITE_OK) {
//NSLog(@"Error: Parse SQL failed");
AIsSuccessedBool = NO;
AErrorString = [[NSString alloc] initWithFormat:@"Error: %s", sqlite3_errmsg(FDatabaseSqlite)];
[self SetErrorString:AErrorString];
[AErrorString release];
}
else {
NSMutableArray *ASQLExecutioResultsMutableArray = [self GetSQLExecutioResultsMutableArray];
char *ATempChar;
NSUInteger ANumberOLineDatasUInteger; //每行数据个数
NSUInteger AIndexUInteger;
NSString *ATempString;
//NSUInteger ASqliteColumnTypeUInteger; //某列数据类型
//执行SQL语句
while (sqlite3_step(AStatementSqlite3_stmt) == SQLITE_ROW) {
ANumberOLineDatasUInteger = sqlite3_column_count(AStatementSqlite3_stmt);
for (AIndexUInteger = ; AIndexUInteger < ANumberOLineDatasUInteger; AIndexUInteger++) {
ATempChar = (char*)sqlite3_column_text(AStatementSqlite3_stmt, AIndexUInteger);
if (ATempChar!=nil) {
ATempString=[[NSString alloc] initWithCString:ATempChar encoding:NSUTF8StringEncoding];
}
else {
ATempString=[[NSString alloc] initWithFormat:@""];
}
[ASQLExecutioResultsMutableArray addObject:ATempString];
[ATempString release];
}
}
AIsSuccessedBool = YES; }
//释放sqlite3_stmt
sqlite3_finalize(AStatementSqlite3_stmt);
//关闭数据库
sqlite3_close(FDatabaseSqlite);
}
return AIsSuccessedBool;
}
- (BOOL)SelectAllData:(NSString *)ASQLString
{
BOOL AIsSuccessedBool=NO;
if ([self OpenDataBase]) {
sqlite3_stmt *AStatementSqlite3_stmt;
NSString *AErrorString = nil;
NSMutableArray *ASQLExecutioResultsMutableArray = [self GetSQLExecutioResultsMutableArray];
[ASQLExecutioResultsMutableArray removeAllObjects];
const char *ASQLChar = [ASQLString UTF8String];
//sqlite3_prepare_v2 接口把一条SQL语句解析到statement结构里去. 使用该接口访问数据库是当前比较好的的一种方法
NSInteger AParseSQLReturnInteger = sqlite3_prepare_v2(FDatabaseSqlite, ASQLChar, -, &AStatementSqlite3_stmt, nil); //如果SQL语句解析出错
if(AParseSQLReturnInteger != SQLITE_OK) {
//NSLog(@"Error: Parse SQL failed");
AIsSuccessedBool = NO;
AErrorString = [[NSString alloc] initWithFormat:@"Error: %s", sqlite3_errmsg(FDatabaseSqlite)];
[self SetErrorString:AErrorString];
[AErrorString release];
}
else {
NSMutableArray *ASQLExecutioResultsMutableArray = [self GetSQLExecutioResultsMutableArray];
char *ATempChar;
NSUInteger ANumberOLineDatasUInteger; //每行数据个数
NSUInteger AIndexUInteger;
NSString *ATempString;
//NSUInteger ASqliteColumnTypeUInteger; //某列数据类型
//执行SQL语句
while (sqlite3_step(AStatementSqlite3_stmt) == SQLITE_ROW) {
ANumberOLineDatasUInteger = sqlite3_column_count(AStatementSqlite3_stmt);
NSMutableArray *ARowMutableArray=[[NSMutableArray alloc] initWithCapacity:];
for (AIndexUInteger = ; AIndexUInteger < ANumberOLineDatasUInteger; AIndexUInteger++) {
ATempChar = (char*)sqlite3_column_text(AStatementSqlite3_stmt, AIndexUInteger);
if (ATempChar!=nil) {
ATempString=[[NSString alloc] initWithCString:ATempChar encoding:NSUTF8StringEncoding];
}
else {
ATempString=[[NSString alloc] initWithFormat:@""];
}
[ARowMutableArray addObject:ATempString];
[ATempString release];
}
[ASQLExecutioResultsMutableArray addObject:ARowMutableArray];
[ARowMutableArray release];
}
AIsSuccessedBool = YES; }
//释放sqlite3_stmt
sqlite3_finalize(AStatementSqlite3_stmt);
//关闭数据库
sqlite3_close(FDatabaseSqlite);
}
return AIsSuccessedBool;
} - (BOOL)DropTable:(NSString *)ASQLString
{
BOOL AIsSuccessedBool=NO;
if ([self OpenDataBase]) {
sqlite3_stmt *AStatementSqlite3_stmt;
NSString *AErrorString = nil;
NSMutableArray *ASQLExecutioResultsMutableArray = [self GetSQLExecutioResultsMutableArray];
[ASQLExecutioResultsMutableArray removeAllObjects];
const char *ASQLChar = [ASQLString UTF8String];
//sqlite3_prepare_v2 接口把一条SQL语句解析到statement结构里去. 使用该接口访问数据库是当前比较好的的一种方法
NSInteger AParseSQLReturnInteger = sqlite3_prepare_v2(FDatabaseSqlite, ASQLChar, -, &AStatementSqlite3_stmt, nil); //如果SQL语句解析出错
if(AParseSQLReturnInteger != SQLITE_OK) {
//NSLog(@"Error: Parse SQL failed");
AIsSuccessedBool = NO;
AErrorString = [[NSString alloc] initWithFormat:@"Error: %s", sqlite3_errmsg(FDatabaseSqlite)];
[self SetErrorString:AErrorString];
[AErrorString release];
}
else {
//执行SQL语句
int AExecuteSQLReturnInt = sqlite3_step(AStatementSqlite3_stmt); if (AExecuteSQLReturnInt == SQLITE_ERROR)
{
AErrorString = [[NSString alloc] initWithFormat:@"Drop Table failed!"];
[self SetErrorString:AErrorString];
[AErrorString release];
AIsSuccessedBool = NO;
}
else {
[self SetErrorString:@"Drop Table Successed"];
AIsSuccessedBool = YES;
}
}
//释放sqlite3_stmt
sqlite3_finalize(AStatementSqlite3_stmt);
//关闭数据库
sqlite3_close(FDatabaseSqlite);
}
return AIsSuccessedBool;
} @end
使用代码:
#import <Foundation/Foundation.h>
#import "SqlService/SQLService.h"
@interface OperatingTheCartTable : NSObject
{ }
/*Cart 表字段
Id
ProductId 产品ID
ProductName 产品名臣
Price 价格
Quantity 数量
ProductColor 产品颜色
ProductSize 产品尺寸
ProductSellAttributeId 产品销售属性ID
*/
+(NSInteger)GetNumberOfProduct;
+(BOOL)CreateCartTable;
+(NSMutableArray *)SelectFromCartTable;
+(NSMutableArray *)SelectFromCartTableWith:(NSInteger)AProductIdInteger;
+(BOOL)DeleteFromCartTable:(NSInteger)AIDInteger ProductSellAttributeId:(NSInteger)AProductSellAttributeIdInteger;
+(BOOL)InsertIntoCartTable:(NSInteger )AProductIdInteger ProductName:(NSString *)ANameString Price:(float )APriceFloat Quantity:(NSInteger)AQuantity ProductColor:(NSString *)AProductColor ProductSize:(NSString *)AProductSize ProductSellAttributeId:(NSInteger)AProductSellAttributeIdInteger;
+(BOOL)UpdateTheNumberOfCart:(NSInteger)AIDInteger Quantity:(NSInteger)AQuantityInteger ProductColor:(NSString *)AProductColor ProductSize:(NSString *)AProductSize ProductSellAttributeId:(NSInteger)AProductSellAttributeIdInteger;
#import "OperatingTheCartDatabase.h" #define kLibrary [NSHomeDirectory() stringByAppendingPathComponent:@"Library"]
#define KDataBaseName @"Cart.sqlite"
@implementation OperatingTheCartTable #pragma mark - 表 Cart
+(NSInteger)GetNumberOfProduct
{
NSInteger ACountInteger=;
SQLService *ASQLService=[[[SQLService alloc] init] autorelease];
NSString *ASelectString=[NSString stringWithFormat:@"select count(*) from Cart"]; [ASQLService SelectData:ASelectString];
NSMutableArray *AMutableArray=[ASQLService GetSQLExecutioResultsMutableArray];
if(AMutableArray.count <= )
{
return ;
}
ACountInteger = [[AMutableArray objectAtIndex:] integerValue];
return ACountInteger;
} //ProductSellAttributeId
+(BOOL)CreateCartTable
{
SQLService *ASQLService=[[SQLService alloc] init];
NSString *ACreateBOOKSetingTable=[NSString stringWithFormat:@"create table if not exists Cart(Id INTEGER PRIMARY KEY autoincrement,ProductId INTEGER,ProductName nvarchar(250),Price float,Quantity INTEGER,ProductColor nvarchar(250),ProductSize nvarchar(250),ProductSellAttributeId INTEGER)"];
if(![ASQLService CreateTable:ACreateBOOKSetingTable])
{
NSLog(@"创建表失败%@",[ASQLService GetErrorString]);
[ASQLService release];
return NO;
}
[ASQLService release];
return YES;
} +(NSMutableArray *)SelectFromCartTable
{
SQLService *ASQLService=[[[SQLService alloc] init] autorelease];
NSString *ASelectString=[NSString stringWithFormat:@"select * from Cart order by Id DESC"]; [ASQLService SelectAllData:ASelectString];
NSMutableArray *AMutableArray=[ASQLService GetSQLExecutioResultsMutableArray];
for (int i=; i<AMutableArray.count; i++) {
NSMutableDictionary *AMutableDictionary=[[NSMutableDictionary alloc] initWithCapacity:];
NSMutableArray *ATempMutableArray=[AMutableArray objectAtIndex:i]; [AMutableDictionary setValue:[ATempMutableArray objectAtIndex:]forKey:@"ProductId"];
[AMutableDictionary setValue:[ATempMutableArray objectAtIndex:]forKey:@"ProductName"];
[AMutableDictionary setValue:[ATempMutableArray objectAtIndex:]forKey:@"Price"];
[AMutableDictionary setValue:[ATempMutableArray objectAtIndex:]forKey:@"Quantity"];
if ([ATempMutableArray count] >= )
[AMutableDictionary setValue:[ATempMutableArray objectAtIndex:]forKey:@"ProductColor"];
if ([ATempMutableArray count] >= )
[AMutableDictionary setValue:[ATempMutableArray objectAtIndex:]forKey:@"ProductSize"];
if ([ATempMutableArray count] >= )
[AMutableDictionary setValue:[ATempMutableArray objectAtIndex:]forKey:@"ProductSellAttributeId"]; [AMutableArray replaceObjectAtIndex:i withObject:AMutableDictionary]; [AMutableDictionary release];
} return AMutableArray;
} //2013-10-31 11:41:57.457 CloudWork[2565:1a903] 红色,39,281
//2013-10-31 11:41:57.457 CloudWork[2565:1a903] 红色,37,278 +(NSMutableArray *)SelectFromCartTableWith:(NSInteger)AProductIdInteger
{
SQLService *ASQLService=[[[SQLService alloc] init] autorelease];
NSString *ASelectString=[NSString stringWithFormat:@"select * from Cart where ProductId=%d order by Id desc",AProductIdInteger];
[ASQLService SelectAllData:ASelectString];
NSMutableArray *AMutableArray=[ASQLService GetSQLExecutioResultsMutableArray];
for (int i=; i<AMutableArray.count; i++) {
NSMutableDictionary *AMutableDictionary=[[NSMutableDictionary alloc] initWithCapacity:];
NSMutableArray *ATempMutableArray=[AMutableArray objectAtIndex:i]; [AMutableDictionary setValue:[ATempMutableArray objectAtIndex:]forKey:@"ProductId"];
[AMutableDictionary setValue:[ATempMutableArray objectAtIndex:]forKey:@"ProductName"];
[AMutableDictionary setValue:[ATempMutableArray objectAtIndex:]forKey:@"Price"];
[AMutableDictionary setValue:[ATempMutableArray objectAtIndex:]forKey:@"Quantity"];
if ([ATempMutableArray count] >= )
[AMutableDictionary setValue:[ATempMutableArray objectAtIndex:]forKey:@"ProductColor"];
if ([ATempMutableArray count] >= )
[AMutableDictionary setValue:[ATempMutableArray objectAtIndex:]forKey:@"ProductSize"];
if ([ATempMutableArray count] >= )
[AMutableDictionary setValue:[ATempMutableArray objectAtIndex:]forKey:@"ProductSellAttributeId"]; [AMutableArray replaceObjectAtIndex:i withObject:AMutableDictionary];
NSLog(@",%@,%@,%@,%@",[AMutableDictionary valueForKey:@"Quantity"],[AMutableDictionary valueForKey:@"ProductColor"],[AMutableDictionary valueForKey:@"ProductSize"],[AMutableDictionary valueForKey:@"ProductSellAttributeId"]);
[AMutableDictionary release];
} return AMutableArray; } +(BOOL)DeleteFromCartTable:(NSInteger)AIDInteger ProductSellAttributeId:(NSInteger)AProductSellAttributeIdInteger
{
BOOL ABOOL=NO;
NSString *ADeleteString=[NSString stringWithFormat:@"delete from Cart where ProductId=%d and ProductSellAttributeId=%d",AIDInteger,AProductSellAttributeIdInteger];
SQLService *ASQLService=[[SQLService alloc] init];
if([ASQLService DeleteData:ADeleteString])
{
ABOOL=YES;
}
else {
ABOOL=NO;
}
[ASQLService release];
return ABOOL;
}
+(BOOL)InsertIntoCartTable:(NSInteger )AProductIdInteger ProductName:(NSString *)ANameString Price:(float )APriceFloat Quantity:(NSInteger)AQuantity ProductColor:(NSString *)AProductColor ProductSize:(NSString *)AProductSize ProductSellAttributeId:(NSInteger)AProductSellAttributeIdInteger
{
BOOL ABOOL=NO;
NSString *AInsertString=[NSString stringWithFormat:@"insert into Cart(ProductId,ProductName,Price,Quantity,ProductColor,ProductSize,ProductSellAttributeId) values(%d,'%@',%f,%d,'%@','%@',%d)",AProductIdInteger,ANameString,APriceFloat,AQuantity,AProductColor,AProductSize,AProductSellAttributeIdInteger];
SQLService *ASQLService=[[SQLService alloc] init];
if([ASQLService InsertData:AInsertString])
{
ABOOL=YES;
}
else {
ABOOL=NO;
}
[ASQLService release];
return ABOOL;
}
+(BOOL)UpdateTheNumberOfCart:(NSInteger)AIDInteger Quantity:(NSInteger)AQuantityInteger ProductColor:(NSString *)AProductColor ProductSize:(NSString *)AProductSize ProductSellAttributeId:(NSInteger)AProductSellAttributeIdInteger
{
BOOL ABOOL=NO;
NSString *AUpdateString=[NSString stringWithFormat:@"update Cart set Quantity=%d,ProductColor='%@',ProductSize='%@' where ProductId=%d and ProductSellAttributeId=%d",AQuantityInteger,AProductColor,AProductSize,AIDInteger,AProductSellAttributeIdInteger];
SQLService *ASQLService=[[SQLService alloc] init]; if ([ASQLService UpdateData:AUpdateString]) {
ABOOL=YES;
}
else {
ABOOL=NO;
}
[ASQLService release];
return ABOOL;
}
@end
第六十六篇、OC_Sqlite数据库操作的更多相关文章
- 《手把手教你》系列技巧篇(六十六)-java+ selenium自动化测试 - 读写excel文件 - 上篇(详细教程)
1.简介 在自动化测试,有些我们的测试数据是放到excel文件中,尤其是在做数据驱动测试的时候,所以需要懂得如何操作获取excel内的内容.由于java不像python那样有直接操作Excle文件的类 ...
- 第三百六十六节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)的bool组合查询
第三百六十六节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)的bool组合查询 bool查询说明 filter:[],字段的过滤,不参与打分must:[] ...
- “全栈2019”Java第六十六章:抽象类与接口详细对比
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...
- 我的Android六章:Android中SQLite数据库操作
今天学习的内容是Android中的SQLite数据库操作,在讲解这个内容之前小编在前面有一篇博客也是讲解了SQLite数据库的操作,而那篇博客的讲解是讲述了 如何在Window中通过DOM来操作数据库 ...
- 十分钟学会mysql数据库操作
Part1:写在最前 MySQL安装的方式有三种: ①rpm包安装 ②二进制包安装 ③源码安装 这里我们推荐二进制包安装,无论从安装速度还是用于生产库安装环境来说,都是没问题的.现在生产库一般采用My ...
- 十六、CI框架之数据库操作get用法
一.使用数据库的Get方法读取内容,如下代码: 二.数据库如下: 二.效果如下: 不忘初心,如果您认为这篇文章有价值,认同作者的付出,可以微信二维码打赏任意金额给作者(微信号:382477247)哦, ...
- 第六十四天 JS基础操作
一.分支结构 1.if语句 if基础语句 if(条件表达式){ 代码块: } // 当条件表达式结果为true,会执行代码块:反之不执行 // 条件表达式可以为普通表达式 // 0.undefined ...
- 第六十二篇、AFN3.0封装网络请求框架,支持缓存
1.网络请求 第一种实现方式: 功能:GET POST 请求 缓存逻辑: 1.是否要刷新本地缓存,不需要就直接发起无缓存的网络请求,否则直接读取本地数据 2.需要刷新本地缓存,先读取本地数据,有就返回 ...
- python【第十二篇下】操作MySQL数据库以及ORM之 sqlalchemy
内容一览: 1.Python操作MySQL数据库 2.ORM sqlalchemy学习 1.Python操作MySQL数据库 2. ORM sqlachemy 2.1 ORM简介 对象关系映射(英语: ...
随机推荐
- C++ 临时对象
1.什么是临时对象? swap方法中,常常定义一个temp对象,这个temp对象不是临时对象,而是局部对象.这里所说的临时对象是不可见的,在原代码中是看不到的. 2.为什么会产生临时对象? a.客户期 ...
- 我所经历的SAP选型
这是一个失败的选型项目,而且在可遇见的未来公司也不会再经历SAP选型,甚至不会再启动erp项目,个中原因很难一言道尽,在此简要的说说我们的选型过程以及在选型过程中对各种因素的考虑. 一.重启选型工作七 ...
- Android Studio升级后projectBuild failed.
近期在升级Android Studio后,发现原先能编译通过的project,突然就编译只是了,原因是生成的AndroidManifest.xml文件里有乱码. 升级后: android studio ...
- Android常用代码集合
这篇文章主要记录一些常用的一些代码段,方便以后查阅,不断更新中. 1:调用浏览器,载入某网址 1 2 3 Uri uri = Uri.parse("http://www.android-st ...
- Nginx Rewrite 实现匹配泛域名规则
Nginx 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器. Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru ...
- cc代码学习笔记1
#define #define INT32 int #define INT8 char #define CHAR char #define SSHORT signed short #define IN ...
- mysql升级小结和mysql_upgrade的用途
http://blog.itpub.net/15480802/viewspace-1412259/ mysql升级 1 升级方式 分为In-place和out-of-place,前者直接覆盖当前版本, ...
- 小白日记9:kali渗透测试之主动信息收集(二)四层发现:TCP、UDP、nmap、hping、scapy
四层发现 四层发现的目的是扫描出可能存活的IP地址,四层发现虽然涉及端口扫描,但是并不对端口的状态进行精确判断,其本质是利用四层协议的一些通信来识别主机ip是否存在. 四层发现的优点: 1.可路由且结 ...
- jQuery Validate 插件[表单验证]
在客户端添加信息提交表单时我们经常需要做一些验证,比如验证不能为空,验证客户输入手机格式,验证客户输入email,url等的格式,我们可以通过EL表达式结合js 进行自主验证,今天总结一个JQuery ...
- 使用JS制作一个鼠标可拖的DIV(三)——移动带图片DIV
当DIV元素里,存在图片元素的时候,会使拖动出现异常. 因为图片元素本身就支持拖动,所以,只要在图片标签加入:draggable='false'. 如下: <div onmousedown=&q ...