如何在iPhone SDK中将SQLite文件导出为CSV文件

发布于 2024-10-11 03:08:29 字数 70 浏览 3 评论 0原文

在我的应用程序中,我想将 SQLite 数据库文件导出为 CSV 文件。

您能建议我如何执行此操作吗? 谢谢。

In my app, I want to export a SQLite database file to CSV file..

Could you suggest me how to do this?
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小帐篷 2024-10-18 03:08:29

首先,您需要确保使用 FMDB 访问数据库,因为直接在 Objective-C 中使用 SQLite C API 都是受虐狂。您可以这样做:

FMDatabase *db = [[FMDatabase alloc] initWithPath:@"/path/to/db/file"];
FMResultSet *results = [db executeQuery:@"SELECT * FROM tableName"];
while([results nextRow]) {
  NSDictionary *resultRow = [results resultDict];
  NSArray *orderedKeys = [[resultRow allKeys] sortedArrayUsingSelector:@selector(compare:)];
  //iterate over the dictionary
}

至于写入 CSV 文件,也有相应的代码

#import "CHCSV.h"

CHCSVWriter * csvWriter = [[CHCSVWriter alloc] initWithCSVFile:@"/path/to/csv/file" atomic:NO];

//write stuff
[csvWriter closeFile];
[csvWriter release];

:要组合它们,您需要执行以下操作:

FMDatabase *db = [[FMDatabase alloc] initWithPath:@"/path/to/db/file"];
if (![db open]) {
  //couldn't open the database
  [db release];
  return nil;
}
FMResultSet *results = [db executeQuery:@"SELECT * FROM tableName"];
CHCSVWriter *csvWriter = [[CHCSVWriter alloc] initWithCSVFile:@"/path/to/csv/file" atomic:NO];
while([results nextRow]) {
  NSDictionary *resultRow = [results resultDict];
  NSArray *orderedKeys = [[resultRow allKeys] sortedArrayUsingSelector:@selector(compare:)];
  //iterate over the dictionary
  for (NSString *columnName in orderedKeys) {
    id value = [resultRow objectForKey:columnName];
    [csvWriter writeField:value];
  }
  [csvWriter writeLine];
}
[csvWriter closeFile];
[csvWriter release];

[db close];
[db release];

这会将 tableName 表的内容写入 CSV 文件。

First, you'll want to make sure that you're using FMDB to access the database, because people who use the SQLite C API directly in Objective-C are masochists. You can do that like this:

FMDatabase *db = [[FMDatabase alloc] initWithPath:@"/path/to/db/file"];
FMResultSet *results = [db executeQuery:@"SELECT * FROM tableName"];
while([results nextRow]) {
  NSDictionary *resultRow = [results resultDict];
  NSArray *orderedKeys = [[resultRow allKeys] sortedArrayUsingSelector:@selector(compare:)];
  //iterate over the dictionary
}

As for writing to a CSV file, well there's code for that too:

#import "CHCSV.h"

CHCSVWriter * csvWriter = [[CHCSVWriter alloc] initWithCSVFile:@"/path/to/csv/file" atomic:NO];

//write stuff
[csvWriter closeFile];
[csvWriter release];

And to combine them, you'd do:

FMDatabase *db = [[FMDatabase alloc] initWithPath:@"/path/to/db/file"];
if (![db open]) {
  //couldn't open the database
  [db release];
  return nil;
}
FMResultSet *results = [db executeQuery:@"SELECT * FROM tableName"];
CHCSVWriter *csvWriter = [[CHCSVWriter alloc] initWithCSVFile:@"/path/to/csv/file" atomic:NO];
while([results nextRow]) {
  NSDictionary *resultRow = [results resultDict];
  NSArray *orderedKeys = [[resultRow allKeys] sortedArrayUsingSelector:@selector(compare:)];
  //iterate over the dictionary
  for (NSString *columnName in orderedKeys) {
    id value = [resultRow objectForKey:columnName];
    [csvWriter writeField:value];
  }
  [csvWriter writeLine];
}
[csvWriter closeFile];
[csvWriter release];

[db close];
[db release];

That will write the contents of the tableName table out to a CSV file.

恋你朝朝暮暮 2024-10-18 03:08:29

只需对每个表执行 SELECT 操作,然后根据需要将每列的值打印到文本文件中。

Simply do a SELECT on each table and print out the value of each column as required to a text file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文