Cordova import big files into database using transactions
up vote
5
down vote
favorite
I have a txt file of about 17 mb that I have to parse, splitting it line by line and then to add it into the database using transactions.
If the file is too large and I try to open it, the application will run out of memory, so I tried to read it in pieces and then import each piece into the database. Due to the transactions, the data entered in DB is not correct.
There is a part of the used code:
await file_reader.resolveLocalFilesystemUrl(path + file).then(async (file_entry: any) => {
await file_entry.file(async (file) => {
let reader = new FileReader();
reader.onprogress = async (reader_result: any) => {
let loaded = _.cloneDeep(reader_result.loaded);
let total = _.cloneDeep(reader_result.total);
let is_last_element: boolean = _.cloneDeep(loaded == total);
let i: number = 0;
let document_length = this.sync_parser.getReaderLength();
let event_type: number = this.sync_parser.getEventType();
content = iconv.encode(reader.result, encoding).toString();
await this.db.db.transaction(async (database: any) => {
while (document_length >= i) {
if (event_type == SyncParserIo.START_TAG) {
this.table = await this.newHeader(this.sync_parser.getName());
} else if (event_type == SyncParserIo.END_TAG) {
// this.file_content = null;
} else if (event_type == SyncParserIo.ROW) {
// here I execute basic_update_insert function
}
event_type = this.sync_parser.next(i);
i++;
}
}).then(()=>{
this.logger.info(this.TAG, "End document from transaction");
}).catch((e)=>{
//log
});
if (is_last_element) {
resolve(true);
}
};
await reader.readAsBinaryString(file);
});
}).catch((e) => {
this.logger.error("FileSystem Error", e.message);
return reject(e);
});
protected basic_update_insert(table, rows_map, where, where_bindings, database?) {
let db_query = database != null ? database : this.database;
let update_query_util: any = DbUtil.update(table, rows_map, where, where_bindings);
let insert_query_util: any = DbUtil.insert(table, rows_map);
this.import_result = null;
db_query.executeSql(update_query_util.query, update_query_util.bindings, (tx, res) => {
if (res.rowsAffected === 0) {
tx.executeSql(insert_query_util.query, insert_query_util.bindings, (tx2, insert_result) => {
if (insert_result.insertId != null) {
this.import_result = ImporterIo.RESULT_OK;
}
}, (e) => {
this.import_result = ImporterIo.ERROR_INSERT_ROW;
});
} else if (res.rowsAffected === 1) {
this.import_result = ImporterIo.RESULT_OK;
} else if (res.rowsAffected > 1) {
this.import_result = ImporterIo.RESULT_OK;
}
}, (e) => {
this.logger.error(this.TAG, `error from ${table} update`, e);
this.import_result = ImporterIo.ERROR_UPDATE_ROW;
});
}
sql typescript cordova ionic-framework transactions
New contributor
add a comment |
up vote
5
down vote
favorite
I have a txt file of about 17 mb that I have to parse, splitting it line by line and then to add it into the database using transactions.
If the file is too large and I try to open it, the application will run out of memory, so I tried to read it in pieces and then import each piece into the database. Due to the transactions, the data entered in DB is not correct.
There is a part of the used code:
await file_reader.resolveLocalFilesystemUrl(path + file).then(async (file_entry: any) => {
await file_entry.file(async (file) => {
let reader = new FileReader();
reader.onprogress = async (reader_result: any) => {
let loaded = _.cloneDeep(reader_result.loaded);
let total = _.cloneDeep(reader_result.total);
let is_last_element: boolean = _.cloneDeep(loaded == total);
let i: number = 0;
let document_length = this.sync_parser.getReaderLength();
let event_type: number = this.sync_parser.getEventType();
content = iconv.encode(reader.result, encoding).toString();
await this.db.db.transaction(async (database: any) => {
while (document_length >= i) {
if (event_type == SyncParserIo.START_TAG) {
this.table = await this.newHeader(this.sync_parser.getName());
} else if (event_type == SyncParserIo.END_TAG) {
// this.file_content = null;
} else if (event_type == SyncParserIo.ROW) {
// here I execute basic_update_insert function
}
event_type = this.sync_parser.next(i);
i++;
}
}).then(()=>{
this.logger.info(this.TAG, "End document from transaction");
}).catch((e)=>{
//log
});
if (is_last_element) {
resolve(true);
}
};
await reader.readAsBinaryString(file);
});
}).catch((e) => {
this.logger.error("FileSystem Error", e.message);
return reject(e);
});
protected basic_update_insert(table, rows_map, where, where_bindings, database?) {
let db_query = database != null ? database : this.database;
let update_query_util: any = DbUtil.update(table, rows_map, where, where_bindings);
let insert_query_util: any = DbUtil.insert(table, rows_map);
this.import_result = null;
db_query.executeSql(update_query_util.query, update_query_util.bindings, (tx, res) => {
if (res.rowsAffected === 0) {
tx.executeSql(insert_query_util.query, insert_query_util.bindings, (tx2, insert_result) => {
if (insert_result.insertId != null) {
this.import_result = ImporterIo.RESULT_OK;
}
}, (e) => {
this.import_result = ImporterIo.ERROR_INSERT_ROW;
});
} else if (res.rowsAffected === 1) {
this.import_result = ImporterIo.RESULT_OK;
} else if (res.rowsAffected > 1) {
this.import_result = ImporterIo.RESULT_OK;
}
}, (e) => {
this.logger.error(this.TAG, `error from ${table} update`, e);
this.import_result = ImporterIo.ERROR_UPDATE_ROW;
});
}
sql typescript cordova ionic-framework transactions
New contributor
add a comment |
up vote
5
down vote
favorite
up vote
5
down vote
favorite
I have a txt file of about 17 mb that I have to parse, splitting it line by line and then to add it into the database using transactions.
If the file is too large and I try to open it, the application will run out of memory, so I tried to read it in pieces and then import each piece into the database. Due to the transactions, the data entered in DB is not correct.
There is a part of the used code:
await file_reader.resolveLocalFilesystemUrl(path + file).then(async (file_entry: any) => {
await file_entry.file(async (file) => {
let reader = new FileReader();
reader.onprogress = async (reader_result: any) => {
let loaded = _.cloneDeep(reader_result.loaded);
let total = _.cloneDeep(reader_result.total);
let is_last_element: boolean = _.cloneDeep(loaded == total);
let i: number = 0;
let document_length = this.sync_parser.getReaderLength();
let event_type: number = this.sync_parser.getEventType();
content = iconv.encode(reader.result, encoding).toString();
await this.db.db.transaction(async (database: any) => {
while (document_length >= i) {
if (event_type == SyncParserIo.START_TAG) {
this.table = await this.newHeader(this.sync_parser.getName());
} else if (event_type == SyncParserIo.END_TAG) {
// this.file_content = null;
} else if (event_type == SyncParserIo.ROW) {
// here I execute basic_update_insert function
}
event_type = this.sync_parser.next(i);
i++;
}
}).then(()=>{
this.logger.info(this.TAG, "End document from transaction");
}).catch((e)=>{
//log
});
if (is_last_element) {
resolve(true);
}
};
await reader.readAsBinaryString(file);
});
}).catch((e) => {
this.logger.error("FileSystem Error", e.message);
return reject(e);
});
protected basic_update_insert(table, rows_map, where, where_bindings, database?) {
let db_query = database != null ? database : this.database;
let update_query_util: any = DbUtil.update(table, rows_map, where, where_bindings);
let insert_query_util: any = DbUtil.insert(table, rows_map);
this.import_result = null;
db_query.executeSql(update_query_util.query, update_query_util.bindings, (tx, res) => {
if (res.rowsAffected === 0) {
tx.executeSql(insert_query_util.query, insert_query_util.bindings, (tx2, insert_result) => {
if (insert_result.insertId != null) {
this.import_result = ImporterIo.RESULT_OK;
}
}, (e) => {
this.import_result = ImporterIo.ERROR_INSERT_ROW;
});
} else if (res.rowsAffected === 1) {
this.import_result = ImporterIo.RESULT_OK;
} else if (res.rowsAffected > 1) {
this.import_result = ImporterIo.RESULT_OK;
}
}, (e) => {
this.logger.error(this.TAG, `error from ${table} update`, e);
this.import_result = ImporterIo.ERROR_UPDATE_ROW;
});
}
sql typescript cordova ionic-framework transactions
New contributor
I have a txt file of about 17 mb that I have to parse, splitting it line by line and then to add it into the database using transactions.
If the file is too large and I try to open it, the application will run out of memory, so I tried to read it in pieces and then import each piece into the database. Due to the transactions, the data entered in DB is not correct.
There is a part of the used code:
await file_reader.resolveLocalFilesystemUrl(path + file).then(async (file_entry: any) => {
await file_entry.file(async (file) => {
let reader = new FileReader();
reader.onprogress = async (reader_result: any) => {
let loaded = _.cloneDeep(reader_result.loaded);
let total = _.cloneDeep(reader_result.total);
let is_last_element: boolean = _.cloneDeep(loaded == total);
let i: number = 0;
let document_length = this.sync_parser.getReaderLength();
let event_type: number = this.sync_parser.getEventType();
content = iconv.encode(reader.result, encoding).toString();
await this.db.db.transaction(async (database: any) => {
while (document_length >= i) {
if (event_type == SyncParserIo.START_TAG) {
this.table = await this.newHeader(this.sync_parser.getName());
} else if (event_type == SyncParserIo.END_TAG) {
// this.file_content = null;
} else if (event_type == SyncParserIo.ROW) {
// here I execute basic_update_insert function
}
event_type = this.sync_parser.next(i);
i++;
}
}).then(()=>{
this.logger.info(this.TAG, "End document from transaction");
}).catch((e)=>{
//log
});
if (is_last_element) {
resolve(true);
}
};
await reader.readAsBinaryString(file);
});
}).catch((e) => {
this.logger.error("FileSystem Error", e.message);
return reject(e);
});
protected basic_update_insert(table, rows_map, where, where_bindings, database?) {
let db_query = database != null ? database : this.database;
let update_query_util: any = DbUtil.update(table, rows_map, where, where_bindings);
let insert_query_util: any = DbUtil.insert(table, rows_map);
this.import_result = null;
db_query.executeSql(update_query_util.query, update_query_util.bindings, (tx, res) => {
if (res.rowsAffected === 0) {
tx.executeSql(insert_query_util.query, insert_query_util.bindings, (tx2, insert_result) => {
if (insert_result.insertId != null) {
this.import_result = ImporterIo.RESULT_OK;
}
}, (e) => {
this.import_result = ImporterIo.ERROR_INSERT_ROW;
});
} else if (res.rowsAffected === 1) {
this.import_result = ImporterIo.RESULT_OK;
} else if (res.rowsAffected > 1) {
this.import_result = ImporterIo.RESULT_OK;
}
}, (e) => {
this.logger.error(this.TAG, `error from ${table} update`, e);
this.import_result = ImporterIo.ERROR_UPDATE_ROW;
});
}
sql typescript cordova ionic-framework transactions
sql typescript cordova ionic-framework transactions
New contributor
New contributor
New contributor
asked Nov 19 at 12:13
Alexandru D
262
262
New contributor
New contributor
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
3
down vote
You may find cordova-sqlite-porter to be useful (there is an Ionic Native Typescript wrapper).
It wraps the SQLite DB API, allowing you to pass it a data dump formatted either as SQL statements or as JSON.
With such a large amount of data, it may be in your interests to convert the data to the JSON structure supported by the plugin since it translates the JSON into batched inserts which, in the plugin example project, leads to an observable performance improvement when importing data of 100 times faster.
Alternatively, you may wish to rework your SQL INSERT statements manually to make use of the UNION SELECT optimisation as outlined in this answer.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
You may find cordova-sqlite-porter to be useful (there is an Ionic Native Typescript wrapper).
It wraps the SQLite DB API, allowing you to pass it a data dump formatted either as SQL statements or as JSON.
With such a large amount of data, it may be in your interests to convert the data to the JSON structure supported by the plugin since it translates the JSON into batched inserts which, in the plugin example project, leads to an observable performance improvement when importing data of 100 times faster.
Alternatively, you may wish to rework your SQL INSERT statements manually to make use of the UNION SELECT optimisation as outlined in this answer.
add a comment |
up vote
3
down vote
You may find cordova-sqlite-porter to be useful (there is an Ionic Native Typescript wrapper).
It wraps the SQLite DB API, allowing you to pass it a data dump formatted either as SQL statements or as JSON.
With such a large amount of data, it may be in your interests to convert the data to the JSON structure supported by the plugin since it translates the JSON into batched inserts which, in the plugin example project, leads to an observable performance improvement when importing data of 100 times faster.
Alternatively, you may wish to rework your SQL INSERT statements manually to make use of the UNION SELECT optimisation as outlined in this answer.
add a comment |
up vote
3
down vote
up vote
3
down vote
You may find cordova-sqlite-porter to be useful (there is an Ionic Native Typescript wrapper).
It wraps the SQLite DB API, allowing you to pass it a data dump formatted either as SQL statements or as JSON.
With such a large amount of data, it may be in your interests to convert the data to the JSON structure supported by the plugin since it translates the JSON into batched inserts which, in the plugin example project, leads to an observable performance improvement when importing data of 100 times faster.
Alternatively, you may wish to rework your SQL INSERT statements manually to make use of the UNION SELECT optimisation as outlined in this answer.
You may find cordova-sqlite-porter to be useful (there is an Ionic Native Typescript wrapper).
It wraps the SQLite DB API, allowing you to pass it a data dump formatted either as SQL statements or as JSON.
With such a large amount of data, it may be in your interests to convert the data to the JSON structure supported by the plugin since it translates the JSON into batched inserts which, in the plugin example project, leads to an observable performance improvement when importing data of 100 times faster.
Alternatively, you may wish to rework your SQL INSERT statements manually to make use of the UNION SELECT optimisation as outlined in this answer.
answered Nov 19 at 15:50
DaveAlden
19.6k95393
19.6k95393
add a comment |
add a comment |
Alexandru D is a new contributor. Be nice, and check out our Code of Conduct.
Alexandru D is a new contributor. Be nice, and check out our Code of Conduct.
Alexandru D is a new contributor. Be nice, and check out our Code of Conduct.
Alexandru D is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374415%2fcordova-import-big-files-into-database-using-transactions%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown