在 iOS 中以编程方式将 SQLite 数据导出到 Excel

2023-11-27

在我的应用程序中,我使用 sqlite 作为后端(在本地存储数据)。 我能够将数据插入到我的表中。但是我想要做的是,想要以编程方式将所有 sqlite 数据导入到 excel 中。而且我不想为此应用程序使用服务器。一旦生成 excel 工作表,用户应该能够邮寄该表。 这在 iPhone 中可能吗: 请帮帮我。

以下是我将数据插入表的代码:

-(IBAction)Login
{
sqlite3_stmt *stmt;
        char *errorMsg; 
        char *update1 = "insert into Login1 values (?,?,?,?);";
        int x = sqlite3_prepare_v2(database, update1, -1, &stmt, nil);

    if (x == SQLITE_OK) 
    { 
        sqlite3_bind_text(stmt, 1, NULL,-1, NULL);
        sqlite3_bind_text(stmt, 2, [USERID UTF8String],-1, NULL);
        sqlite3_bind_text(stmt, 3, [str1 UTF8String],-1, NULL);
        sqlite3_bind_text(stmt, 4, [str4 UTF8String],-1, NULL);


    } 

    if (sqlite3_step(stmt) != SQLITE_DONE)
        NSLog(@"Error: %@",errorMsg); 
    sqlite3_finalize(stmt);

}


对于我执行此操作的应用程序,SQLite 数据相当大。因此,我使用后台线程将所有数据导出到 CSV(逗号分隔值)文件,Excel 可以导入该文件,然后打开邮件编辑器,并将 CSV 文件作为附件。如果您的数据很小,您可能不需要使用后台线程:

- (IBAction) export: (id) sender
{    
    // in my full code, I start a UIActivityIndicator spinning and show a 
    //  message that the app is "Exporting ..."

    [self performSelectorInBackground: @selector(exportImpl) withObject: nil];
}

Here is exportImpl

- (void) exportImpl
{
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

    NSArray* documentPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSSystemDomainMask, YES);
    NSString* documentsDir = [documentPaths objectAtIndex:0];
    NSString* csvPath = [documentsDir stringByAppendingPathComponent: @"export.csv"];

    // TODO: mutex lock?
    [sqliteDb exportCsv: csvPath];

    [pool release];

    // mail is graphical and must be run on UI thread
    [self performSelectorOnMainThread: @selector(mail:) withObject: csvPath waitUntilDone: NO];
}

- (void) mail: (NSString*) filePath
{
    // here I stop animating the UIActivityIndicator

    // http://howtomakeiphoneapps.com/home/2009/7/14/how-to-make-your-iphone-app-send-email-with-attachments.html
    BOOL success = NO;
    if ([MFMailComposeViewController canSendMail]) {
        // TODO: autorelease pool needed ?
        NSData* database = [NSData dataWithContentsOfFile: filePath];

        if (database != nil) {
            MFMailComposeViewController* picker = [[MFMailComposeViewController alloc] init];
            picker.mailComposeDelegate = self;
            [picker setSubject:[NSString stringWithFormat: @"%@ %@", [[UIDevice currentDevice] model], [filePath lastPathComponent]]];

            NSString* filename = [filePath lastPathComponent];
            [picker addAttachmentData: database mimeType:@"application/octet-stream" fileName: filename];
            NSString* emailBody = @"Attached is the SQLite data from my iOS device.";
            [picker setMessageBody:emailBody isHTML:YES];

            [self presentModalViewController:picker animated:YES];
            success = YES;
            [picker release];
        }
    }

    if (!success) {
        UIAlertView* warning = [[UIAlertView alloc] initWithTitle: @"Error"
                                                          message: @"Unable to send attachment!"
                                                         delegate: self
                                                cancelButtonTitle: @"Ok"
                                                otherButtonTitles: nil];
        [warning show];
        [warning release];
    }
}

然后,我有一个类封装了所有 SQLite 数据。该类是唯一进行 sqlite 调用的类。在本课程中,我有一种将数据导出到应用程序中的 CSV 文件的方法caches目录。变量sqliteDb上面的代码是这个类的一个实例。导出数据的方法如下:

