Querying and working with Cursors in SQLite on Android

前端 未结 2 936
甜味超标
甜味超标 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.
    0 讨论(0)
  • 2020-12-12 16:26

    I tested a lot of my SQL in SQLite before copying them over to Android as strings. It's easier for me to debug when I can directly interact with the command line.

    Another technique I use is saving as much of my queries as possible as string constants or string resources.

    You also don't need SQL such as INTEGER NOT NULL since SQLite uses duck typing/manifest typing. It does help for type affinity though..

    0 讨论(0)
提交回复
热议问题