I am trying to make a simple android app that get info of user from database, and I want to separate these info by line between them.
like this:
here is my code:
public String getData() {
// TODO Auto-generated method stub
String[] columns = new String[] { KEY_ID2, KEY_NAME, KEY_EMAIL};
Cursor c = ourDbase.query(TABLE_SCORE, columns, null, null, null, null, null + " DESC");
String result ="";
int iRow = c.getColumnIndex(KEY_ID);
int iName = c.getColumnIndex(KEY_NAME);
int iEmail= c.getColumnIndex(KEY_EMAIL);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
result = result + c.getString(iEmail) + " " + c.getString(iName) + " -" + c.getString(iRow)+ "\n";
}
return result;
}
Do you mean you want something like :
1 Jason j#yahoo.com-2 Mark m#yahoo.com-3 Freya f#yahoo.com
You can do something like :
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
result = result + c.getString(iEmail) + " " + c.getString(iName) + " -" + c.getString(iRow)+ "\n";
result+="-"; //add "-" for each person
}
Please tell me if i miss-understood your question.
UPDATE
Looking from your example, you probably want to use a List of person. If you want this, try :
public List<Person> getData() {
// TODO Auto-generated method stub
String[] columns = new String[] { KEY_ID2, KEY_NAME, KEY_EMAIL};
Cursor c = ourDbase.query(TABLE_SCORE, columns, null, null, null, null, null + " DESC");
List<Person> people = new ArrayList<Person>();
int iRow = c.getColumnIndex(KEY_ID);
int iName = c.getColumnIndex(KEY_NAME);
int iEmail= c.getColumnIndex(KEY_EMAIL);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
Person p = new Person();
p.setEmail(c.getString(iEmail));
//set other info, like id, name
people.add(person);
}
return people;
}
Where person is a new public class with private id,name and email.
UPDATE
public class Person {
private String name, id, email;
//create the name,id, email setter - getter or just make those variables public
}
This is what class Person should look like :
Feel free to ask if you dont understand or i miss understood you.
Related
hello am new at android development. am creating an app that has four columns. Student, age, number and class...but the data displayed under the columns is disorganized, how can i organize the data displayed in proper columns?
here is my code.
my database name = schooldatabase
String[] columns = new String[] { KEY_ROWID, KEY_STUDENT, KEY_AGE, KEY_ID_NUMBER, KEY_CLASS };
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result = "";
ArrayList<String> mArrayList = new ArrayList<String>();
int IROW = c.getColumnIndex(KEY_ROWID);
int ISTUDENT = c.getColumnIndex(KEY_STUDENT);
int IAGE = c.getColumnIndex(KEY_AGE);
int INUMBER = c.getColumnIndex(KEY_ID_NUMBER);
int ICLASS = c.getColumnIndex(KEY_CLASS);
// ////////////////////////////////////////////////////////
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
// ////////////////////////////////////////////////////
result = result + c.getString(IROW) + " " + c.getString(ISTUDENT)
+ " " + " " + c.getString(IAGE) + " " + " "
+ c.getString(INUMBER) + " " + " " + c.getString(ICLASS)
+ "\n";
}
return result;
You can use rawQuery to query items as what you want column like:
ourDatabase.rawQuery("select <your columns order>" + from + DATABASE_TABLE,null);
And I think whether the data is ordered depends on how you resolve this data,when you get all data you can handle these as you want order.
I have two columns in a database, id and city. How can I get all the city data and put it in a string when the row id is less than 3. I am having difficulty in writing a query to get data when the row id is less than 3. Right now my method gets all the data from the database. Thanks.
String[] columns = new String[]{KEY_ROWID, KEY_CITY};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result =" ";
int iRow = c.getColumnIndex(KEY_ROWID);
int iCity = c.getColumnIndex(KEY_CITY);
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
result = result + c.getString(iCity) + "\n";
}
return result;
String[] columns = new String[]{KEY_ROWID, KEY_CITY};
// "note "rowid" is used in fts tables and "id" in normal tables.
// I'm assuming your constant KEY_ROWID chose the correct one.
String where = KEY_ROWID + " <= " + 3;
Cursor cursor = ourDatabase.query(DATABASE_TABLE, columns, where, null, null, null, null);
if(!cursor.moveToFirst()){
Log.e("System.out", "cursor is empty");
}else{
int ix_city = cursor.getColumnIndexOrThrow(KEY_CITY);
StringBuilder sb = new StringBuilder();
do{
String city = cursor.getString(ix_city);
Log.i("System.out", "city name " + city);
if(TextUtils.isEmpty(city))
continue;
sb.append(city).append("\n");
}while(cursor.moveToNext());
Log.i("System.out", "the entire list " + sb.toString());
}
I am trying to display a SQLite table in a view. I want my first column to have a width of 20 characters. I have attempted to achieve this effect by using the substring method but I get stringindexoutofboundsexceptions with strings less than 20 characters. Any help is greatly appreciated. Here is my code:
Method for displaying table in view:
public String getData() {
// TODO Auto-generated method stub
String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_QUANTITY, KEY_VALUE };
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result = "";
int iRow = c.getColumnIndex(KEY_ROWID);
int iName = c.getColumnIndex(KEY_NAME);
int iQuantity = c.getColumnIndex(KEY_QUANTITY);
int iValue = c.getColumnIndex(KEY_VALUE);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
result = result + /*c.getString(iRow) + " " +*/ c.getString(iName).substring(0, 20) + " " + c.getString(iQuantity) + " " + c.getString(iValue) + "\n";
}
Just use c.getString(iName).substring(0, Math.min(20, c.getString(iName).length()) (possibly using a temporary for c.getString(iName)) instead of the substring you have now.
If you don't want them then why select. You can just do
String[] columns = new String[]{ KEY_ROWID, "substr(" + KEY_NAME + ",1,20)", KEY_QUANTITY, KEY_VALUE };
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
and get at most 20 chars from the column.
Hello friends i am new to android.well i have created a database table in that i have few rows as follows
_id name city
1 maddy0 xyz0
2 maddy1 xyz1
3 maddy2 xyz2
now what i want to pass a city to the function and get the row id of that city
say i pas xyz0 then it should give me that out put as 1 .for that i tried this
public String getDatatonumber(String s) {
Cursor c = null;
String result = new String();
// TODO Auto-generated method stub
// String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_SCORE};
try {
c = ourDatabase.rawQuery(
"select _id from quotes_internal where text ='"+s+"';", null);
// Cursor c = ourDatabase.query(DATABASE_TABLE_LOGICAL, columns,
// null, null, null, null, null);
int iName = c.getColumnIndex(KEY_ROWID);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
result=(" " + c.getInt(iName));
}
} catch (Exception e) {
// TODO: handle exception
} finally {
c.close();
}
return result;
}
I put this code in the database helper class and from the main activity I am calling it as
results =new String();
results=db.getDatatonumber(texts).trim();
text.settext(results);
I am not able to understand where I am going wrong I am passing the same values which are there in the database.
c = ourDatabase.rawQuery(
"select _id from quotes_internal where city='"+s+"';", null);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
result=(" " + c.getColumnIndex("_id"));
}
this will return your row id
You need to user LIKE Operator while comparing with Text
c = ourDatabase.rawQuery("select _id from quotes_internal where text LIKE '" + s.trim() + "';", null );
I am currently creating a database to store a list of names and coordinates of service stations. However I cannot view the database when i click the view button. Here is my code: `
public String getData()
{
// TODO Auto-generated method stub
String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_COORDINATES};
Cursor c = myDatabase.query(DATABASE_NAME, columns, null, null, null, null, null);
String result = "";
int iRow = c.getColumnIndex(KEY_ROWID);
int iName = c.getColumnIndex(KEY_NAME);
int iCoordinates = c.getColumnIndex(KEY_COORDINATES);
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
{
result = result + c.getString(iRow) + " " + c.getString(iName) + " " + c.getString(iCoordinates) + "\n";
}
return result;
}`
Does anybody see a problem with this code?
Your cursor might not contain any rows. If so, c.getString() will throw exception.
moveToFirst() returns false on error. Do your loop like this:
if (c.moveToFirst())
{
do
{
result = result + c.getString(iRow) + " " + c.getString(iName) + " " + c.getString + "\n";
} while (c.moveToNext())
}
then you can deal with why your table has no rows if this is in fact the case...