-(void) exportCsv: (NSString*) filename
{
    // We record this filename, because the app deletes it on exit
    self.tempFile = filename;

    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
    // Setup the database object
    sqlite3* database;

    // Open the database from the users filessytem
    if (sqlite3_open([self.databasePath UTF8String], &database) == SQLITE_OK)
    {
        [self createTempFile: filename];
        NSOutputStream* output = [[NSOutputStream alloc] initToFileAtPath: filename append: YES];
        [output open];
        if (![output hasSpaceAvailable]) {
            NSLog(@"No space available in %@", filename);
            // TODO: UIAlertView?
        } else {
            NSString* header = @"Source,Time,Latitude,Longitude,Accuracy\n";
            NSInteger result = [output write: [header UTF8String] maxLength: [header length]];
            if (result <= 0) {
                NSLog(@"exportCsv encountered error=%d from header write", result);
            }

            BOOL errorLogged = NO;
            NSString* sqlStatement = @"select timestamp,latitude,longitude,horizontalAccuracy from my_sqlite_table";

            // Setup the SQL Statement and compile it for faster access
            sqlite3_stmt* compiledStatement;
            if (sqlite3_prepare_v2(database, [sqlStatement UTF8String], -1, &compiledStatement, NULL) == SQLITE_OK)
            {
                 // Loop through the results and write them to the CSV file
                 while (sqlite3_step(compiledStatement) == SQLITE_ROW) {
                     // Read the data from the result row
                     NSInteger secondsSinceReferenceDate = (NSInteger)sqlite3_column_double(compiledStatement, 0);
                     float lat = (float)sqlite3_column_double(compiledStatement, 1);
                     float lon = (float)sqlite3_column_double(compiledStatement, 2);
                     float accuracy = (float)sqlite3_column_double(compiledStatement, 3);

                     if (lat != 0 && lon != 0) {
                         NSDate* timestamp = [[NSDate alloc] initWithTimeIntervalSinceReferenceDate: secondsSinceReferenceDate];
                         NSString* line = [[NSString alloc] initWithFormat: @"%@,%@,%f,%f,%d\n",
                                       table, [dateFormatter stringFromDate: timestamp], lat, lon, (NSInteger)accuracy];
                         result = [output write: [line UTF8String] maxLength: [line length]];
                         if (!errorLogged && (result <= 0)) {
                             NSLog(@"exportCsv write returned %d", result);
                             errorLogged = YES;
                         }
                         [line release];
                         [timestamp release];
                     }
                     // Release the compiled statement from memory
                     sqlite3_finalize(compiledStatement);
                 }
            }
        }
        [output close];
        [output release];
    }

    sqlite3_close(database);
    [pool release];
}

-(void) createTempFile: (NSString*) filename {
    NSFileManager* fileSystem = [NSFileManager defaultManager];
    [fileSystem removeItemAtPath: filename error: nil];

    NSMutableDictionary* attributes = [[NSMutableDictionary alloc] init];
    NSNumber* permission = [NSNumber numberWithLong: 0640];
    [attributes setObject: permission forKey: NSFilePosixPermissions];
    if (![fileSystem createFileAtPath: filename contents: nil attributes: attributes]) {
        NSLog(@"Unable to create temp file for exporting CSV.");
        // TODO: UIAlertView?
    }
    [attributes release];
}

我的代码正在导出位置信息的数据库。显然,里面exportCsv,您需要将我的 sqlite 调用替换为适合您的数据库内容的调用。

此外,代码将数据存储在临时文件中。您可能需要决定何时清除这些临时文件。

