I'm trying to read 2nd column in last record using a cursor. First, does the code layout for query and method looks workable? If not what could be improved?
Second, I lack experience to detect the error and would appreciate suggestion to correct the code.
from Class:
SQLiteDatabase db = this.getWritableDatabase();
try {
Cursor cursor = this.getSurveyData();
if(cursor.moveToLast())
{cursor.moveToLast();
String name = cursor.getString(2);
Log.v(TAG,name);
}
cursor.close();
}catch (SQLiteException e )
{
Log.v(TAG, "Exception " + e.getMessage());
}
query:
public Cursor getSurveyData(){
SQLiteDatabase db = this.getWritableDatabase();
String[] projection = {
COLUMN_DATO1
};
// String sortOrder = "ID1" + " DESC";
return db.query(
table1,
projection,
null,null,null,null,null,null
);
}
Error from debug :
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.parkinsonaider, PID: 4092
java.lang.IllegalStateException: Couldn't read row 6, col 2 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
at android.database.CursorWindow.nativeGetString(Native Method)
at android.database.CursorWindow.getString(CursorWindow.java:438)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
at com.example.android.parkinsonaider.DatabaseHelper.insertData1(DatabaseHelper.java:153)
at com.example.android.parkinsonaider.InfoActivity$1.onClick(InfoActivity.java:91)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Disconnected from the target VM, address: 'localhost:8603', transport: 'socket'
Thanks everyone, I rewrote it a bit, works now.Next is to compare dates to see if to append or insert in database. Thats another story.
String dd1="",dbdato="";
SQLiteDatabase db = this.getWritableDatabase();
long millis1=System.currentTimeMillis();
final Date dat1=new java.sql.Date(millis1);
dd1 = dat1.toString();
Cursor cursor = db.query(table1,new String[]{COLUMN_DATO1},null,null,null,null,null);
if (cursor.moveToLast()) {
cursor.moveToLast();
dbdato = cursor.getString(cursor.getColumnIndex(COLUMN_DATO1));
}
When using Cursor get????
methods (e.g. getString(?)
), the parameter is the offset as opposed to the column order. Thus the first column is offset 0
, the 2nd column is offset 1
.
It is generally better/more flexible to not use hard coded offsets but to use getColumnIndex(column_name)
to get the index.
So from your description as wanting the 2nd column then use:-
String name = cursor.getString(1);
If, as an example, your 2nd column is named name
then using :-
String name = cursor.getString(cursor.getColumnIndex("name"));
would be less problematic and more flexible.
Your projection only contains one column, you need to add all you want to fech:
String[] projection = {
COLUMN_DATO1,
COLUMN_DATO2
};
error: selenium.common.exceptions.TimeoutException: Message:
jetpack compose not responding to keyboard input on emulator
Hexagon Grid CSS - All hexagons change size when each of them is clicked
How to render HTML pages as PNG/JPEG images with URL array using Javascript only?
Get count of items that matches a condition from mongo db in node js
“Duplicate entry” even though the column has UNIQUE constraint
From the App I should be able to add an image from my gallery to the image button, then add Food name, Description and Price and finally click uploadThe image should be uploaded to my firebase storage while Price,Description and Food name should be uploaded...
I have a RelativeLayout with an ImageView that has the following properties,
I have a multidex project with minSDK 21, using gradle 45
I am using following code to fetch and render data from the published API source but unable to set the state after successfully getting the data on the consoleFollowing is my code;