0

when button is pressed i want to the text to change to the database collumn values, i know its wrong but here is the code:

    private void MostraDados() {
    // TODO Auto-generated method stub
    final TextView text = (TextView) findViewById(R.id.tvUSUARIO);

    Button mostrar = (Button) findViewById(R.id.bMostrar);
    mostrar.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            db = openOrCreateDatabase("dbtest.db", Context.MODE_PRIVATE, null);

            String q = "SELECT * FROM dbtest.db WHERE usuarioorigem='";

            text.setText(q);


            //text.execSQL("DROP COLUMN IF EXISTS usuarioorigem");
        }
    });
}
Taryn
  • 242,637
  • 56
  • 362
  • 405
baTimá
  • 554
  • 1
  • 10
  • 28

3 Answers3

0

The SQL is not written correctly. You must SELECT from a column. And you're passing the query string the the text view. You should first review how to query the database with the cursor, and how to retrieve what you want from the cursor.

So I would look into how to use the curosr, all of that's available in the Android docs, and you might want to try the API demos in the emulator I'm sure you can learn how to work with the cursor there as well. So look here, http://developer.android.com/reference/android/database/Cursor.html.

And here, Is Android Cursor.moveToNext() Documentation Correct?.

Community
  • 1
  • 1
CAM-Dev
  • 186
  • 3
  • could you help me by explaining it in my code? well i tried something but still gives errors in my setText. cursor = db.rawQuery("SELECT * FROM dbtest.db WHERE usuarioorigem", null, null); usuario.setText(cursor); – baTimá Sep 06 '12 at 20:34
0

After getting the cursor, you could do something like this:

while(c.moveToNext(){

text.setText(c.getString(0))

}
CAM-Dev
  • 186
  • 3
0

Your code is missing some critical parts for example a DatabaseClass that manages the cursor and database.

private void MostraDados() {

final TextView text = (TextView) findViewById(R.id.tvUSUARIO);

Button mostrar = (Button) findViewById(R.id.bMostrar);
mostrar.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        // our missing a database helper
        MyDatabaseClass dbhelper = new MyDatabaseClass();
        dbhelper.open();

        Cursor result = dbhelper.doMyQuery();
        String mystring = result.getString(0);

        text.setText(mystring);

        dbhelper.close();
    }
});
....
public class WorkoutDbAdapter {
    ....
    public Cursor doMyQuery()
    {
        return this.mDb.query( yourQuery );
    }

}

This is the minimum you'd need and even with the above i'm missing a lot of the smaller detail. Search for some tutorials on creating and using Databases.

Essentially however you need to get the cursor back, set the position of the cursor, or cursor.moveNext() and then get the value that you can assign to the textField.

Your source code lacks a correct call to a database and access to the cursor. Hopefully you'll find some decent tutorials that will flesh the rest out for you.

Emile
  • 11,451
  • 5
  • 50
  • 63