I use one in my sqlite database a config table. This has the following composition:
private static final String DATABASE_CONFIG_CREATE =
"CREATE TABLE " + TABLE_CONFIGS
+ "("
+ CONFIG_HIDDEN_CATEGORIES + " TEXT DEFAULT '1,2' NULL"
+ ");";
Later I try to access with the following code:
Cursor cursor = database.query(DatabaseHelper.TABLE_CONFIGS, new String[] {DatabaseHelper.CONFIG_HIDDEN_CATEGORIES}, null, null, null, null, null);
try {
cursor.moveToFirst();
int test = cursor.getCount();
String data = cursor.getString(0);
}
catch (Exception exception) {
But the line cursor.getString(0) throws the following exeption (variable test is 0):
android.database.CursorIndexOutOfBoundsException: Index 0 requested,
with a size of 0
when I run the following code, the column CONFIG_HIDDEN_CATEGORIES is displayed to me... what is wrong?
Cursor dbCursor = database.query(DatabaseHelper.TABLE_CONFIGS, null, null, null, null, null, null);
String[] columnNames = dbCursor.getColumnNames(); // index 0 is the value of CONFIG_HIDDEN_CATEGORIES
It means that your Cursor is empty. You should wrap your actions to correct condition:
try {
if (cursor.moveToFirst()) {
String data = cursor.getString(cursor.getColumnIndex("columnName"));
// do your stuff
}
else {
// cursor is empty
}
}
...
Your actual code won't work correct because you just calling moveToFirst() method but you don't know (are not testing) if it'll return true or false.
Your code works correct only if Cursor is not empty. In second case it won't work.
Note: I recommend you to use getColumnIndex(<columnName>) method for getting column index due to its name. This method is safer and more human-readable.
Related
I am using the following to fetch a string from my table. The cursor is always returning empty even when I have data in database. Is the query wrong?
public void find(String myNumber){
String[] thecolumns = new String[]{
ID,
FLAG};
cursor = sqlDb.query(MY_TABLE,
thecolumns, NUMBER + "='"
+ myNumber+ "'", null, null, null, null);
if (cursorToFetchAssets != null ) {
cursorToFetchAssets.moveToFirst();{
try{
//code to fetch
}catch{
//return when there are no rows found.
}
}
EDIT: NUMBER is of type string "...+ NUMBER + " TEXT,.. " and myNumber is also a string
FIXED: Issue was on the server side of my code. Not over here..
try this:
cursor = sqlDb.query(
MY_TABLE,
thecolumns,
NUMBER + "=?",
new String[]{String.valueOf(number)},
null, null, null);
I am just trying to search for the data in multiple table.If the where condition data is not present in first table(tab1) then it has to search in the second table(tab2) but I am getting the exception showing that
Cursor Index Out of Bounds Exception: Index -1 requested with size 0
Here is my code
SQLiteDatabase db=openOrCreateDatabase("train",SQLiteDatabase.CREATE_IF_NECESSARY, null);
Cursor c1;
String[] table={"tab1","tab2","tab3","tab4"};
int i=0;
do {
c1 = db.rawQuery("select * from '"+table[i]+"' where name='Triplicane'", null);
i++;
} while(c1 == null);
int id1=c1.getInt(0);
String nam1=c1.getString(1);
Toast.makeText(fare.this,"ID no:"+id1, Toast.LENGTH_LONG).show();
Toast.makeText(fare.this,"name"+nam1, Toast.LENGTH_LONG).show();
So from the beginning. Implicitly, each Cursor is positioned before first row so if you want to work with it you need to call
cursor.moveToFirst()
that moves Cursor to first row if is not empty and then is ready for work. If Cursor is empty simply it returns false. So how i mentioned now this method is very handy indicator whether your Cursor is valid or not.
And as my recommendation i suggest you to change your code because i think is broken and it sounds like "spaghetti code"
Cursor c = null;
String[] tables = {"tab1", "tab2", "tab3", "tab4"};
for (String table: tables) {
String query = "select * from '" + table + "' where name = 'Triplicane'";
c = db.rawQuery(query, null);
if (c != null) {
if (c.moveToFirst()) { // if Cursor is not empty
int id = c.getInt(0);
String name = c.getString(1);
Toast.makeText(fare.this, "ID: " + id, Toast.LENGTH_LONG).show();
Toast.makeText(fare.this, "Name: " + name, Toast.LENGTH_LONG).show();
}
else {
// Cursor is empty
}
}
else {
// Cursor is null
}
}
Notes:
Now i want to tell you some suggestions:
An usage of parametrized statements is very good practise so in a
future if you will work with statements, use placeholders in them. Then your statements becomes more human-readable, safer(SQL Injection) and faster.
It's also a very good practise to create static final fields that will hold
your column names, table names etc. and to use
getColumnIndex(<columnName>) method to avoid "typo errors" which are looking for very bad.
Your Cursor flag to empty row , On Sqlite cursor pointed to row number -1 ,
then if you use c.moveNext() or c.moveToFirst() you'll be able to read rows "row by row "
write cursor.movetoFirst() before getting data from cursor.
I am trying to search for information in one table (DATABASE_FAV_EQUIP_TABLE) based on a value, pull that information, and then place it into another table (DATABASE_EQUIP_TABLE) - checking first if it is not already there. My code is below. Nothing is crashing, and I am not getting any errors, however no information gets placed into the second table (DATABASE_EQUIP_TABLE). I have gone over it several times and cannot figure out why this isn't working.
//takes all equip data from saved equip set, and puts into equip table
public String AddSavedEquipToEquipTable(String name) {
String[] columns = new String[]{KEY_EQUIP};
Cursor c = ourDatabase.query(DATABASE_FAV_EQUIP_TABLE, columns, KEY_FAVNAME + "='" + name + "'", null, null, null, null);
if (c == null){
return null;
}
else if(c != null && c.moveToFirst()) {
do {
int iEquip = c.getColumnIndex(KEY_EQUIP);
String equip = c.getString(iEquip);
CheckEquipTable(equip);
c.moveToNext();
} while (KEY_FAVNAME == name);
}
return null;
}
public void CheckEquipTable(String equip){
String[] columns = new String[]{KEY_EQUIP};
ContentValues cv = new ContentValues();
Cursor check = ourDatabase.query(DATABASE_EQUIP_TABLE, columns, KEY_EQUIP + "='" + equip + "'", null, null, null, null);
if(check == null){
cv.put(KEY_EQUIP, equip);
ourDatabase.insert(DATABASE_EQUIP_TABLE, null, cv);
}
}
Try this:
public void AddSavedEquipToEquipTable(String name) {
String[] columns = new String[]{KEY_EQUIP};
Cursor c = ourDatabase.query(DATABASE_FAV_EQUIP_TABLE, columns, KEY_FAVNAME + "='" + name + "'", null, null, null, null);
int iEquip = c.getColumnIndex(KEY_EQUIP);
while(c.moveToNext())
CheckEquipTable(c.getString(iEquip));
}
I'll explain a few changes:
The column index of KEY_EQUIP will not change so we only need to retrieve the index once
In Java you must compare Strings with if(name.equals(KEY_FAVNAME)) not ==, (KEY_FAVNAME == name) is probably false.
ourDatabase.query() will never return null, the smallest result will be an empty Cursor (as in cursor.getCount() == 0)
cursor.moveToNext() returns true / false, and when the index is at -1 moveToNext() is the same as moveToFirst(). Also if the Cursor is empty then moveToNext() returns false.
Don't hesitate with any other questions with my suggestion. Hope that helps!
Addition
I didn't notice CheckEquipTable() was checking if a Cursor is null the first time:
if(check == null){
Change it like so:
if(check.getCount() == 0){
Um, you should be able to do this solely in sqlite. I can help more if I had the table names / columns, but you would basically do a query like this.
INSERT INTO SomeTable(C, D)
SELECT A, B
FROM OtherTable
WHERE NOT EXISTS (
SELECT 1
FROM SomeTable
WHERE A = C AND B = D
)
I have the following function for SQLite select statement
public Boolean itemFound(String cartId,Long ItemId){
SQLiteDatabase db = this.getReadableDatabase();
Log.d("search",cartId+"--"+ItemId);
try {
String where = COL_ITEM_CART_ID+"='"+cartId+"' and "+COL_ITEM_ID+" ="+ ItemId+"";
Log.d("where===============", where);
String columns[] = new String[] { COL_ITEM_ID,COL_ITEM_NAME, COL_ITEM_PRICE, COL_ITEM_ADDDATE,COL_ITEM_QUANTITY,COL_ITEM_CART_ID }; // > null means * (all)
Cursor c = db.query(TABLE_NAME,
columns ,
COL_ITEM_CART_ID+"='?' and "+ COL_ITEM_ID + "=?" ,
new String[]{cartId,String.valueOf(ItemId)},
null,
null,
COL_ITEM_ADDDATE+" desc");
int numRows = c.getCount();
c.close();
db.close();
if(numRows>0)
return true;
else
return false;
} catch (SQLException e) {
db.close();
return false;
}
}
where the parameters are :
Long type itemId= 9882889921
String type cartId = ca1745ef-24eb-4da8-a1ea-9650d78ba5c0
Despite there is recode with these data. the returned rows is 0 , why ?
You have wrapped a ? in single quotes, change this:
COL_ITEM_CART_ID+"='?' and "+ COL_ITEM_ID + "=?"
The line above is trying to literally match COL_ITEM_CART_ID to a question mark, change it like this:
COL_ITEM_CART_ID + "=? and "+ COL_ITEM_ID + "=?"
The query method will sanitize your data input for you, so when you wrap the ?s with apostrophes, the apostrophes become part of the input string itself. This is causing your query to return 0 rows.
To fix the problem, remove change all instances of '?' to ?.
I am using a database to store date and want a single column returned based on which id is given in the where claus.
public String getPmax(String serial) {
String[] columns = new String[]{KEY_SERIAL, KEY_PMAX};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, "serial = " + serial,
null, null, null, null);
String result = "nothing found";
if(c != null) {
int iPmax = c.getColumnIndex(KEY_PMAX);
c.moveToFirst();
result = c.getString(iPmax);
}
return result;
}
This code works if I use a serial that is in the database but it gives me a force close when i am using a serial that's not in the database!
it says:
04-11 16:31:19.423: E/AndroidRuntime(21226): java.lang.RuntimeException: Unable to start activity ComponentInfo{nl.janjanus.solardetect/nl.janjanus.solardetect.DatabaseView}: android.database.sqlite.SQLiteException: no such column: g: , while compiling: SELECT serial, pmax FROM SolareCel WHERE serial = g
So my question is: How do I check if there is result? or do I need to check if the serial given is a serial that is in the database?
if (!(mCursor.moveToFirst()) || mCursor.getCount() ==0){
//cursor is empty
}
If you look closely, the error is that the SQL is faulty.
It reads the g as a column. You have to encase it in apostrophes like so 'g'.
In your case, the line where you get the cursor should be:
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, "serial = '" + serial + "'",
null, null, null, null);
As to checking whether the cursor has no results, look at Samir Mangroliya's answer.
You can check if the Cursor has elements:
boolean isEmpty = cursor.getCount() < 1;
AFAIK, the Cursor never returns null when there is no row which the requested conditions, just return a empty Cursor.
int x = cursor.getCount(); //this will return number of records in current cursor
if ( x == 0 ) {
// there are no records
} else {
// do your stuff
}