Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite for Android custom table view (SQL VIEW, not Android view) discrepancy?

I've created a custom view in an SQLite database for an Android application.
I'm using Sqliteman on Ubuntu to test my SQL statements before I put them in my app.
I'm trying to do a simple select statement on my view.
The select statement works fine in SQLiteman but when I put the same statement in my code it throws an error.

The statement:

select * from item_view where parent_item_id = 0;

The view (converted to Java as a String):

"create view if not exists item_view as select " +
    "item._id, item.status, item.name, item.position, " +
    "item.parent_item_id, item.note_id, item.other_id, " + 
    "note.contents, other.priority " +
"from item, note, other where item.note_id = note._id and item.other_id = other._id"

The error:

07-16 14:21:15.153: ERROR/AndroidRuntime(5054): Caused by: android.database.sqlite.SQLiteException: no such column: parent_item_id: , while compiling: SELECT * FROM item_view WHERE parent_item_id = 0

I first tried calling the field item.parent_item_id in my select statement, but that didn't work.
Then I pulled the db and opened it with Sqliteman.
The fields were listed as they were in the original tables (_id, status, name, etc.)
So I ran the SQL above in Sqliteman and was able to retrieve the appropriate data no problem but I can't get it to work in my app either way.
I also noticed that dropping the view as a DROP TABLE command worked in SQLiteman but not in my app.
I'm wondering if I'm maybe missing some other VIEW specific functionality.
I didn't see any in either the android documentation or any SQL documentation though.

Technically I could just do this with a more complex SQL call using the original tables, but all my tables follow certain guidelines so that I can dynamically generate SQL calls. I'd like to have the view tables work to keep my code uniform and always reusing the same calls to avoid maintenance and other buggy issues from duplicate code.

like image 674
leetheguy Avatar asked Jul 16 '10 22:07

leetheguy


People also ask

How is SQLite different from other database approach in Android?

SQLite is file-based. It is different from other SQL databases because unlike most other SQL databases, SQLite does not have separate server process. Main components of SQL are Data Definition Language(DDL), Data Manipulation Language(DML), Data Control Language(DCL).

Is it practical to use an SQLite database for Android studio?

Whenever an application needs to store large amount of data then using sqlite is more preferable than other repository system like SharedPreferences or saving data in files. Android has built in SQLite database implementation. It is available locally over the device(mobile & tablet) and contain data in text format.

Can SQL be used in Android Studio?

The Android SDK includes a sqlite3 shell tool that allows you to browse table contents, run SQL commands, and perform other useful functions on SQLite databases.


1 Answers

It turns out that you need to specifically name each field you're creating so that android can treat it like a regular table. The solution...

"create view if not exists item_view as select " +
    "item._id as _id, item.status as item_status, item.name as item_name, item.position as item_position, " +
    "item.parent_item_id as item_parent_item_id, item.note_id as item_note_id, item.other_id as item_other_id, " + 
    "note.contents as note_contents, other.priority as other_priority " +
"from item, note, other where item.note_id = note._id and item.other_id = other._id"
like image 163
leetheguy Avatar answered Sep 20 '22 23:09

leetheguy