Comments (5)
Hi Donald
avoid doing stuff on the db open status callback - wait for db instance to be returned and then invoke this.populateDatabase(db)
try removing this.setState(this.state); or recoding this stmt to that.setState(that.state); in populateDatabase.
var that = this is not there by accident
you can either do var that = this or use a bind(this)_ on the method.
from react-native-sqlite-storage.
otherwise the sample you sent is not complete..where is getService getting called from. What is the param to openDatabase for example?
from react-native-sqlite-storage.
This code "works" i.e. I see return values from getServices...
React Native is bad in terms of eating up bad references to this it seems...
openErrorCallback(message) {
},
populateDatabase(db){
var that = this;
db.executeSql('SELECT version_id FROM Version ORDER BY version_id DESC LIMIT 1', [], function(results) {
console.log("Post version query (success)");
var len = results.rows.length;
for (let i = 0; i < len; i++) {
let row = results.rows.item(i);
database_current_version = row.version_id;
}
that.setState(that.state);
db.transaction(that.upgradeTables, function() {
console.log("upgradeTables failed");
}, function() {
console.log("upgradeTables success");
that.getServices(() => {});
});
}, function() {
console.log("Post version query (failed)");
db.transaction(that.upgradeTables, function() {
console.log("Upgrade failed");
}, function() {
console.log("Upgrade success");
});
});
},
upgradeTables(tx) {
console.log("Database running version "+database_current_version);
if(database_current_version < 1) {
tx.executeSql("CREATE TABLE IF NOT EXISTS Version ( " +
"version_id INTEGER PRIMARY KEY" +
");", []);
tx.executeSql("CREATE TABLE IF NOT EXISTS Service (" +
"id INTEGER PRIMARY KEY, " +
"name TEXT" +
");", []);
tx.executeSql("INSERT INTO Version (version_id) VALUES (?)", [1]);
}
},
addService(callback, obj) {
var that = this;
db.transaction(tx).then(() => {
callback(true);
});
},
addAccountTransaction(tx, obj) {
tx.executeSql("INSERT INTO Service (name) VALUES (?)", [obj.name]);
},
getServices(callback) {
db.executeSql("SELECT id FROM Service", [], function(results) {
console.log("Got service results "+JSON.stringify(results));
var len = results.rows.length;
console.log("len = "+len);
for (var i = 0; i < len; i++) {
let row = results.row.item(i);
console.log("row "+i+" = "+row.id);
}
callback(results);
});
},
executeQuery(query, params) {
console.log("Executing query "+query);
return db.executeSql(query, params);
},
executeQueryCallback(query, params, callbackSuccess, callbackFailure) {
db.executeSql(query, params, callbackSuccess, callbackFailure);
},
executeTransaction(func, callback) {
console.log("Executing transaction on AuthioDB");
db.transaction(func).then((results) => {
callback(true, results);
}).catch((error) => {
callback(false);
});
},
getDbResource() {
return db;
},
loadAndQueryDB(){
var that = this;
that.state.progress.push("Opening database ...");
that.setState(that.state);
//database_ready_callback = readyCallback;
db = SQLite.openDatabase(database_name, database_version, database_displayname, database_size, function() {
console.log("db opened");
}, that.openErrorCallback);
this.populateDatabase(db);
},
from react-native-sqlite-storage.
Hi Andrzej,
Thanks for taking the time to help. It appears I have misunderstood the purpose of the open callbacks. I thought that the entire nature of this module was asynchronous - ie: you can only use once the open callback has executed, which is why I was doing things in there.
I'll try it again now by just using the populate/etc methods outside of the callback - because my entire structure was basically executing in a chained callback.
Thanks again.
from react-native-sqlite-storage.
I follow the below blog to integrate SQLite storage on this blog provided ExecuteQuery function and how to use it because based on promise it handles multiple select queries.
`/**
- Execute sql queries
- @param sql
- @param params
- @returns {resolve} results
*/
ExecuteQuery = (sql, params = []) => new Promise((resolve, reject) => {
db.transaction((trans) => {
trans.executeSql(sql, params, (trans, results) => {
resolve(results);
},
(error) => {
reject(error);
});
});
});`
https://infinitbility.com/react-native-sqlite-storage-examples-of-query
from react-native-sqlite-storage.
Related Issues (20)
- OPEN database: my.db failed, aborting any pending transactions HOT 1
- Works very fast in debug, but works very slow in production
- how to debag? how to view the contents of the table in react-native-debugger? HOT 7
- How to connect cordova-sqlcipher-adapter?
- Error while opening the database on React 0.71.2 HOT 2
- Update a pre-populated sqlite database HOT 10
- Can't downgrade database from version 2 to 1
- Is this repo still maintained? HOT 3
- Error in opening the database : openDatabase() HOT 2
- Package react-native-sqlite-storage contains invalid configuration HOT 11
- install error on android
- TypeError: Cannot read properties of undefined (reading 'open') HOT 2
- can we retrive database file from iPhone?
- receiving the error "Invalid database handle" HOT 1
- Duplicate Symbol resulting in link error
- No rollback on failed transactions HOT 1
- too slow
- Slow run
- [Query] Support for new Architecture HOT 1
- db.executeSql('PRAGMA foreign_keys = ON');
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-native-sqlite-storage.