简体   繁体   中英

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM