please I would need your help to figure out, why these code lines do not work.
I would like to fill a list with String elements taken from the database.
This is the mothod fillTheList() which returns a list:
public List<String> fillTheList() {
List<String> list = new ArrayList<String>();
//My database is called "ITEMS_" and the first column is called "typeOfItem"
String[] column = {"typeOfItem"};
String selection = "SELECT DISTINCT type FROM ITEMS_";
Cursor cursor = MainActivity.sqLiteHelper.getDataCategories("ITEMS_", column, selection);
if(cursor != null) {
list.clear();
if (cursor.moveToFirst()) {
do {
String type = cursor.getString(1);
list.add(type);
} while (cursor.moveToNext());
}
}
Log.d("List", cursor.getString(1));
return list;
}
The getDataCategories() is a mehod of the sqLiteHelper-class
public Cursor getDataCategories(String table, String[] columns, String selection )
{
SQLiteDatabase database = getReadableDatabase();
Cursor cursor = database.query(true, table, new String[] {String.valueOf(columns)}, selection,
null, null, null, null, null);
return cursor;
}
The error occurs at:
Cursor cursor = MainActivity.sqLiteHelper.getDataCategories("ITEMS_", column, selection); and the logcat is:
"Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor SQLiteHelper.getDataCategories(java.lang.String, java.lang.String[], java.lang.String)' on a null object reference"
The error log indicates that MainActivity.sqLiteHelper is null.
I don't know in which class you have fillTheList(), but you deal with the issue of filling the list in a wrong way.
Change fillTheList() it like:
public List<String> fillTheList() {
List<String> list = new ArrayList<>();
Cursor cursor = sqLiteHelper.getDataCategories();
if(cursor != null) {
if (cursor.moveToFirst()) {
do {
list.add(cursor.getString(0));
} while (cursor.moveToNext());
}
}
return list;
}
As you can see you must supply a context for the initialization of SQLiteHelper to fetch the cursor and populate the list.
Then change getDataCategories() to this:
public Cursor getDataCategories() {
SQLiteDatabase database = getReadableDatabase();
return database.rawQuery("SELECT DISTINCT typeOfItem FROM ITEMS_", null);
}
This method uses rawQuery() which is simpler and not query().
I don't know if the name of the column is typeOfItem or type, so if you need change the sql statement.
Now you can use fillTheList() if first you have a valid SQLiteOpenHelper object, say sqlObject, like:
List<String> list = sqlObject.fillList();
Related
I use a content provider/resolver, have a separate project/lib that provides a number of DB helper methods. I have a second project/lib that does handy things with a cursor.
Imagine as such DB Helper Method (com.example.DBHelper):
public String[] dumpColumnTable() {
Cursor cursor = cr.query(MY_URI,
new String[] { FIELD },
null,
null,
null
);
return UtilMethods.createArrayFromCursor(cursor);
}
Then the Util methods (com.example.UtilMethods):
public static String[] createArrayFromCursor(Cursor cursor) {
return createArrayFromCursor(cursor, 0);
}
public static String[] createArrayFromCursor(Cursor cursor, int column) {
if (cursor == null) return null;
String[] strings = new String[cursor.getCount()];
if (cursor.moveToFirst()) {
int i=0;
do {
strings[i] = cursor.getString(column);
i++;
} while (cursor.moveToNext());
}
return strings;
}
Obviously the cursor isn't closed. This will leak a cursor. Logcat will give you that message.
SO, close it in the inner util function:
public static String[] createArrayFromCursor(Cursor cursor, int column) {
if (cursor == null) return null;
String[] strings = new String[cursor.getCount()];
if (cursor.moveToFirst()) {
int i=0;
do {
strings[i] = cursor.getString(column);
i++;
} while (cursor.moveToNext());
}
cursor.close();
return strings;
}
But logcat will still claim the cursor wasn't closed before finalize.
If instead, in the DB Helper method, I save the return value, close the cursor, then return it, I get no cursor leak/logcat message:
public String[] dumpColumnTable() {
Cursor cursor = cr.query(MY_URI,
new String[] { FIELD },
null,
null,
null
);
String[] toret = UtilMethods.createArrayFromCursor(cursor);
cursor.close();
return toret;
}
Why ? In debugging, the cursor is marked as close when the calls return. The call stack goes from my activity->db helper->util methods. The db helper and util methods are in separate projects from the activity.
Is there some pass by reference/value issue I'm missing, or crossing multiple JAR boundaries, or the casting of what is a SQLiteCursor to the generic Cursor type that I'm missing ?
I have a database manager class where i build a table that has two columns. the first column being an integer, and i am trying to write a method that will return all values in that column as a list:
public List<Integer> getAllStyleIDs(){
List<Integer> results = null;
SQLiteDatabase db = this.getReadableDatabase();
// Select All Query
String selectQuery = "select * from " + DBConstants.allStyles;
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
results.add(cursor.getInt(0));
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return results;
}
but i am getting a null point exception
any ideas?
Your list results is null, then you attempt to add to it. Try this:
List<Integer> results = new ArrayList<Integer>();
First thing: the list hasn't been initialized.
Second thing: using raw queries is not recommended:
Try something like this:
List<Integer> results = new ArrayList<Integer>();
this.database = this.getReadableDatabase();
String [] columns = {_ID}; // name of the column
Cursor cursor = this.database.query(COURSES_TABLE, columns, null, null, null, null, null);
int iId = cursor.getColumnIndex(_ID);
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext())
{
Integer rowId = cursor.getInt(iName);
results.add(rowId);
}
cursor.close();
this.database.close();
return results;
I have been trying to get all rows from the SQLite database. But I got only last row from the following codes.
FileChooser class:
public ArrayList<String> readFileFromSQLite() {
fileName = new ArrayList<String>();
fileSQLiteAdapter = new FileSQLiteAdapter(FileChooser.this);
fileSQLiteAdapter.openToRead();
cursor = fileSQLiteAdapter.queueAll();
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
fileName.add(cursor.getString(cursor.getColumnIndex(FileSQLiteAdapter.KEY_CONTENT1)));
} while (cursor.moveToNext());
}
cursor.close();
}
fileSQLiteAdapter.close();
return fileName;
}
FileSQLiteAdapter class:
public Cursor queueAll() {
String[] columns = new String[] { KEY_ID, KEY_CONTENT1 };
Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns, null,
null, null, null, null);
return cursor;
}
Please tell me where is my incorrect. Appreciate.
try:
Cursor cursor = db.rawQuery("select * from table",null);
AND for List<String>:
if (cursor.moveToFirst()) {
while (!cursor.isAfterLast()) {
String name = cursor.getString(cursor.getColumnIndex(countyname));
list.add(name);
cursor.moveToNext();
}
}
Using Android's built in method
If you want every column and every row, then just pass in null for the SQLiteDatabase column and selection parameters.
Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null, null);
More details
The other answers use rawQuery, but you can use Android's built in SQLiteDatabase. The documentation for query says that you can just pass in null to the selection parameter to get all the rows.
selection Passing null will return all rows for the given table.
And while you can also pass in null for the column parameter to get all of the columns (as in the one-liner above), it is better to only return the columns that you need. The documentation says
columns Passing null will return all columns, which is discouraged to prevent reading data from storage that isn't going to be used.
Example
SQLiteDatabase db = mHelper.getReadableDatabase();
String[] columns = {
MyDatabaseHelper.COLUMN_1,
MyDatabaseHelper.COLUMN_2,
MyDatabaseHelper.COLUMN_3};
String selection = null; // this will select all rows
Cursor cursor = db.query(MyDatabaseHelper.MY_TABLE, columns, selection,
null, null, null, null, null);
This is almost the same solution as the others, but I thought it might be good to look at different ways of achieving the same result and explain a little bit:
Probably you have the table name String variable initialized at the time you called the DBHandler so it would be something like;
private static final String MYDATABASE_TABLE = "anyTableName";
Then, wherever you are trying to retrieve all table rows;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from " + MYDATABASE_TABLE, null);
List<String> fileName = new ArrayList<>();
if (cursor.moveToFirst()){
fileName.add(cursor.getString(cursor.getColumnIndex(COLUMN_NAME)));
while(cursor.moveToNext()){
fileName.add(cursor.getString(cursor.getColumnIndex(COLUMN_NAME)));
}
}
cursor.close();
db.close();
Honestly, there are many ways about doing this,
I have been looking into the same problem! I think your problem is related to where you identify the variable that you use to populate the ArrayList that you return. If you define it inside the loop, then it will always reference the last row in the table in the database. In order to avoid this, you have to identify it outside the loop:
String name;
if (cursor.moveToFirst()) {
while (cursor.isAfterLast() == false) {
name = cursor.getString(cursor
.getColumnIndex(countyname));
list.add(name);
cursor.moveToNext();
}
}
Update queueAll() method as below:
public Cursor queueAll() {
String selectQuery = "SELECT * FROM " + MYDATABASE_TABLE;
Cursor cursor = sqLiteDatabase.rawQuery(selectQuery, null);
return cursor;
}
Update readFileFromSQLite() method as below:
public ArrayList<String> readFileFromSQLite() {
fileName = new ArrayList<String>();
fileSQLiteAdapter = new FileSQLiteAdapter(FileChooser.this);
fileSQLiteAdapter.openToRead();
cursor = fileSQLiteAdapter.queueAll();
if (cursor != null) {
if (cursor.moveToFirst()) {
do
{
String name = cursor.getString(cursor.getColumnIndex(FileSQLiteAdapter.KEY_CONTENT1));
fileName.add(name);
} while (cursor.moveToNext());
}
cursor.close();
}
fileSQLiteAdapter.close();
return fileName;
}
Cursor cursor = myDb.viewData();
if (cursor.moveToFirst()){
do {
String itemname=cursor.getString(cursor.getColumnIndex(myDb.col_2));
String price=cursor.getString(cursor.getColumnIndex(myDb.col_3));
String quantity=cursor.getString(cursor.getColumnIndex(myDb.col_4));
String table_no=cursor.getString(cursor.getColumnIndex(myDb.col_5));
}while (cursor.moveToNext());
}
cursor.requery();
public List<String> getAllData(String email)
{
db = this.getReadableDatabase();
String[] projection={email};
List<String> list=new ArrayList<>();
Cursor cursor = db.query(TABLE_USER, //Table to query
null, //columns to return
"user_email=?", //columns for the WHERE clause
projection, //The values for the WHERE clause
null, //group the rows
null, //filter by row groups
null);
// cursor.moveToFirst();
if (cursor.moveToFirst()) {
do {
list.add(cursor.getString(cursor.getColumnIndex("user_id")));
list.add(cursor.getString(cursor.getColumnIndex("user_name")));
list.add(cursor.getString(cursor.getColumnIndex("user_email")));
list.add(cursor.getString(cursor.getColumnIndex("user_password")));
// cursor.moveToNext();
} while (cursor.moveToNext());
}
return list;
}
a concise solution can be used for accessing the cursor rows.
while(cursor.isAfterLast)
{
cursor.getString(0)
cursor.getString(1)
}
These records can be manipulated with a loop
i want to get the value from the cursor without the SimpleCursorAdapter part.here is the code
public Cursor queueAll(){
String[] columns = new String[]{KEY_ID, KEY_CONTENT1, KEY_CONTENT2,KEY_CONTENT3};
Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns,
null, null, null, null, null);
return cursor;
}
and the activity side code
cursor = mySQLiteAdapter.queueAll();
from = new String[]{SQLiteAdapter.KEY_ID, SQLiteAdapter.KEY_CONTENT1, SQLiteAdapter.KEY_CONTENT2, SQLiteAdapter.KEY_CONTENT3};
int[] to = new int[]{R.id.id, R.id.text1, R.id.text2,R.id.text3};
cursorAdapter =
new SimpleCursorAdapter(this, R.layout.row, cursor, from, to);
listContent.setAdapter(cursorAdapter);
while(!cursor.isAfterLast())
{
String tilt = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.KEY_CONTENT1));
String pkg = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.KEY_CONTENT3));
if(tilt.equals("LEFT"))
{
Log.v("LEFT",pkg);
}
else if(tilt.equals("RIGHT"))
{
Log.v("RIGHT",pkg);
}
cursor.moveToNext();
}
i am getting the pkg value correct.but i want to get the values directly from the cursor while removing SimpleCursorAdapter part the code doesn't work.
any help would be appreciated :)
You can get values without declaring adapters. Adapters are need if you want to show the data in the list widgets. So your can be the following:
cursor = mySQLiteAdapter.queueAll();
if (cursor == null) {
//check if there are errors or query just return null
}
if (cursor.moveToFirst()) {
while(!cursor.isAfterLast())
{
String tilt = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.KEY_CONTENT1));
String pkg = cursor.getString(cursor.getColumnIndex(SQLiteAdapter.KEY_CONTENT3));
if(tilt.equals("LEFT"))
{
Log.v("LEFT",pkg);
}
else if(tilt.equals("RIGHT"))
{
Log.v("RIGHT",pkg);
}
cursor.moveToNext();
}
}
First you must count how many rows are in your table. Then assign that count to variable and it use to create an array. For array size you can use that count variable. Then create a for loop and use that count variable for rounds. After create your query and assign values to your arrays. 100% worked!.
int sqlcount;
Cursor mFetch;
SQLiteDatabase mydb = openOrCreateDatabase("your database name", MODE_PRIVATE, null);
mydb.execSQL("create table if not exists customer(name varchar,email varchar)");
Cursor mCount = mydb.rawQuery("select count(*) from customer", null);
mCount.moveToFirst();
sqlcount = mCount.getInt(0);
mCount.close();
String cname[] = new String[sqlcount];
String cemail[] = new String[sqlcount];
for(int i=0;i<sqlcount;i++) {
mFetch= mydb.rawQuery("select name,email from customer", null);
mFetch.moveToPosition(i);
cname[i] = mFetch.getString(0);
cemail[i] = mFetch.getString(1);
}
mFetch.close();
Toast.makeText(MainActivity.this,String.valueOf(cname[0]),Toast.LENGTH_SHORT).show();
Toast.makeText(MainActivity.this,String.valueOf(cemail[1]),Toast.LENGTH_SHORT).show();
what i want to do is do a search of my database for a string then find out what the row id is where that string is.
I thought by doing this
public void getRow(){
ContactDB db = new ContactDB(this);
db.open();
Cursor c = db.getId("1234567890");
String test = c.getString(c.getColumnIndex(db.PHONE_NUMBER));
Log.v("Contact", "Row ID: " + test);
db.close();
database class
public Cursor getId(String where){
return db.query(DATABASE_TABLE, new String[] {ID},where,null,null,null,null);
}
that it would give me what i want but i get a "cursor index out of bounds" error, how should i be doing this?
change getId to:
public Cursor getId(String where){
Cursor c = db.query(DATABASE_TABLE, new String[] {ID},where,null,null,null,null);
if (c != null) c.moveToFirst();
return c;
}
You need to do c.moveToFirst() before tying to read any information.
Also do c.close() when you're done.