I've following table structure in my SQLite Database.
In my SqliteOpenHelper class, I wrote following method.
public Form getAllForms(){
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from "+FORM_TABLE, null);
int count = cursor.getCount();
String formID = cursor.getString(0);
Form form = new Form();
form.setId(formID);
cursor.close();
db.close();
return form;
}
I'm sure there's some data in it because I already watched count in debugging mode and I saw the quantity of row that actually existing in database. But CursorIndexOutOfBoundException shows up at cursor.getString(0). plus cursor.getInt(0) and cursor.getString(1) didn't work also. What's the problem might be?
You need to move the cursor to a valid row.
Valid rows are indexed from 0 to count-1. At first the cursor will point to row index -1 i.e. the one just before the first row.
The canonical way of looping through all rows is
if (cursor.moveToFirst()) {
do {
// now access cursor columns
} while (cursor.moveToNext());
}
Try this
try {
cursor.moveToFirst();
for (int i = 0; i < cursor.getCount(); i++) {
Form form = new Form();
form.setId(formID);
cursor.moveToNext();
}
} finally {
// database.close();
cursor.close();
dbHelper.close();
}
You need to call moveToFirst() to get to the first row before asking for values:
if ((cursor!= null) && (cursor.getCount() > 0)) {
cursor.moveToFirst();
while (cursor.isAfterLast() == false) {
}
So Simply use your code as
public Form getAllForms(){
Form form = new Form();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from "+FORM_TABLE, null);
if ((cursor != null) && (cursor.getCount() > 0)) {
cursor.moveToFirst();
while (cursor.isAfterLast() == false) {
String formID = cursor.getString(0);
form.setId(formID);
cursor.moveToNext();
}
cursor.close();
}
db.close();
return form;
}
Related
How do i do a select statement which retrieves 2 String and 1 integer value. I am receiving this error. My app keep crashing at cursor.getString(0); In my log, i do get this message Log.d("TAG","Row found");
Login.java
private void getQRCodeInformation(){
//database helper object
DatabaseHelper db;
//initializing views and objects
db = new DatabaseHelper(this);
Cursor cursor = db.getQRCodeInformation();
if(cursor.getCount() == 0) {
Log.d("TAG","Nothing found");
}
else{
Log.d("TAG","Row found");
if(cursor != null && cursor.moveToFirst()){
//cursor.getString(0);
//cursor.getString(1);
//cursor.getInt(1);
Log.d("TAG","Data found");
}
}
}
DatabaseHelper.java
public Cursor getQRCodeInformation(){
SQLiteDatabase db = this.getReadableDatabase();
String sql = "SELECT "+COLUMN_0_UserDetail+" , "+COLUMN_4_UserDetail+" , "+COLUMN_5_UserDetail+ " FROM "+ TABLE_NAME_UserDetail;
Cursor c = db.rawQuery(sql, null);
return c;
}
you just need to get index of column
int index = cursor.getColumnIndex(COLUMN_NAME);
then
String columnValue = cursor.getString(index);
Good luck :)
public String[] getAllDescription() {
db = this.getReadableDatabase();
cursor = db.query(NOTIFICATIONTABLE, new String[]{"notification_id", "notification_title",
"notification_description", "notification_isread", "date"}, null, null, null,
null, null);
String[] result = new String[cursor.getCount()];
int i = 0;
if (cursor.moveToFirst()) {
do {
result[i] = cursor.getString(cursor.getColumnIndex("notification_description"));
i++;
} while (cursor.moveToNext());
}
if ((cursor != null) && !cursor.isClosed()) {
cursor.close();
db.close();
}
return result;
}
this is my code currently i am getting from 0 to n data in staring array i want to get in reverse array data from n to 0 so that i can Print please suggest me how i will get data
Try this method :
public ArrayList<String> getAllDescription()
{
ArrayList<String> array_list = new ArrayListString>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("SELECT * " + " FROM " + NOTIFICATIONTABLE+ " DESC;", null);
res.moveToFirst();
while (res.isAfterLast() == false)
{
array_list.add(hashmap);
res.moveToNext();
}
return array_list;
}
Or If you want to reverse arrayList then you can also do it By just a single line of code .
Collections.reverse("yourArrayList");
Hope it will help you.
i am getting from 0 to n data in staring array i want to get in
reverse array data from n to 0
Why not using ASC or DESC in db query to get result in sequence as want according to notification_description column.
do it as:
cursor = db.query(NOTIFICATIONTABLE,
new String[]{"notification_id",
"notification_title",
"notification_description",
"notification_isread",
"date"}, null, null, null,
null, "notification_description DESC");
You can insert the data from end of theString[]...
public String[] getAllDescription() {
db = this.getReadableDatabase();
cursor = db.query(NOTIFICATIONTABLE, new String[]{"notification_id", "notification_title",
"notification_description", "notification_isread", "date"}, null, null, null,
null, null);
String[] result = new String[cursor.getCount()];
int i = 0;
if (cursor.moveToFirst()) {
do {
///////////////////////
result[(cursor.getCount()-1)-i] = cursor.getString(cursor.getColumnIndex("notification_description"));
///////////////////////
i++;
} while (cursor.moveToNext());
}
if ((cursor != null) && !cursor.isClosed()) {
cursor.close();
db.close();
}
return result;
}
public String[] getNewsLink(String prodcuttye) {
Cursor cursor = mDb.query(SQLITE_TABLE, new String[] { "productname" }, "ProdctType='"
+ prodcuttye + "'", null, null, null, null, null);
String[] result = new String[cursor.getCount()];
int i = 0;
if (cursor.moveToFirst()) {
do {
result[i] = cursor.getString(cursor.getColumnIndex("productname"));
i++;
} while (cursor.moveToNext());
}
if ((cursor != null) && !cursor.isClosed()) {
cursor.close();
mDb.close();
}
return result;
}
This function for fetch data in String array i am trying to excute this Query am getting data when i am call the in Sqlite browser[ select productname from MyShopingTable where ProdctType= 'Basmati Rice'] But when i try to run this Query in function i am getting cursor count 0 so i am unable to get data please tell me where am doing wrong please suggest me
Try changing your query like following :
Cursor c = mDb.query("SELECT * FROM MyShopingTable where ProdctType = ?",new String[]{prodcuttye})
See if that works in your case...
I have table subject, when I try the query in adb shell the output is OK
SELECT * FROM subject;
The output is 10 rows...
When I do the same in Java/Android:
public ArrayList<Subject> getSubjectList() {
SQLiteDatabase db = DatabaseManager.getInstance().openDatabase();
ArrayList<Subject> subjects = new ArrayList<Subject>();
String selectQuery = "SELECT * FROM subject";
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
while (cursor.moveToNext()) {
Subject subject = new Subject();
subject.setId_subject(Integer.parseInt(cursor.getString(0)));
subject.setName(cursor.getString(1));
subjects.add(subject);
}
}
cursor.close();
DatabaseManager.getInstance().closeDatabase();
return subjects;
}
The output is 9 rows... the first row with id 1 will not display, why?
Because you move to the next (moveToNext()) row immediately after moving on the first one (moveToFirst())...
What i normally do is something like this:
if (cursor != null)
{
if (cursor.moveToFirst())
{
do
{
// ...
}
while (cursor.moveToNext());
}
}
Because on entering the if statement, you moveToFirst(), then on the first entry to the while loop, you .moveToNext(). Meaning you skip the first record.
So use a do... while() construct instead:
if (cursor.moveToFirst()) {
do {
Subject subject = new Subject();
subject.setId_subject(Integer.parseInt(cursor.getString(0)));
subject.setName(cursor.getString(1));
subjects.add(subject);
}while (cursor.moveToNext())
}
Reference: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html
The code to returns all the data in the table in list.But this isn't working.
I've called this method from CheckData class which is called by main class
public List<String[]> selectAll() {
List<String[]> list = new ArrayList<String[]>();
Cursor cursor = db
.query(TABLE_NAME, null, null, null, null, null, null);
int x = 0;
if (cursor.moveToFirst()) {
do {
String[] b1 = new String[] { cursor.getString(1),
cursor.getString(2) };
list.add(b1);
x = x + 1;
} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
cursor.close();
return list;
}
My database contains 3 columns - id(integer primary key), symbol(text) and company_name(text).
My Data Base name is AppDB and table name is scrip.
Here are the good tutorial to LEARN use of Android-SQLite.
so,I also advise you the same As I answered here