'What does getString(1) mean in terms of getting info from a database?
So there is this tutorial on reading data from databases. My questions are:
Do the numbers inside the getString represent the field that the method is getting the string from?
Here is the method:
public ArrayList<CourseModal> readCourses() {
// on below line we are creating a
// database for reading our database.
SQLiteDatabase db = this.getReadableDatabase();
// on below line we are creating a cursor with query to read data from database.
Cursor cursorCourses = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
// on below line we are creating a new array list.
ArrayList<CourseModal> courseModalArrayList = new ArrayList<>();
// moving our cursor to first position.
if (cursorCourses.moveToFirst()) {
do {
// on below line we are adding the data from cursor to our array list.
courseModalArrayList.add(new CourseModal(cursorCourses.getString(1),
cursorCourses.getString(4),
cursorCourses.getString(2),
cursorCourses.getString(3)));
} while (cursorCourses.moveToNext());
// moving our cursor to next.
}
// at last closing our cursor
// and returning our array list.
cursorCourses.close();
return courseModalArrayList;
}
Solution 1:[1]
Lets start with the query "SELECT * FROM SomeTable". What does it do?
What is does is return all of the rows in the table (in some order that will depend on the actual database). This row set presented via the Cursor object returned by the rawQuery command.
The Cursor API presents the row set as a sequence of rows, and allows the application to step through the sequence. At any point in time, the Cursor has a current row.
The Cursor API provides a bunch of get methods (e.g. getFloat, getInt, getString, getBlob) that take an column index argument and return a value. The column index is the index of a column / field within the the row.
You can lookup the column index value using the
Cursor.getColumnIndex(String)method where the string is the name of the column; e.g. from the row set. See the javadoc.Alternatively, you can rely on the fact that order of the column indexes is the same as the order of the columns in the row set. Thus index
1is the first column in the rowset, index2is the second column, and so on.
The example code is doing the latter. So cursorCourses.getString(2) means return the value of the second column of the current row as a string.
Finally, in your example they have used SELECT * to select all columns in the table. The columns in the resulting row set will be in the same order as columns in the table declaration.
It is worth noting that using SELECT * and hard-wired column index values like that code does makes the code rather fragile. If someone or something added an extra column at the start or reordered the columns, the hard-wired indexes would be incorrect. You can avoid this by selecting specific columns, or by using getColumnIndex as above.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