显然,这段代码是在 ARC 可用之前编写的。根据需要进行调整。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 iOS 中以编程方式将 SQLite 数据导出到 Excel 的相关文章

  • 当点击 UITableViewCell 的子视图时引发选择事件 (didSelectRowAtIndexPath)

    我创建了一个自定义 UITableViewCell 其中包含许多子视图 在大多数情况下 我希望 UITableViewCell 的控制器来处理事件 在一种情况下 我希望子视图简单地将事件传递给父 UITableViewCell 这将导致它在
  • 如何在 iPhone 应用程序的 url 中传递字符串值

    NSURLRequest request NSURLRequest requestWithURL NSURL URLWithString http www krsconnect no community api html method ba
  • just_audio 无法在 ios flutter 上工作未处理的异常:(-11800)操作无法完成

    我正在尝试从它自己的存储库运行 just audio 示例项目https github com ryanheise just audio tree master just audio example https github com rya
  • 让约束在尺寸类别中发挥作用

    所以 我正在 Xcode 6 beta 中尝试尺寸类 我对图像设置了一些限制 使其根据 iPhone 纵向和横向对应的尺寸类别处于不同的位置 这些限制在下图中可见 正如您所看到的 当我处于紧凑 紧凑状态时 一些约束被 安装 而其他约束则没有
  • 将 HTML 字符串加载到 UIWebView 中的延迟

    我在导航控制器中有两个视图控制器 第一个视图控制器有一个带有按钮的菜单 按下此按钮将移动到第二个视图控制器并将 html 字符串加载到 UIWebView 中 没有其他东西被加载到 webview 中 只是一个简单的 NSString 其中
  • 无法下载应用程序 - 此时无法下载“APP”

    我的应用程序有 PLUS 版本和常规版本 我使用不同的目标对它们进行存档 我将 ipa 上传到 TestFlight 也上传到我的曲棍球服务器 PLUS 版本总是下载得很好 但普通版本总是给我 无法下载应用程序 错误 我根本没有更改两个版本
  • 标签中的文字大小

    如何限制标签中的字符数 您可以通过设置自动截断带有省略号的文本frame of the UILabel NSInteger newSize 10 label frame CGRectMake label frame origin x lab
  • 连接到 Apple Music

    所以我尝试使用 React Native 应用程序从 iOS 设备连接到 Apple Music 有一个 API 可以执行相同的操作 但我需要从 storekit 框架调用一个函数 提出个性化请求 苹果音乐API https develop
  • Facebook 登录 Apple CNA

    问题 是否可以设置 Facebook 登录以在 CNA 中使用 是否为开发人员提供 CNA 文档 您可以使用任何开发人员工具调试 CNA 屏幕吗 Details 我创建了一个使用电子邮件提交表单或 Facebook 登录按钮的强制门户登录页
  • 获取 Swift 子目录中资源的所有 URL

    我正在尝试为 iOS 应用程序的子目录中的所有资源创建 URL 数组 我似乎无法到达正确的路径 即使我不知道名称 我也希望能够检索 URL 即我不想将文件名硬编码到代码中 Below is a screen shot of the hier
  • 子视图控制器旋转方法未被调用

    Summary 我试图将子视图控制器添加到父视图控制器 并让父视图控制器通知子视图控制器旋转事件 但是 旋转消息不会转发到子视图控制器 这是默认行为 为什么这种默认行为没有发生 环境 iOS 7 XCode 5 OSX 10 9 Detai
  • 当你在 sqlite 中打开/关闭标题时会发生什么?

    当你在 sqlite 中打开 关闭标题时会发生什么 标题对应什么 这是什么意思 如果您在 sqlite3 命令行客户端中指的是以下内容 headers on off Turn display of headers on or off 启用该
  • 带约束的 Swift 动画

    是否可以通过改变约束来制作 UIView 动画 基本上 我想要动画myv UIView 具有 x y 高度和宽度约束 使用 UIView animateWithDuration 1 5 通过改变旧的限制 是的 这是可能的 你可以这样做 fu
  • iOS:addConstraints:应用程序崩溃

    Problem 我似乎无法在现有项目中采用自动布局 Details 我之前也遇到过与此问题相同的问题presentViewController 在 iOS 但所提供的答案都不是我的解决方案 我正在使用所有没有 xib 的故事板视图 我的 使
  • Apple Mach-O 链接器错误 armv7s 和 libGoogleAdMobAds.a

    我刚刚升级了我的应用程序以在新的 iPhone5 模拟器上运行 但是当我尝试为我的 iPhone 4S 设备构建它时 我收到此 Apple Mach O Liner 错误 ld 文件是通用的 3片 但不包含 n armv7s 切片 User
  • iPhone,如何将一张图像叠加到另一张图像上以创建要保存的新图像? (水印)

    基本上 我想拍摄用户从照片库中选择的图像 然后应用水印 即右下角的三角形 上面有应用程序名称 我已经在 Photoshop 中用透明层制作了第二张图像 我尝试了一个函数 我不记得它的确切名称 但它涉及 CGIImages 和蒙版 这将两个图
  • AVAudioMixerNode pan 或 AVAudioUnitSamplerstereoPan 属性无法更改 AVAudioEngine 声音输出的左/右平衡

    我有以下代码 它播放单个 MIDI 音符 但我希望能够调整平衡 平移 以便它仅从左扬声器或右扬声器或某些组合中播放 我认为更改 sampler stereoPan 或 engine mainMixerNode pan 也许可以解决问题 但它
  • Objective-C 声明的 @property 属性(非原子、复制、强、弱)

    有人可以向我详细解释一下我何时必须使用每个属性 nonatomic copy strong weak等等 对于声明的属性 并解释每个属性的作用是什么 某种例子也很好 我正在使用ARC 非原子的 Nonatomic https stackov
  • 如何在 XCode5 中将部署目标更改为 5.1.1 [重复]

    这个问题在这里已经有答案了 我正在一个项目中工作 我需要支持 iOS 5 1 1 但在 部署目标 的下拉菜单中我没有 5 1 1 作为选项 我的问题是如何将 iOS 5 1 1 添加为部署目标 我将非常感谢你的帮助 如果您愿意 您可以在框中
  • NSURLConnection 是否自动保留从服务器发送的 cookie?

    我从 ios 登录到我的龙卷风后端并发回 secure cookie 我注意到只要验证我设置的 secure cookie 我还可以请求其他信息 NSURLConnection 会保留 cookie 多久 或者关闭应用程序后 cookie

