Querying and working with Cursors in SQLite on Android

前端 未结 2 972
甜味超标
甜味超标 2020-12-12 16:02

Not sure if I\'m the only one who feels this...

I find working with the sqlite api in android a complete pain in the butt and pretty soul destroying. Has anyone got

2条回答
  •  情深已故
    2020-12-12 16:22

    1. Don't use model objects if you do not have to. I've concluded, unless there is significant business logic that can only be represented via model objects, that they are more trouble than they are worth in a mobile platform.
    2. Assuming you are stuck with model objects, have them load themselves out of a Cursor, rather than trying to pass in umpteen parameters.
    3. query() is much more verbose than rawQuery() for limited added value, if you know SQL.
    4. Assembling your CREATE TABLE clause via concatenation is self-imposed pain, not mandated by SQLite or Android.
    5. Don't use getColumnIndexOrThrow() from custom-written code. You wrote the query, so you know what order the columns are coming back in. Only use something like getColumnIndexOrThrow() if you are creating some abstract library that does not know the details of the Cursor it was given.
    6. String inherits from CharSequence, so all those casts can be dropped.

提交回复
热议问题