1

hi im very new to android so i have this function that needed to add and retrieve from my database. so far here is what i have, so i have this set of java files newLoc.java contains the code that responds when the save button is clicked by the user. Area.java contains the getter and setter. DBHandler.java have the database stuff. In the add area i have logged it so that i can check if it is added or not. the problem is when im reading the table. in my newLoc.java, im also calling the getALLAREA function to check wheter the record was inserted but the only ouput im getting is the ID of the records in the database any idea wht i am doing wrong? the code are here

newLoc.java

here are the codes responding to when the user clicked the save button. it gets the data from the editText then calls the DBHanlder to add the data

 public void onClick(View v)
    {
        String text ="user created";

        Toast tempMessage =
                Toast.makeText(newLoc.this,
                        text,
                        Toast.LENGTH_SHORT);
        tempMessage.show();

        areaname = narea.getText().toString();
        DBHandler db = new DBHandler(newLoc.this);
        Log.d("Insert: ", "Inserting .." + areaname);
        db.addNArea( new Area(areaname));

        Log.d("Reading: " , "Reading users..");
        List<Area> area = db.getALLAREA();

        for(Area ar : area)
        {
            String arean = ar.getArea();
            Log.d("Areas" , arean);
        }

    }

Area.java

this is my getter and setter from the area

public class Area
{
private int arID;
private String narea;

public Area()
{
}
public Area(String area)
{
    this.narea = area;
}

public void setArea(String area)
{
    this.narea = area;
}

public String getArea()
{
    return narea;
}
}

DBHandeler.java this is where the database stuff is done. in the addNArea, i have put a log inside the try to check if the data was added. the log is printing the data from the user so i think it is properly inserting because i wasn't able to see the error from the log

    public void addNArea(Area are)
{
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_ANAME, are.getArea());
    try
    {
        db.insert(TABLE_AREA, null, values);
        Log.d("Insert: ", "Inserting .." + are.getArea());
    }
    catch (SQLException e)
    {
        Log.d("error: " , "" + e);
    }


    db.close();
}

//view areas
public List<Area> getALLAREA()
{
    List<Area> arealist = new ArrayList<Area>();
    String selectQuery = "SELECT * FROM " + TABLE_AREA;
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    if (cursor.moveToFirst()) {
        do {
            Area allarea = new Area();
            allarea.setArea(cursor.getString(0));

            arealist.add(allarea);
        } while (cursor.moveToNext());
    }
    return arealist;
}

so the problem now is that in my newLoc.java i can't see the list of the areas i have added. any ideas on what i am doing wrong? thanks so much in advance!

1 Answer 1

1

You should not use

    'SELECT *'

use:

  'SELECT KEY_ANAME'

And thats why: By creating the content value

    values.put(KEY_ANAME, are.getArea())

the db driver will generate:

  INSERT INTO tabname (col1) values ('my area name')

in case that u defined

   KEY_ANAME = "col1"

Maybe that is what u realy whanted to do: Assume that ur table has that definition:

    TArea (pk_area int pkey, area_name varchar(100))

Than your insert should look like:

    private static final String COL_NAME = "area_name";
    private static final String PKEY = "pk_area";
    private static final String TAB_AREA = "TArea";
    ...
    values.put(COL_NAME, are.getArea())

And your select like:

    String sql = "SELECT " + PKEY +", "+ COL_NAME + " FROM " + TAB_AREA;
    Cusor cur = db.rawQuerry(sql, null);
    ...
        int id = cur.getInt(0);
        String name = getString.get(1);
Sign up to request clarification or add additional context in comments.

1 Comment

thaanks so much! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.