why below error is coming
ajithamp opened this issue · 11 comments
i am using react native android and my code is
componentDidMount() {
var sql = 'INSERT INTO todo(name, completed) VALUES (?, ?)'
var params = ["Create and delete react native android sqlite", 1]
sqlite.exec(sql, params, this.rowCallback, this.completeCallback)
.then((_) => {
alert('row inserted.')
}
)
}
Hi @ajithamp, did you sqlite.init first ?
Also check that your db structure matches the sql you're sending.
Finally, make sure that you're importing the module only once.
hi @jbrodriguez, i did sqlite.init but i dont know exactly what i have to do.If you Don't mind will you please explain clearly because i am new to react native.
And my code is
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
Alert,
View
} from 'react-native';
var sqlite = require('react-native-android-sqlite');
export default class PrepopulatedDatabaseExample extends Component {
constructor(props) {
super(props)
this.state = {
record: null
};
var databaseName = 'app.db';
sqlite.init(databaseName)
.then((_) => {
alert('database initialized.')
});
}
componentDidMount() {
var sql = 'INSERT INTO todo(name, completed) VALUES (?, ?)'
var params = ["Create and delete react native android sqlite", 1]
sqlite.exec(sql, params)
.then((_) => {
alert('row inserted.')
} )
}
success() {
var sql = 'SELECT * FROM todo WHERE completed = ?';
var params = [1];
sqlite.query(sql, params)
.then((data) => {
alert('retrieved: ', data)
} )
}
onclick() {
sqlite.close()
.then(() => {
alert('database closed')
}
)
}
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome} >
This is an example with sqlite3 and a prepopulated database. Enjoy!
</Text>
<Text onPress={this.success}>retiveData</Text>
<Text onPress={this.onclick} >closeDatabase</Text>
<Text style={styles.instructions} >
{this.state.record !== null ? 'Success: ' + this.state.record.name : 'Ajith'}
</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
AppRegistry.registerComponent('PrepopulatedDatabaseExample', () => PrepopulatedDatabaseExample);
@ajithamp take a look at https://github.com/jbrodriguez/rn-sqlite.
It's the successor to react-native-android-sqlite and it works with the recent React Native versions.
react-native-android-sqlite works as well, but instructions to install it have changed, due to changes in RN.
In any case, in your example,
componentDidMount() {
success() {
var sql = 'SELECT * FROM todo WHERE completed = ?';
var params = [1];
sqlite.query(sql, params)
.then((data) => {
alert('retrieved: ', data)
} )
}
you should replace the alert with actually setting the this.state.record, based on the logic you're using, something like
success() {
var sql = 'SELECT * FROM todo WHERE completed = ?';
var params = [1];
sqlite.query(sql, params)
.then((data) => {
this.setState({record: data})
} )
}
Also, you need to make sure that the app.db has a table todo with columns name and completed.
errorMessage = The callback ${method}() exists in module ${module}, + 'but only one callback may be registered to a function in a native module.';
from here the error is coming.
and DBmanager code is
@ReactMethod
public void query(final String sql, final ReadableArray values, final Callback callback) {
new GuardedAsyncTask<Void, Void>(getReactApplicationContext()) {
@Override
protected void doInBackgroundGuarded(Void ...params) {
WritableArray data = Arguments.createArray();
// FLog.w(ReactConstants.TAG, "dbmanager.query.sql=%s", sql);
// FLog.w(ReactConstants.TAG, "dbmanager.query.values.size()=%d", values.size());
try {
data = mDb.query(sql, values);
} catch(Exception e) {
FLog.w(ReactConstants.TAG, "Exception in database query: ", e);
callback.invoke(ErrorUtil.getError(null, e.getMessage()), null);
}
callback.invoke(null, data);
}
}.execute();
}
I had that error once , check this issue facebook/react-native#7522 ... it was because I was invoking the callback twice ... not sure what your particular scenario is
It looks like DBManager isn't defined in Java land.
Did you set it up as explained in the README ?
If you're on ReactNative 0.29+, then the instructions have changed (look at manual instructions at https://github.com/jbrodriguez/rn-sqlite)
https://github.com/jbrodriguez/rn-sqlite and https://github.com/jbrodriguez/react-native-android-sqlite
i followed steps from both these urls and still it remains the same error.I can't resolve the error any solution there
You need to follow one set of steps, depending on which RN version you're using.
It certainly looks like a setup issue
What's your output for $ adb logcat | grep 'SQLiteAssetHelper' ?
There should be some lines there.
$ react-native run-android should have some lines beginning with :react-native-android-sqlite. Is that the case ?
hi @jbrodriguez thanks, i fixed this issue.One more help i want to know how to throw error.
When invoking the callback twice it throws error implicitly , i want to throw explicitly via alert or console how to do it any idea.
And one more think how to use sqlite delete query.
@ajithamp You should use the library as a Promise rather than via callbacks.
sqlite.exec(sql, params)
.then((_) => {
console.log('row inserted.')
})
.catch( error => {
console.error("something went wrong: " + error)
})
)As for delete, just write the appropriate sql string
const sql = 'DELETE FROM todo WHERE completed = 1;'
then send that via sqlite.exec



