Android Studio: Unable to print cursor results - android

I am attempting to print the cursor results to log.d.
When I print the results of the cursor, it does not print the array.
i.e. D/Row values: com.example.androidlabs.Todo#7c9655f
Here is the code:
public void printCursor(Cursor c) {
//The database version number using db.getVersion for the version number.
int version = db.getVersion();
//The number of rows in the cursor
int rowCount = c.getCount();
//The number of columns in the cursor
int columnCount = c.getColumnCount();
//The names of the columns in the cursor
String[] columnNames = c.getColumnNames();
//The results of each row in the cursor
ArrayList<Todo> rowValuesList = new ArrayList<>();
int ColIndex = c.getColumnIndex(myOpener.COL_1);
int itemColIndex = c.getColumnIndex(myOpener.COL_2);
int urgentColIndex = c.getColumnIndex(myOpener.COL_3);
c.moveToFirst();
if(c.moveToFirst()) {
long id = c.getLong(ColIndex);
String item = c.getString(itemColIndex);
int urgentInt = c.getInt(urgentColIndex);
if (urgentInt == 1) {
urgent = true;
} else {
urgent = false;
}
rowValuesList.add(new Todo(item, urgent, id));
}
String rowValues = TextUtils.join(",", rowValuesList);
//Printing variables to log
Log.d("Database version", String.valueOf(version));
Log.d("Row count", String.valueOf(rowCount));
Log.d("Column count", String.valueOf(columnCount));
Log.d("Column names", Arrays.toString(columnNames));
Log.d("Row values", rowValues);
}
Other options I have tried that have not worked:
Log.d("Row values", rowValuesList.toString());
for (Todo t : rowValuesList) {
Log.d("Row values", String.valueOf(t));
}
StringBuilder s = new StringBuilder();
for(Todo todo : rowValuesList) {
s.append(todo);
s.append(",");
}
Log.d("Row values", String.valueOf(s));
I know the cursor is not empty as it is displays the results when loaded from SQLite on the application.
Any advice would be helpful.
Thank you,

Your output is:
D/Row values: com.example.androidlabs.Todo#7c9655f
That would make sense if:
rowValuesList contains a single Todo object, and
Your Todo class does not have a custom implementation of toString()
The default implementation of toString() that you inherit from Object gives a result like what you see: the fully qualified class name and an object ID, separated by #.

Related

How to return all value from a function

I want to return all values of dbManagement.ORIGINATING_ADDRESS from this function but it just gives me last one value to blockedNumber. I know it'll give last one value but how can i get all value. kindly help
public String selectBlockedNumbers() {
Cursor cursor = dbManagement.selectBlockedNumbers();
String blockedNumber = null;
cursor.moveToFirst();
for(int i = 0; i < cursor.getCount(); i++) {
blockedNumber = cursor.getString(cursor.getColumnIndex(dbManagement.ORIGINATING_ADDRESS));
cursor.moveToNext();
}
Toast.makeText(this, blockedNumber, Toast.LENGTH_LONG).show();
return blockedNumber;
}
In
Cursor cursor = dbManagement.selectBlockedNumbers();
the function selectBlockedNumbers() consist of following query:
cursor = db.rawQuery("SELECT " + ORIGINATING_ADDRESS + " FROM " + TABLE_BLOCK_LIST, null);
When the for-loop runs for the last time, blockedNumbers will contain the last number retrieved. So, instead of returning a String, rather returns a List<String>. In that case your code should change into
public List<String> selectBlockedNumbers() {
Cursor cursor = dbManagement.selectBlockedNumbers();
List<String> blockedNumbers = new ArrayList<String>();
cursor.moveToFirst();
for(int i = 0; i < cursor.getCount(); i++) {
String blockedNumber = cursor.getString(cursor.getColumnIndex(dbManagement.ORIGINATING_ADDRESS));
blockedNumbers.add(blockedNumber);
cursor.moveToNext();
}
Toast.makeText(this, blockedNumber, Toast.LENGTH_LONG).show();
return blockedNumbers;
}

I need help for getting SQLite data