随机推荐

  • 攻击者可以有害地使用检查元素吗?

    我知道这是一个广泛的问题 但我认为我在这里遗漏了一些东西 攻击者是否可以通过简单地使用检查元素并编辑 javascript 和 html 来对站点造成损坏 例如 对于某人来说 更改输入的最大长度并上传太多数据可能会导致服务器崩溃 这似乎太容
  • C# 中的数组大小(长度)

    如何在 C 中确定数组的大小 长度 项目数 如果是一维数组a a Length 将给出的元素数量a If b是一个矩形多维数组 例如 int b new int 3 5 b Rank 将给出维数 2 和 b GetLength dimens
  • 多处理函数上的超时装饰器

    我有这个装饰器直接取自我在网上找到的一个例子 class TimedOutExc Exception pass def timeout timeout def decorate f def handler signum frame rais
  • 如何从 Azure DevOps 部署到 AWS Kubernetes

    我使用 Azure DevOps 来处理 PBI 存储库 PRS 和构建 但我的所有基础设施 包括 Kubernetes 均由 AWS 管理 没有文档 也没有关于如何使用 Azure DevOps 任务部署到 AWS EKS 的 正确且简单
  • 翻译动画的工作原理:Android

    我正在尝试移动一个RelativeLayout using TranslateAnimation 我为执行相同操作而编写的代码是 translateAnimation new TranslateAnimation 0 0 heightOfR
  • Spring HandlerInterceptors是如何实例化的?

    每个请求是否都有一个新的 Spring HandlerInterceptors 实例 我在 Spring 中有一个拦截器 它有一个类字段 public class MyInterceptor extends HandlerIntercept
  • 在 Cocoa KVO 中,为什么 NSMutableArray 代理上的更改不会通知观察者?

    我正在实施一个DocumentsManageriOS 中的类 我想创建一个名为的多对多属性documents符合 KVO 要求 它似乎大部分工作 并且我的 KVO 访问器和修改器方法被调用 然而 令我困扰的是 直接在NSMutableArr
  • 使用facet时ggplot2在面板边框之外

    我希望在多面图的外部有一个边框 但没有分隔图内面板的线 问题是 panel border 在构面中的每个面板周围绘制一个边框 而没有选择只在整个图周围有一个边框 或者 您可以将内部分隔线设置为 白色 但保持外部边框为 黑色 这是我的代码 m
  • Xcode 项目范围编译器标志

    使用 Xcode 4 2 和 LLVM 编译器 在针对 ARMv6 进行编译时 生成的应用程序中存在一些非常奇怪的错误 例如 CGSize 的 width 属性返回 height 为了解决这个问题 我发现我必须设置编译器标志 mno thu
  • 如何使用 requests 库发送 xml 正文?

    def request encoded xml urllib urlencode XML read xml encoded xml read xml headers Authorization AUTH TOKEN developerTok
  • 为什么应用太多参数会抛出“超出最大调用堆栈大小”?

    在 Chrome 和 Node 中 以下代码会引发错误 function noop var a new Array 1e6 Array 1000000 noop apply null a Uncaught RangeError Maximu
  • addActionListener 有什么作用?

    我有以下代码 JButton button new JButton Clear button addActionListener this 据我了解 我创建了一个按钮 上面写着 清除 然后我必须将一个动作与这个按钮关联起来 如果按下按钮会发
  • Python gc.get_count() 返回的 count0、count1 和 count2 值是什么

    python gc 包的文档对 gc get count 做了这样的描述 gc get count Return the current collection counts as a tuple of count0 count1 count
  • 在 Javascript 中使用引号和 innerHTML

    我编写了一些代码来让玩家了解故事的进展 当玩家单击按钮时 他们会看到一些新文本和更多选项 到目前为止一切顺利 但是当我传递带有附加参数的函数调用时 我需要单引号和双引号 但是 如果我同时使用两者 则会破坏innerHTML 代码如下 如果需
  • 如何获取JButton默认背景颜色?

    我用这个myButton setBackground myColor 改变JButton背景颜色为我的颜色 如何找到它原来的默认背景颜色 以便我可以将其更改回来 我知道我可以在更改和使用它之前保存它的默认背景颜色 但我想知道 Java 是否
  • 如何从 Json.NET 获取密钥列表?

    我正在使用 C 和 Json NET 如果我有一个 JObject 我想要对象内的键列表 类似于object Keys 返回对象内的键 这似乎是显而易见的 但我很难找到一种方法来做到这一点 Edit 我正在遍历该对象 并且我想在遍历时吐出对
  • 通过 Net:SSH 出现“非绝对主页”错误

    有问题的代码 Net SSH start server name user 这返回 非绝对家 用户 实际上有一个主目录 一种建议的方法是使用 IdentityFile 的完整路径修改 ssh config 这并没有解决问题 最疯狂的部分是
  • MATLAB:强制 doc 命令打开指定的参考 HTML 页面

    假设我在包中编写了一个类 名为mypackage myclass 我已经为包和类编写了自己的 HTML 文档 并将其包含在 MATLAB 帮助浏览器中 如下所述MATLAB 文档 我可以通过使用帮助浏览器直接导航到该 HTML 文档来显示该
  • 如何使用 Javascript 而不是 jQuery 用 JSON 数据动态填充 html 元素?

    我有以下 JSON 数据片段 items title sample 1 author author 1 title sample 2 author author 2 如何使用此数据填充以下 html 元素 div class news st
  • 在 iOS 中以编程方式将 SQLite 数据导出到 Excel

    在我的应用程序中 我使用 sqlite 作为后端 在本地存储数据 我能够将数据插入到我的表中 但是我想要做的是 想要以编程方式将所有 sqlite 数据导入到 excel 中 而且我不想为此应用程序使用服务器 一旦生成 excel 工作表