第六十六篇、OC_Sqlite数据库操作
生活随笔
收集整理的這篇文章主要介紹了
第六十六篇、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:1];[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, -1, &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, -1, &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, -1, &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, -1, &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, -1, &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 = 0; 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, -1, &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:1];for (AIndexUInteger = 0; 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, -1, &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 表字段IdProductId 產品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=0;SQLService *ASQLService=[[[SQLService alloc] init] autorelease];NSString *ASelectString=[NSString stringWithFormat:@"select count(*) from Cart"];[ASQLService SelectData:ASelectString];NSMutableArray *AMutableArray=[ASQLService GetSQLExecutioResultsMutableArray];if(AMutableArray.count <= 0){return 0;}ACountInteger = [[AMutableArray objectAtIndex:0] 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=0; i<AMutableArray.count; i++) {NSMutableDictionary *AMutableDictionary=[[NSMutableDictionary alloc] initWithCapacity:1];NSMutableArray *ATempMutableArray=[AMutableArray objectAtIndex:i];[AMutableDictionary setValue:[ATempMutableArray objectAtIndex:1]forKey:@"ProductId"];[AMutableDictionary setValue:[ATempMutableArray objectAtIndex:2]forKey:@"ProductName"];[AMutableDictionary setValue:[ATempMutableArray objectAtIndex:3]forKey:@"Price"];[AMutableDictionary setValue:[ATempMutableArray objectAtIndex:4]forKey:@"Quantity"];if ([ATempMutableArray count] >= 6)[AMutableDictionary setValue:[ATempMutableArray objectAtIndex:5]forKey:@"ProductColor"];if ([ATempMutableArray count] >= 7)[AMutableDictionary setValue:[ATempMutableArray objectAtIndex:6]forKey:@"ProductSize"];if ([ATempMutableArray count] >= 8)[AMutableDictionary setValue:[ATempMutableArray objectAtIndex:7]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=0; i<AMutableArray.count; i++) {NSMutableDictionary *AMutableDictionary=[[NSMutableDictionary alloc] initWithCapacity:1];NSMutableArray *ATempMutableArray=[AMutableArray objectAtIndex:i];[AMutableDictionary setValue:[ATempMutableArray objectAtIndex:1]forKey:@"ProductId"];[AMutableDictionary setValue:[ATempMutableArray objectAtIndex:2]forKey:@"ProductName"];[AMutableDictionary setValue:[ATempMutableArray objectAtIndex:3]forKey:@"Price"];[AMutableDictionary setValue:[ATempMutableArray objectAtIndex:4]forKey:@"Quantity"];if ([ATempMutableArray count] >= 6)[AMutableDictionary setValue:[ATempMutableArray objectAtIndex:5]forKey:@"ProductColor"];if ([ATempMutableArray count] >= 7)[AMutableDictionary setValue:[ATempMutableArray objectAtIndex:6]forKey:@"ProductSize"];if ([ATempMutableArray count] >= 8)[AMutableDictionary setValue:[ATempMutableArray objectAtIndex:7]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?
轉載于:https://www.cnblogs.com/HJQ2016/p/5986336.html
總結
以上是生活随笔為你收集整理的第六十六篇、OC_Sqlite数据库操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [译]15个关于Chrome的开发必备小
- 下一篇: Django基础之Model创建表