I use this method for retrieving my data
public String getdata() {
String[] columns= new String[]{RowId,RowBusinessName};
Cursor c=OurDatabase.query(TableName,columns,null,null,null,null,null);
String Result="";
int iRowId=c.getColumnIndex(RowId);
int iRowBusinessName=c.getColumnIndex(RowBusinessName);
for(c.moveToFirst();!c.isAfterLast();c.moveToNext()){
Result=Result+c.getString(iRowBusinessName)+"\n";
}
return Result;
}
How can I make it return structured data (id & business_name)?
I want to display every business_name in a single textview.
Please help
If I understand what you are trying to do, here is the solution if you want to get only 1 RowBusinessName returned as a String. (Hoping that your RowBusinessName is type String).
public String getdata(int rowId) {
String[] columns= new String[]{RowId,RowBusinessName};
Cursor cursor = db.query(TABLENAME, columns, RowId + "=?", new String[]{rowId + ""}, null, null, null, null);
String Result="";
if (cursor != null && cursor.moveToFirst()) {
// not required though
int rowId = cursor.getInt(cursor.getColumnIndexOrThrow(RowId));
String rowBusinessName = cursor.getString(cursor.getColumnIndexOrThrow(RowBusinessName));
result = rowBusinessName;
}
return result;
}
Now if you want a list of RowBusinessName, then you have to build a List<String> rather than appending it to Result. That's not really a good way!
public List<String> getAll() {
List<String> businessNameList = new ArrayList<String>();
String[] columns= new String[]{RowId,RowBusinessName};
Cursor c=OurDatabase.query(TableName,columns,null,null,null,null,null);
if (c != null && c.moveToFirst()) {
// loop until the end of Cursor and add each entry to Ticks ArrayList.
do {
String businessName = cursor.getString(cursor.getColumnIndexOrThrow(RowBusinessName));
if (businessName != null) {
businessNameList.add(businessName);
}
} while (c.moveToNext());
}
return businessNameList;
}
These are work around.
The appropriate answer would be to create an Object that holds id and businessName. That way, you build an object from DB and just return the entire Object.

insertWithOnConflict() vs. Insert or replace

I was refactoring my code and replacing all insert statements with insert or replacewhile i found the following method that uses db.insert() instead of a prepared statement:
public static void insertIntoTableByNameAndFieldsAndValues(
String tableName, String[] fields, String[] values, Context c)
throws FieldsAndValuesMismatchException, UnrecognizedTypeException {
DatabaseAbstractionLayer dal = new DatabaseAbstractionLayer(c);
SQLiteDatabase db = dal.getWritableDatabase();
ContentValues con = new ContentValues();
if (fields.length != values.length)
throw new FieldsAndValuesMismatchException(
"fieldsString and values are not the same size");
int fieldCount = fields.length;
for (int i = 0; i < fieldCount; i++) {
String[] fieldArr = fields[i].split(":");
String value = values[i];
if (fieldArr[1].equalsIgnoreCase("long")) {
// long
if(value.equalsIgnoreCase("null")){
con.putNull(fieldArr[0]);
}else{
con.put(fieldArr[0],(long) Integer.parseInt(value));
}
} else if (fieldArr[1].equalsIgnoreCase("string") || fieldArr[1].equalsIgnoreCase("text")) {
// string
if(value.equalsIgnoreCase("null")){
con.putNull(fieldArr[0]);
}else{
con.put(fieldArr[0], value);
}
}else if(fieldArr[1].equalsIgnoreCase("double")){
//double
if(value.equalsIgnoreCase("null")){
con.putNull(fieldArr[0]);
}else{
con.put(fieldArr[0], Double.valueOf(value));
}
} else {
throw new UnrecognizedTypeException(fieldArr[1]
+ " is not string,text, long or double");
}
}
db.insert(tableName, null, con);
db.close();
}
I don't want to rewrite the code and use a raw query.
Is there a way to replace a row when a conflict occurs? I guess insertWithOnConflict() would do that but I couldn't find a good example.
Yup, (change BaseColumns._ID to something suitable if you do not follow the standard name for the row identifier used by Android) and this should do what you want:
db.insertWithOnConflict(tableName, BaseColumns._ID, v,
SQLiteDatabase.CONFLICT_REPLACE);
But - that method sure does a lot of pointless things. In short, it could be boiled down to this:
public static void insertIntoTableByNameAndFieldsAndValues(
String tableName, String[] fields, String[] values, Context c)
throws FieldsAndValuesMismatchException {
if (fields.length != values.length) {
throw new FieldsAndValuesMismatchException(
"fields[] and values[] are not the same length");
}
ContentValues v = new ContentValues();
// You really don't need to distinguish between integers, doubles
// etc. etc. - SQLite is type agnostic, just dumping Strings from Java
// will work just fine - the *only* point in converting stuff is to check
// the format prior to inserts.
// Using "null" as the null identifier is also pointless. Pass actual null
// values instead - calling #putNull("somecolumn") is the same
// as #put("somecolumn", null)
for (int i = 0; i < fields.length; i++) {
v.put(fields[i], values[i]);
}
db.insertWithOnConflict(tableName, BaseColumns._ID, v,
SQLiteDatabase.CONFLICT_REPLACE);
}

How to access particular column value in android sqlite application?

