In my application,i am using sqlite as a backend(to store data locially).
I am able to insert data into my table.but what i want to do is,want to import all my sqlite data into excel programmatically.And i don’t want to use server for this app.once the excel sheet is generate user should be able to mail that sheet.
Is this possible in iPhone:
Please help me out .
Following is my code to insert data into table:
-(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);
}
For the app I have that did this, the SQLite data was fairly large. Therefore, I used a background thread to export all the data to a CSV (comma separated value) file, which Excel can import, and then opened up a mail composer with the CSV file as an attachment. If your data is small, you might not need to use a background thread:
Here is
exportImplAnd then, I have a class that encapsulates all my SQLite data. This class is the only one that makes sqlite calls. In this class, I have a method for exporting data into a CSV file in my app’s caches directory. The variable
sqliteDbin the code above is an instance of this class. Here’s the method to export data:My code is exporting a database of location information. Obviously, inside
exportCsv, you will need to replace my sqlite calls with ones that are appropriate for your database content.Also, the code stores the data in a temporary file. You’ll probably want to decide when to clean out those temp files.
Obviously, this code was written before ARC was available. Adjust as needed.