I am trying to fetch data from Sqlite database using a sqlite plug-in in phonegap but I seem to be missing something. It doesn't report error when I checked the logcat but instead the database was opened successfully but my code won't fetch the data.
Below is my code so far...
function ViewData(id){
var db = window.sqlitePlugin.openDatabase("mydb", "1.0", "mydb", 100000);
db.transaction(function(transaction) {
transaction.executeSql('SELECT * FROM mytable WHERE userid=?', [id],
function(transaction, result) {
var row = result.rows.length;
if(row>0){
//fetch saved data
alert(result.rows.item(0)['lname']);
}
},errorHandler);
},errorHandler,nullHandler);
return ;
}
Note:
1. The error handler, success handler and null handler are well coded.
Please, any help is appreciated.
Sorry guys, i was calling the function before onDeviceReady
Related
In my project, I am trying to save data into a sqllite database and trying to read those data once its saved. I am able to write all the data in the database but while trying to retrieve the data then I am getting unable to start activity: Kotlin.kotlinNullPointerException error. I followed all the recommendation by android suggestion but still getting this error. Any help is appreciated.
Language Used
Kotlin
Error
Unable to start activity ComponentInfo: kotlin.KotlinNullPointerException
Code
noteList = dbHandler!!.readNotes()
Read Code
fun readNotes(): ArrayList<Notes>
{
var db=readableDatabase
val list:ArrayList<Notes> = ArrayList()
var selectAll = "SELECT * FROM " + TABLE_NAME
var cursor: Cursor = db.rawQuery(selectAll, null)
if(cursor.moveToFirst())
{
do {
var note = Notes()
note.id=cursor.getInt(cursor.getColumnIndex(KEY_ID))
note.noteName = cursor.getString(cursor.getColumnIndex(KEY_NOTE_NAME))
list.add(note)
}
while (cursor.moveToNext())
}
return list
}
Steps Checked
1) Activity is presented in Android Manifest File
2) Data's are stored in the database
3) Text Views castings are done properly
The dbHandler might be null. using !! asserts that the object is not null. Check if you have initialised the dbHandler or post the whole code
I have created the database in flutter using sqflite plugin. I created android home screen widgets for my app in android native. Now I need to fetch the data from SQLite. That is I am trying to fetch the data using Android Native sqflite Package from the SQLite db. Is that possible?
> There is No Limit on Flutter
Basic Rules of Sqlite, you need to remember
sqflite is works on Top of SQLite Database
so , when ever flutter plugin creates the database ,it was availible for native code (verify folder structure)
You can OpenDatabase by giving Database Path to SQLiteDatabase on Native Android or IOS
finally you can Read table data by query()
private fun accessDB(){
try {
GlobalScope.launch {
var db:SQLiteDatabase = SQLiteDatabase.openDatabase(getDatabasePath("epistle.db").absolutePath,null,SQLiteDatabase.OPEN_READWRITE);
Log.d(MainActivity::class.java.name , "Database Path "+db.path.toString())
Log.d("" , "Database version "+db.version.toString())
var cursor = db.query("userInfo",null,null,null,null,null,null)
cursor?.let {
while (cursor.moveToNext()){
var columnNames =cursor.columnNames
columnNames.forEach {
Log.d(
MainActivity::class.java.name,
"$it "+cursor.getString(cursor.getColumnIndex(it))
)
}
}
}
}
}catch (e:Exception){
e.printStackTrace();
}
}
MainActivity: Database Path /data/u
MainActivity: id 1
MainActivity: uid XfXCx613bKWl0EuDU
MainActivity: username Lee And May
MainActivity: email yepi1#yep.com
MainActivity: location US
MainActivity: isBiometricEnabled 0
MainActivity: isNotificationsEnable
MainActivity: morning 152.549
MainActivity: evening 166.529
MainActivity: plan Basic
MainActivity: lattitude 0
MainActivity: lognitude 0
MainActivity: userID XfXCx613bKWl0E
MainActivity: theme 2
MainActivity: premiumStartDate null
MainActivity: premiumEndDate null
MainActivity: isPremium 0
I'm using Twitter4J with the Twitter API to retrieve some Tweets (also called statuses) from Twitter.
I need to temporarily store these Statuses to a SQL database. The way that I do this is by converting the Status to a JSON String
statusItem.setmStatus(TwitterObjectFactory.getRawJSON(status));
Where status is a Status object, part of Twitter4J's library and statusItem is my own custom item.
However, when I save this statusItem to my database:
public void createStatusItem(StatusItem mStatus) {
Log.d("StatusDBHelper","createStatusItem");
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(StatusItem.KEY_status, mStatus.mStatus);
values.put(StatusItem.KEY_date, mStatus.mDate);
values.put(StatusItem.KEY_user, mStatus.mUser);
db.insert(StatusItem.TABLE, null, values);
db.close();
}
I try to retrieve the StatusItem later like so:
status = TwitterObjectFactory.createStatus(statusItem.getmStatus());
However, statusItem.getmStatus is always null!! When I try to retrieve it from the database, it's too large so it doesn't get save into StatusItem's mStatus field!
How do I overcome such an issue in storing Statuses from Twitter into my SQL database for Android??
It turns out, the null issue was occuring much sooner than the point at which I was saving my status to my statusitem.
For some reason:
TwitterObjectFactory.getRawJSON(status);
returns null no materr what. As a result, I turned to Google's GSON parser and converted it to a JSON object that way.
Clearly, TwitterObjectFactory has some issues.
I am going to save records to Parse database.
Saving to Parse:
ParseObject database = null;
database = new ParseObject("Record_db");
database.put("period_ref", current1_draw_ref_I);
database.put("remark", "na");
database.put("publishing", "publishing");
database.saveInBackground();
Cloud Code:
Parse.Cloud.beforeSave("check_duplicate", function(request, response)
{
var DB = Parse.Object.extend("Record_db");
var query = new Parse.Query(DB);
query.equalTo("period_ref", request.object.get("period_ref"));
query.first
({
success: function(object)
{
if (object)
{
response.error("A Period with this ref already exists.");
}
else
{
response.success();
}
},
error: function(error)
{
response.error("Could not validate uniqueness for this period ref object.");
}
});
});
Question:
The records can be saved to the Record_db database. But I do not know how to connect and invoke the "check_duplicate" cloud function for checking duplicate beforeSave. I found there are no tutorials or documentations on such basic operations.
How such beforesave works and when should it be called???
Could you please kindly tell me how to write in the Android code to check duplicate (if duplicate then do not save, if it is new record, then save to Parse DB) and then save to Parse? This basics stuck me for a week which is so desperating...Many thanks for your help in advance.
You are very close with your implementation, however, the before*/after* methods require the parameter being the actual classname the code should be run for, not a random method name.
beforeSave,afterSave and beforeDelete,afterDelete, get invoked automatically by Parse once an object of the class defined in the function definition is saved.
So instead of naming the method check_duplicate, use the classname Record_db like so:
Parse.Cloud.beforeSave("Record_db", function(request, response) {
//... your code ...
});
Also please note that these methods run on every save, not just on object creation, you can use request.object.isNew() to check if the object that gets saved is new or already existed.
I am inserting a row in sqlite table in phonegap using following function. My row is successfully inserted but after that it gives me error message
function executeQuery(qry_str){
var db = getDatabase();
var response = db.transaction(function(tx){
tx.executeSql(qry_str);
}, successCB, errorCB);
}
function errorCB(){
console.log('query error');
}
function successCB(){
console.log('success');
}
Please check out the mistake I am doing.