2
\$\begingroup\$

I am developing an Android native game, but I have problems with Data Storage. For a better comprehension think it like as i am developing Clash of Clans, but with Native Andorid.

Now imagine I have to store all datas for Buildings, moneys, troups, ecc in my local phone DB.

I read some guides about SQLite for Android, but i don't really like it. It picks always a Cursor object and i have to create an "Helper" class for each table so I can convert the Cursor into the object I picked.

My question is: is there a way to do it in a "easier" way but still complete as SQLite?

I learned SugarORM and it is really simple and intuitive, but it has no possibilities for ID and a lot of other limitations.

I want a library or anything else that allows you to do like windows phone c#:

SQLiteConnection conn = new SQLiteConnection(DBPath);
return conn.Query<TABLE_NAME>(SQL_QUERY);
//in this way i have a list of TABLE_NAME items without any casting or similar.

Is there a way to do it?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

I tried it all and the best way is the cursor/helper way. Its much simpler than using any framework.

Just create a helper class to manage all db interactions.

Persisting data is as easy as this:

ContentValues cv = new ContentValues();
cv.put("column1", "some value");
db.insert("mytable", null, cv);

And fetching from db is like this:

Cursor c = db.rawQuery("select * from mytable where column1 = ?", new String[]{"some value"});

c.moveToPosition(-1);
while (c.moveToNext()) {
    String userId = c.getString(c.getColumnIndex("userId"));
}
c.close();

Thats it, it doesn't get any easier.

You of course can make these queries not in a helper method.

Also make sure to invoke all db writes/fetches not from the UI thread.

You can read more about in my blog posts: http://tocrva.blogspot.com/2015/04/android-sqlite-made-easy-tutorial.html http://tocrva.blogspot.com/2015/08/android-sqlite-best-practices-how-to.html

\$\endgroup\$
8
  • \$\begingroup\$ Thanks, unfortunatly the SQLite management in Android isn't the most intuitive and easy to do \$\endgroup\$ Commented Dec 12, 2016 at 8:35
  • \$\begingroup\$ Once you get into it is very easy! \$\endgroup\$
    – lxknvlk
    Commented Dec 12, 2016 at 11:37
  • \$\begingroup\$ Yeah, You are right, the problem is that it's really really long and annoying. That's why I developed a generic handler which does everything for me for any object :) \$\endgroup\$ Commented Dec 12, 2016 at 11:42
  • \$\begingroup\$ @PierGiorgioMisley, you got me interested, could you share it? \$\endgroup\$
    – lxknvlk
    Commented Dec 13, 2016 at 12:35
  • \$\begingroup\$ Sure, I posted it in this question \$\endgroup\$ Commented Dec 13, 2016 at 13:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .