如何在数据库操作中插入更新删除并将数据库保存在文档目录中[关闭]

Posted

技术标签:

【中文标题】如何在数据库操作中插入更新删除并将数据库保存在文档目录中[关闭]【英文标题】:How to insert update delete in database operation and also save database in document directory [closed] 【发布时间】:2013-09-23 06:39:15 【问题描述】:

我需要通过查询获取数据库并插入更新删除,以便任何类都可用于简单操作

 +(void)createEditableCopyOfDatabaseIfNeeded 
    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentDirectory stringByAppendingPathComponent:@"YOUR DB NAME"];
    success = [fileManager fileExistsAtPath:writableDBPath];
    if (success) 
        return;
    
    NSString *defualtDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"YOUR DB NAME"];
    success = [fileManager copyItemAtPath:defualtDBPath toPath:writableDBPath error:&error];

    if (!success) 
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"Database Error!!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
    

我需要通过查询完成操作...

【问题讨论】:

你有没有试过在谷歌上找到这个。看看Goodle search和一个reference link 您解决了或需要帮助吗?? 【参考方案1】:

在下面给出的创建文件 把这个写在database.h文件中

    #import <Foundation/Foundation.h>
    #import <sqlite3.h>

    @interface Database : NSObject 
   
   +(NSString* )getDatabasePath;
   +(NSMutableArray *)executeQuery:(NSString*)str;
   +(NSString*)encodedString:(const unsigned char *)ch;
   +(BOOL)executeScalarQuery:(NSString*)str;
   @end

这将在 database.m 文件中

   #import "Database.h"
 #import <sqlite3.h>


  @implementation Database

  +(NSString* )getDatabasePath
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"YOURDBNAME"];
return writableDBPath;

    

  +(NSMutableArray *)executeQuery:(NSString*)str
  
sqlite3_stmt *statement= nil;
sqlite3 *database;
NSString *strPath = [self getDatabasePath];
NSMutableArray *allDataArray = [[NSMutableArray alloc] init];
if (sqlite3_open([strPath UTF8String],&database) == SQLITE_OK) 

    if (sqlite3_prepare_v2(database, [str UTF8String], -1, &statement, NULL) == SQLITE_OK) 
    
        while (sqlite3_step(statement) == SQLITE_ROW) 
        
            NSInteger i = 0;
            NSInteger iColumnCount = sqlite3_column_count(statement);
            NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
            while (i< iColumnCount) 
                NSString *str = [self encodedString:(const unsigned char*)sqlite3_column_text(statement, i)];
                NSString *strFieldName = [self encodedString:(const unsigned char*)sqlite3_column_name(statement, i)];
                [dict setObject:str forKey:strFieldName];
                i++;
            

            [allDataArray addObject:dict];
            [dict release];
        
    
    else 
         NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
    

    sqlite3_finalize(statement);
 
sqlite3_close(database);
return allDataArray;
  
  +(BOOL)executeScalarQuery:(NSString*)str

sqlite3_stmt *statement= nil;
sqlite3 *database;
BOOL fRet = NO;
NSString *strPath = [self getDatabasePath];
if (sqlite3_open([strPath UTF8String],&database) == SQLITE_OK) 
    if (sqlite3_prepare_v2(database, [str UTF8String], -1, &statement, NULL) == SQLITE_OK) 
        if (sqlite3_step(statement) == SQLITE_DONE)
            fRet =YES;
    

    sqlite3_finalize(statement);
 
sqlite3_close(database);
return fRet;
   


   +(NSString*)encodedString:(const unsigned char *)ch
    
NSString *retStr;
if(ch == nil)
    retStr = @"";
else
    retStr = [NSString stringWithCString:(char*)ch encoding:NSUTF8StringEncoding];
return retStr;
   

   @end

【讨论】:

以上是关于如何在数据库操作中插入更新删除并将数据库保存在文档目录中[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

如何在查询中使用 $push 将数据插入子文档,而不是检索文档并将其保存回来

C# winform中dataGridView控件如何插入、删除、更新记录,操作完成后怎样写回数并保存据库,怎么写代码?

NGXS状态管理:在调用操作时更改状态并将其保存到DB

如何在 SQL Server 触发器中复制插入、更新、删除的行

C#中datagridview控件 如何进行删除修改添加数据并保存到数据库中

如何使用 Python 更新、插入和删除 Redshift 数据库?