I have an application where I want to use the values returned from an SQL query against a database in my application. I want to use attach these values to a uri that links to a script on remote host. I am doing a call to the method to retrieve the values, after which I will now extract them. The issue is that I am facing a challenge in going about this. I know a bit of php and what I want in android is done like this in php:
....(preceding code)...$row['email'];
and I can now assign a variable e.g. $email=$row['email'];
How can I do this in android?
I don't want a cursor returned, just the values of the columns as strings.
Below is my code (I will need help to retrieve the password column)
public String[] selectAll()
{
Cursor cursor = db.query(DB_TABLE_NAME, new String[] { "email","password"},null, null, null, null,null);
if(cursor.getCount() >0)
{
String[] str = new String[cursor.getCount()];
int i = 0;
while (cursor.moveToNext())
{
str[i] = cursor.getString(cursor.getColumnIndex("email"));
i++;
}
return str;
}
else
{
return new String[] {};
}
}
I need help on inserting the "password" column so that it will be returned with the email.
try this way
String[][] str = new String[cursor.getCount()][cursor.getColumnCount()];
while (cursor.moveToNext())
{
for(int j=0;j<cursor.getColumnCount();j++)
str[i][j] = cursor.getString(j); /// may be this column start with the 1 not with the 0
i++;
}
you need to store this in two dimenstion array for row wise and column wise
but if this will give only one result everytime
String[] str = new String[cursor.getColumnCount()]; // here you have pass the total column value
while (cursor.moveToNext())
{
str[0] = cursor.getString(cursor.getColumnIndex("email"));
str[1] = cursor.getString(cursor.getColumnIndex("password"));
}
You should change your design like this
public Cursor selectAll() {
Cursor cursor = db.query(DB_TABLE_NAME, new String[] { "email","password"},null, null, null, null,null);
return cursor;
}
and use the selectAll function like this
Cursor cursor=selectAll();
String[] mail = new String[cursor.getCount()];
String[] pass = new String[cursor.getCount()];
int i=0;
while (cursor.moveToNext())
{
mail[i] = cursor.getString(cursor.getColumnIndex("email"));
pass[i] = cursor.getString(cursor.getColumnIndex("password"));
i++;
}

Two Cursors, ListView, CursorIndexOutOfBoundsException

As a beginner in android java world I need your help. I've got problem with famous "CursorIndexOutOfBoundsException".
I'm using SQLite db, and I have two cursors, I'm getting some rows from database.
I need to get some previous values with some conditions with second cursor (c2) and put these values on ListView.
Code works with one exception:
"android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0".
ListView looks OK if I just ignore this exception but I want to fix it.
I know it is connected to with Cursor. I tried to check some conditions - it didn't help. Maybe if you could take a look at my code you find where is the cause.
Code:
public void LoadLogGrid()
{
dbHelper=new DatabaseHelper(this);
try
{
int LogName = (int) spinLog.getSelectedItemId();
Cursor c=dbHelper.getLogByLogID(LogName);
if (c != null) c.moveToFirst();
int count = c.getCount();
if (c.moveToFirst()){
ArrayList<String> mArrayList = new ArrayList<String>();
int i=0;
do {
int sVar1 = c.getInt(c.getColumnIndex("Var1"));
Long sId = (long) c.getInt(c.getColumnIndex("_id"));
Cursor c2=dbHelper.getPrevLogByLogID(LogName,sVar1);
c2.moveToFirst();
if (c2!=null) {
String sPrevOdo = c2.getString(c2.getColumnIndex("Odo"));
mArrayList.add(sPrevOdo);
c2.close();
} else {
//stopManagingCursor(c2);
//c2.close();
Log.d("A:", "Something");
}
String [] from=new String []{"Date","Col1","Col2","Col3"};
int [] to=new int [] {R.id.logDate,R.id.logCol1,R.id.logCol2,R.id.logCol3,R.id.rowOpt2};
SimpleCursorAdapter sca=new LogCursorAdapter(this,R.layout.loggridrow,c,from,to,mArrayList);
grid.setAdapter(sca);
registerForContextMenu(grid);
i++;
} while (c.moveToNext());
c.close();
dbHelper.close();
}
}
catch(Exception ex)
{
AlertDialog.Builder b=new AlertDialog.Builder(this);
b.setMessage(ex.toString());
b.show();
}
}
Query in second cursor:
public Cursor getPrevLogByLogID(long LogID, long Var1)
{
SQLiteDatabase db=this.getReadableDatabase();
String[] params=new String[]{String.valueOf(LogID),String.valueOf(Var1)};
Cursor c2=db.rawQuery("SELECT LogID as _id, Col1 from Log WHERE Col2=? AND Col3<? AND Full=1 ORDER BY Odo DESC", params);
if (c2 != null) { c2.moveToFirst();}
return c2;
}
Try changing your
do {
....
} while (c.moveToNext());
to
while (c.moveToNext()) {
....
}
The way it is now, it will run the loop at least once no matter what.
you have moved the c2 to the position as c2.moveToFirst() and after that you are doing the checking process whether it is null or not and i think the cursor should be null so that the exception is raised try putting the checking condition before moving the cursor to the first position

Categories

Resources