Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store huge data in SQLite [duplicate]

I have to add the huge data in the SQLite database and need some suggestion how I can do that functionality in iOS. I have to sync around 1 GB of data from server to the iPhone SQLite database.

I need alternative ways to store fast data in the database on the iOS side. I have tried to store data one by one, but it's taking too much time to synchronise and store data.

like image 889
DeveshM Avatar asked Jun 28 '13 12:06

DeveshM


People also ask

Is SQLite good for large datasets?

Very large datasets And even if it could handle larger databases, SQLite stores the entire database in a single disk file and many filesystems limit the maximum size of files to something less than this.

Can SQLite store datasets larger than 1 petabyte?

There is no 2 GB limit. SQLite database files have a maximum size of about 140 TB. On a phone, the size of the storage (a few GB) will limit your database file size, while the memory size will limit how much data you can retrieve from a query. Furthermore, Android cursors have a limit of 1 MB for the results.

Does SQLite support double?

developer.android.com/reference/java/lang/… @Doomsknight: yes, but long fits in the sqlite type INTEGER , which can be up to 8 bytes.

Is SQLite faster than CSV?

Unless you're doing something very trivial to the CSV, and only doing it once, SQLite will be faster for runtime, coding time, and maintenance time, and it will be more flexible.


1 Answers

I would suggest you read this Stack Overflow question:

How do I improve the performance of SQLite?

It's a very thourough look at how to improve the performance of SQLite in general, and it was very helpful when I was hitting speed problems trying to insert 100,000 records into an SQLite database on iOS.

In specific, the use of Transactions dramatically cut down on the overall insert speed. Here is a short block of sample code so you can see what I mean:

const char *dbpath = [[Utilities pathInDocumentsFolder: MY_DATABASE] UTF8String];
const char *sql = "INSERT INTO Filters (Region, District, Territory) " \
    "VALUES (?, ?, ?)";
sqlite3 *mapDB;
char *sqliteError;

sqlite3_stmt *insertStatement;

sqlite3_open(dbpath, &mapDB);

sqlite3_exec(mapDB, "BEGIN TRANSACTION", NULL, NULL, &sqliteError);

if (sqlite3_prepare_v2(mapDB, sql, -1, &insertStatement, NULL) == SQLITE_OK) {
    for (NSArray *row in filtersArray) {
        sqlite3_bind_text(insertStatement, 1, [[row objectAtIndex: 0] UTF8String], -1, SQLITE_TRANSIENT);  // Region
        sqlite3_bind_text(insertStatement, 2, [[row objectAtIndex: 1] UTF8String], -1, SQLITE_TRANSIENT);  // District
        sqlite3_bind_text(insertStatement, 3, [[row objectAtIndex: 2] UTF8String], -1, SQLITE_TRANSIENT);  // Territory

        if (sqlite3_step(insertStatement) != SQLITE_DONE) {
            break;
        }

        sqlite3_clear_bindings(insertStatement);
        sqlite3_reset(insertStatement);
    }
}

sqlite3_exec(mapDB, "END TRANSACTION", NULL, NULL, &sqliteError);

sqlite3_finalize(insertStatement);

The sqlite3_exec with the BEGIN and END TRANSACTION statements are the magic.

like image 178
Axeva Avatar answered Sep 26 '22 09:09

Axeva