开发者

How to write and retrieve the sqlite data into the file

Friend it just general question .

How can write and retrieve the data on to the file .And how can make this file in .csv format.

For example i am retrieve data like that

NSString开发者_运维问答 *coords = [[[NSString alloc] initWithFormat:@"%f,%f\n",longitude,latitude] autorelease];
[locations addObject:coords];

Than how can write this data on to the file in .csv format.

And than How can i retrieve this data


To write csv

NSString *coords = [[[NSString alloc] initWithFormat:@"%f,%f\n",longitude,latitude] autorelease];

NSData *csvData =  [coords dataUsingEncoding:NSUTF8StringEncoding];

NSArray *UsrDocPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *DocsDir = [UsrDocPath objectAtIndex:0];
    NSString *csvPath = [DocsDir stringByAppendingPathComponent:@"coords.csv"];

    //This is upto your logic that do you want to remove existant csv file or not
    BOOL success = [FileManager fileExistsAtPath:csvPath];
    if(success){
        [FileManager removeItemAtPath:csvPath error:&error];
    }

    [csvData writeToFile:csvPath atomically:YES];

To read from file

NSData *csvData = [NSData dataWithContentsOfFile:csvPath];
NSString *strCSV = [[NSString alloc]initWithData:csvData encoding:NSUTF8StringEncoding];

Hope it helps


There is a csv parser written by dave delong. you can use that.


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.

Then you can use CSV Parser to parse the CSV and get data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