Reputation: 187
I have a database which have around 100 tables. In all tables all the field has different datatype like int,string etc.Now requirement is.
When I use Select query for any tables it should give me all the fields of the particular table as String type. If select query can provide all the filed as string then I don't need to write cursor.getInt()
for int
datatype. everywhere I only need to use cursor.getString()
.
Please tell me such a select query that return all the field as String type.
Upvotes: 0
Views: 508
Reputation: 15774
Please check the code snippet below. The getTableContents
returns an ArrayList
containing another ArrayList
or String
data. The inner list is the contents of each tuple/row of the table in String
format. The getAsString
method contains the actual sauce of checking for data type and converting to String
if necessary.
public ArrayList<ArrayList<String>> getTableContents(String inStrQuery, String[] selectionArgs){
ArrayList<ArrayList<String>> rows = new ArrayList<ArrayList<String>>();
//Check database and open if required
if(null == database){
open();
}
Cursor cursor = database.rawQuery(inStrQuery, selectionArgs);
try {
cursor.moveToFirst();
while(!cursor.isAfterLast()){
ArrayList<String> row = new ArrayList<String>();
int columnCount = cursor.getColumnCount();
for(int c=0; c< columnCount; c++){
row.add(getAsString(cursor, c));
}
rows.add(row);
cursor.moveToNext();
}
}finally{
cursor.close();
}
return rows;
}
public static String getAsString(Cursor cursor, int colIndex){
String retVal = null;
if(null != cursor){
switch(cursor.getType(colIndex)){
case Cursor.FIELD_TYPE_INTEGER:
retVal = String.valueOf(cursor.getInt(colIndex));
break;
case Cursor.FIELD_TYPE_BLOB:
retVal = String.valueOf(cursor.getBlob(colIndex));
break;
case Cursor.FIELD_TYPE_FLOAT:
retVal = String.valueOf(cursor.getFloat(colIndex));
break;
case Cursor.FIELD_TYPE_NULL:
retVal = String.valueOf(null);
break;
case Cursor.FIELD_TYPE_STRING:
retVal = cursor.getString(colIndex);
break;
}
}
return retVal;
}
Upvotes: 0
Reputation: 614
Hi @Nil you need to cast int to string in query like:: "select cast(some_integer_column as text) from some_table"
Upvotes: 2