sqlite: sqllite.ImportFromJson not working
Describe the bug When importing json it doens’t create the route table. When we try to collect things it give the error that the routes table doesn’t excist
To Reproduce Steps to reproduce the behavior:
Use this json object:
const JSONSchema = { database: 'storage', version: 1, encrypted: false, mode: 'full', tables: [ { name: 'routes', schema: [ { column: 'id', value: 'TEXT NOT NULL'}, { column: 'name', value: 'TEXT NOT NULL' }, { column: 'date', value: 'TEXT NOT NULL' }, { constraint: 'routes_pk', value: 'PRIMARY KEY (id)'}, ], indexes: [ { name: 'routes_id_uindex', value: 'id' }, { name: 'routes_id_uindex', value: 'name' }, ], }, { name: 'route_points', schema: [ { column: 'id', value: 'INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL' }, { column: 'route_id', value: 'TEXT NOT NULL' }, { column: 'accuracy', value: 'REAL' }, { column: 'altitude', value: 'REAL' }, { column: 'altitudeAccuracy', value: 'REAL' }, { column: 'heading', value: 'REAL' }, { column: 'latitude', value: 'REAL' }, { column: 'longitude', value: 'REAL' }, { column: 'speed', value: 'REAL' }, { column: 'TIMESTAMP', value: 'INTEGER' }, { foreignkey: 'route_id', value: 'REFERENCES routes ON DELETE CASCADE', }, ], indexes: [ { name: 'route_points_id_uindex', value: 'id' }, ], }, ], };
and import it like this:
const tableQuery = SELECT name FROM sqlite_master WHERE type=‘table’ AND name=‘routes’; const tableResult = await db.query(tableQuery); if (tableResult.values.length === 0) { await console.log('table bestaat niet') const result = await sqlite.isJsonValid(JSON.stringify(JSONSchema)); await console.log(result) if(!result.result) { throw new Error(isJsonValid: “schemaToImport179” is not valid); } const resJson = await sqlite.importFromJson(JSON.stringify(JSONSchema)); await console.log(resJson) if(resJson.changes && resJson.changes.changes && resJson.changes.changes < 0) { throw new Error(importFromJson: “full” failed); }
Expected behavior Create the routes table
Screenshots If applicable, add screenshots to help explain your problem.
Desktop (please complete the following information):
- OS: Windows
- Browser: Chrome
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 21
@DanielvdSpoel if found the solution in fact the importFromJson requires to not have a connection open and close the connection after the import so the code should be
the database is stored as an Uint8Array in a localforage key/value pair database.
Voilà, it is now working fine. the closeConnection close also the db so you do not need to have db.close and sqliteCloseConnection. as sometimes in RouteModule you close the db without closing the connection you may change your connection.js as follows:
and then remove all the db.open() in main.js and RouteModule.js.
an other thing is about the db.close in RouteModule.js i do not think it is required if you have modified the getConnection as above. The only reason for it is for the web version as it will save the data to store which i think is a good idea. so better to use for the web the sqlite.saveToStore(database) for the web only so the RouteModules should be modified as follows:
like this it is working fine on web on android i got an error with the timestamp of undefined but i test on the simulator Hope with this you will make some progress and find the solution for Android keep me in the loop