1

This is our main class to get information from the database. We are trying to get the values of the "name" column in the "barcode" database that we have created. Right now, we are not able to get the desired value but values as "a, b, c, d, ..." (such as; it returns "a" when we ask for the 1st one, it return "b" when we ask for the second one in the "name" column) value with the cursor.getString method

    public class MainActivity extends Activity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            DataBaseHelper myDbHelper = new DataBaseHelper(this);
            myDbHelper = new DataBaseHelper(this);
            try {
                myDbHelper.createDatabase();
            } catch (IOException ioe) {
                throw new Error("Unable to create database");
            }

            try {
                myDbHelper.openDataBase();
            } catch(SQLException sqle){
                throw sqle;
            }

            Cursor cursor = myDbHelper.getAllAccounts();

            String[] values = new String[6];
            int i=0;

            //take into the array from the database
            while(cursor.moveToNext()){
                values[i]= cursor.getString(cursor.getColumnIndex("name"));
                i++;
            }

            cursor.close(); 

            //Write to textview
            TextView youtextview = (TextView) findViewById(R.id.textView1);
            youtextview .setText(values[2]);
        }
    }
MAV
  • 7,260
  • 4
  • 30
  • 47
  • 2
    `getString method does not work` - yes it does. Your code apparently doesn't but you haven't clearly described what the problem is. What *exactly* happens? – Simon Apr 29 '13 at 18:45
  • I actually meant that instead of the value of values[2] in the database i just got the value "b". when i change values[2] to values[1] I got "a" or values[3] i got "c". In the database I didn't have any data like "a","b" or "c". I created database in SQLite Database Browser by the way. My database looks like: – user2255446 Apr 29 '13 at 19:11
  • it has an id column which is an "integer primary key" and name column which is "text". – user2255446 Apr 29 '13 at 19:12
  • http://stackoverflow.com/questions/9029668/android-sqlite-cursor-getcolumnindex-is-case-sensitive – Yaqub Ahmad Apr 30 '13 at 01:41

1 Answers1

0

You are not dealing with the cursor in the right way (also you should not do DB calls in the main UI thread which is what you are doing right now). See: http://developer.android.com/reference/android/database/Cursor.html

static final COL_NAME = "name";
Cursor cursor = myDbHelper.getAllAccounts();
cursor.moveToFirst();
int maxCursorIndex = cursor.getCount();
for(int cursorIndex=0;cursorIndex<maxCursorIndex;cursorIndex+=1){
    values[cursorIndex] = cursor.getString(cursor.getColumnIndex(COL_NAME);
}
cursor.close();
Krispy
  • 1,098
  • 1
  • 7
  • 11