0

Using this example (https://stackoverflow.com/a/9109728/2808099) I hooked up my database. But I did not get to organize the search in this database. I am trying to convey a request from the EditText and display the result in the textView (or ListView).

MainActivity

public void LoadAddress(View v)
    {

        searchAddress = editTextSearch.getText().toString();

        TestAdapter mDbHelper = new TestAdapter(this);
        mDbHelper.createDatabase();
        mDbHelper.open();


         Cursor testdata = mDbHelper.getTestData("SELECT * FROM Address " +searchAddress+"';", null);
        String address=testdata.getString(1).toString();
        textView.setText(address); 
        mDbHelper.close();
    }

TestAdapter

  public Cursor getTestData(String s, Object o)
{

    try
    {

        String sql ="SELECT * FROM Address";

        Cursor mCur = mDb.rawQuery(sql, null);
        if (mCur!=null)
        {
            mCur.moveToNext();
        }
        return mCur;
    }
    catch (SQLException mSQLException)
    {
        Log.e(TAG, "getTestData >>"+ mSQLException.toString());
        throw mSQLException;
    }
}

Regardless of the content of the EditText, the first line of output from the database

Community
  • 1
  • 1
user2808099
  • 23
  • 1
  • 5
  • What have you tried, what specifically is your problem, and what are you expecting? (Post it *in the question* please) – gparyani Oct 01 '13 at 18:40
  • "Questions asking for code must **demonstrate a minimal understanding of the problem being solved**. Include attempted solutions, why they didn't work, and the *expected* results." – gparyani Oct 01 '13 at 18:42
  • I would have been happy, but my code has become very confusing (after weeks of attempts to search), and I fell back to the beginning. – user2808099 Oct 01 '13 at 18:54
  • Still, you should post it. We can help identify your problems. – gparyani Oct 01 '13 at 18:57
  • your going to have to be more specific. The DB is open (good connection) and you can query it? Or is the problem in taking data from the edittext? – Alex Oct 01 '13 at 18:57
  • The database opens, I do not know how to transfer data from EditText. – user2808099 Oct 01 '13 at 19:00
  • what goes into the edittext and what do you want out of the database. How you approach the problem will depend on this. Also how large is the dataset? – Alex Oct 01 '13 at 19:04
  • and yes you should be posting at least some code. Try to post some of the most relevant parts. – Alex Oct 01 '13 at 19:10
  • Added code. I'm sorry for being late. – user2808099 Oct 01 '13 at 19:46

1 Answers1

0

Your always calling "select * from Address". This will ALWAYS query EVERYTHING.

String userInp = editTextSearch.getText().toString();

String query = "select * from Addresses where address = " + userInp; 

Cursor cursor= mDbHelper.getDb().rawQuery(query, null);

if (null != cursor)
    cursor.moveToFirst();

String output = cursor.getString(1);

A good tutorial for Android is: http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/

You may want to consider using query with parameters rather than raw sql for such simple queries.

You can learn SQL here: http://www.w3schools.com/sql/

Alex
  • 271
  • 3
  • 8
  • Then why do I get only the first row from the database? And getDb (): java: method getTestData in class com.network.CableDiag.TestAdapter cannot be applied to given types; required: java.lang.String, java.lang.Object found: no arguments reason: actual and formal argument lists differ in length – user2808099 Oct 01 '13 at 21:00
  • if you are doing above then you are only moveing the cursor to the first position. Also getDb() was a simplification. YOU write CableDiag.TestAdapter..... note in your TestAdapter you have the line mDb.rawQuery() so you have the db. I just passed the db up to your main class via what would be a typical getter method in most normal classes. – Alex Oct 01 '13 at 23:35