I've been racking my brain on this for days and I just can't wrap my head around using SQLite databases in Android/Java. I'm trying to select two rows from a SQLite database into a ListArray (or two, one for each row. Not sure if that would be better or worse) and I just don't understand how to do it. I've tried various database manager classes that I've found but none of them do what I need and it seems that I should be able to do this simple task without the extra features I've seen in other database managers. Is there any simple way to JUST query some data from an existing SQLite database and get it into a ListArray so that I can work with it? I realize I need to copy the database from assets into the Android database path and I can handle that part. I also need to be able to modify one of the columns per row. I don't need to create databases or tables or rows. I implore someone to help me with this as I consider any code I've written (copied from the internet) to be completely useless.
5
-
What data/information are you trying to access, exactly? Is it something you've input into a database yourself, or are you trying to access information from an Android database such as contacts or call logs? If you are trying to access Android databases, you are generally advised to use a ContentProvider. This provides a layer of abstraction and allows Android to handle the database transactions.Codeman– Codeman2011-07-27 16:07:13 +00:00Commented Jul 27, 2011 at 16:07
-
it's a database that i've provided myself.Carnivoris– Carnivoris2011-07-27 16:08:43 +00:00Commented Jul 27, 2011 at 16:08
-
could you provide some source code that you have used so we could help debug?Suchi– Suchi2011-07-27 16:25:20 +00:00Commented Jul 27, 2011 at 16:25
-
I don't have any code that I feel comfortable with. I can't find any examples of just selecting some data and working with it. I suppose most of the people posting code assumes that people are familiar with how to use that code but I simply don't understand it. I come from PHP and C#, and both those languages have very simple methods for accessing databases. It seems that Java does not. Is there no simple way of just querying a database and putting data from a select into a ListArray?Carnivoris– Carnivoris2011-07-27 16:31:08 +00:00Commented Jul 27, 2011 at 16:31
-
you could use myDB.rawQuery() and get back a cursor. you could then use cursor.getString(YOUR_COLUMN_NAME) and add it to your array.Suchi– Suchi2011-07-27 16:42:40 +00:00Commented Jul 27, 2011 at 16:42
Add a comment
|
1 Answer
You can create a method like this :
private List<MyItem> getAllItems()
List<MyItem> itemsList = new ArrayList<MyItem>();
Cursor cursor = null;
try {
//get all rows
cursor = mDatabase.query(MY_TABLE, null, null, null, null,
null, null);
if (cursor.moveToFirst()) {
do {
MyItem c = new MyItem();
c.setId(cursor.getInt(ID_COLUMN));
c.setName(cursor.getString(NAME_COLUMN));
itemsList.add(c);
} while (cursor.moveToNext());
}
} catch (SQLiteException e) {
e.printStackTrace();
} finally {
cursor.close();
}
return itemsList;
}
This will be inside your class let say MyDatabaseHelper where you will also have to declare a :
private static class DatabaseHelper extends SQLiteOpenHelper{
private final static String DATABASE_CREATE="create table " + MY_TABLE + " (id integer primary key, country string);";
public DatabaseHelper(Context context,String name, CursorFactory factory, int version){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db){
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion){
Log.w(TAG, "Upgrading database from version " + oldVersion
+ " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS "+ MY_TABLE);
onCreate(db);
}
}
used to open() and close() the database.
5 Comments
Codeman
The other option (that I typically use) is to store these values in a hashmap, storing the _ID as the value and my "searchable term" in the key so I can find everything I need easily with one query, rather than making several database transactions occur (causing extreme slowdown)
Carnivoris
Well, your database helper class doesn't help me at all. It assumes that it's creating the database at runtime when i'm importing an existing database from assets but I think I can work around that. The second thing I don't understand about this code is the query.
cursor = mDatabase.query(MY_TABLE, null, null, null, null, null, null); Is this just basically doing a SELECT * FROM MY_TABLE?Carnivoris
i'm still trying to figure out how to make sense of the docs, to be honest.
Carnivoris
i give up. the list code there doesn't work and i can't make heads nor tails of the docs to figure out how to make it work. i'll just see what i can do in xml to make this work and leave out the feature that required sqlite. i'll try to figure this out at a later time and add that as an update to the app.
502_Geek
@Ovidiu Latcu Where MyItem come?