Is there any way I can create and drop tables similar to a 'RawQuery'?
I tried with a #RawQuery annotation (which it would be the perfect solution for me) but when I am compiling I get an error saying methods annotated with RawQuery can't return void.
I read only SELECT, UPDATE and DELETE statements are allowed when using #Query.
I would like to achieve the "creation or deletion of tables" by passing a tablename as a parameter, something like the following:
#Query("DROP TABLE :name")
void deleteTable (String name);
Any ideas on how to achieve this?
Thanks!
Official doc states that,
RawQuery serves as an escape hatch where you can build your own SQL query at runtime but still use Room to convert it into
objects.
RawQuery methods must return a non-void type. If you want to execute a raw query that does not return any value, use
RoomDatabase#query methods.
or use it like,
#RawQuery
int deleteTable (SupportSQLiteQuery query); //We can return int status like it used to return with database.delete()
//Usage
dao.deleteTable(
new SimpleSQLiteQuery("DROP TABLE tablename")
)
The ting is, wit Room, you don't have to "drop" tables, the tables re created based on your entity classes (annotated with #Entity).
As far as I know, you usually need to drop tables in case the columns change or there are some updates on the "structure", with Room there's no point in doing this unless you change the structure of your entity that can't be automatically handled by the migration. In this case, Room gives you the chance to do the migration by yourself. Check the documentation here: https://developer.android.com/training/data-storage/room/migrating-db-versions
But like the documentation states, be really careful with this.
Related
I migrate my database from DBFlow to Room finally.
However, some queries I made for my old database don't really match what I know about Room. So in my Entity I have / had these calls:
"CREATE INDEX IF NOT EXISTS `idDeletedStartEndLocalIndex` ON `QUANTITY`(START_LOCAL, END_LOCAL DESC)"
I implemented that in Room as
Index("START_LOCAL", "END_LOCAL")
but how can I add the Descending at the index? Should I just write "END_LOCAL DESC"? Would that work as expected?
Same for this one
"CREATE UNIQUE INDEX IF NOT EXISTS `serverQtyId` ON QUANTITY(SERVER_QUANTITY_ID) WHERE SERVER_QUANTITY_ID > 0"
How can I add the WHERE SERVER_QUANTITY_ID > 0 clause to the Index annotation of room? Is that even possible?
Okay, looks like there are not many ways around this. So I did the queries manually.
For those who have the same problem, my code looks like this:
runBlocking(Dispatchers.IO){
with (getInstance().openHelper.writableDatabase) {
execSQL("CREATE INDEX IF NOT EXISTS `someIndexName` ON `QUANTITY`(...)")
...
}
}
So what I do is basically get the Database Instance and their openHelper. From there you can get the writable Database. It then supports executing SQL queries directly.
All this is run in the IO coroutine scope but that's just for convenience.
I would like to create fully dynamic where clause in Room.
Is there any way to make something like code below to work and return number of updated columns?
#Query("UPDATE table SET column = ${xyConstVal} WHERE :where")
fun updateTable(where:String):Int
Thanks.
I would like to create fully dynamic where clause in Room.
Instead of using #Query annotation in dao you could try using #RawQuery which accepts SupportSQLiteQuery as value then write your query in old fashioned way
Is there any way to make something like code below to work and return
number of updated columns?
A method, annotated with #Insert can return a long. This is the newly generated ID for the inserted row. A method, annotated with #Update can return an int. This is the number of updated rows.
In your case you can combine both
I hope to delete a row by _id, I hope to delete multiple rows by where args using Anko too.
I have read the article at https://github.com/Kotlin/anko/wiki/Anko-SQLite#updating-values, but I have no result, could you help me?
First off, using the update method is wrong. Deleting is not the same as updating. Updating in SQL means changing the value of one or more fields in a row. What you are looking for is the delete method
dbHelper.delete(TABLE_NAME, whereClause="Your actual where statement here", args)
The where statement follows a slightly different syntax than the regular where clauses in SQLite. This is an exception to the update method because it has a method for "regular" SQL through a builder. The delete method does not have this though, so you have to use the Anko syntax.
Essentially you can use this as a where clause:
"someRow = {someRowData} AND differentRow = {otherData}"
and in the arguments:
"someRowData" to "whatever value you want",
"otherData" to 1234
I'm not entirely sure whether or not the AND keyword will work, the syntax isn't fully documented. I assume it should because there's nothing else documented. The general SQLite syntax should be valid, and only the argument replacement (to avoid SQL injection I assume) is different
I am currently using greenDAO as the ORM for my Android Application. I ran into an issue when trying to execute a GROUPBY clause.
greenDAO does not have API / helper methods for performing groupby clauses, so I decided to use query() or queryRaw() methods available for the AbstractDAO class, where I can pass a valid SQL query. BUT, both these methods return a java.util.List, so what confuses me is that how can I get values of column aliases in the result?
Eg,
SELECT COUNT(ID) AS NUMOFRECORDS, NAME FROM PERSONS GROUP BY AGE
My entity will have NAME and AGE fields, but I created a column alias NUMOFRECORDS, which is not part of the Entity.
Appreciate your help!
This is a alternative solution for your question:
Include the ORDER BY insize of Where.
Ex:
List<Taxi> list = daoSession.getDaoTaxi().queryBuilder().where(new WhereCondition.StringCondition("1 GROUP BY cant_aciento")).list();
I'm stuck with a similar problem. It seems that greenDao doesn't support GROUP BY querys and it won't change in the future, according to what they said here:
GROUP BY is SQL-ish, so stick to SQL. greenDAO is about entities, where GROUP BY is unsupported.
I'm implementing a PRIVATE ContentProvider which has few tables with relationships (one to many, many to many). In my current implementation all of the tables are accessible by URIs.
how can I simplify the interface so the inner 'through' tables won't have to be accessed by URIs ?
for example, I have a POSTS table, each POST has many TAGS through the TAGGINGS table.
I want to interact only with the POSTS URI and do the 'private' work inside of the ContentProvider.
for query its simple to return a cursor with joined tables, but how do I do this with insert ? is bulkInsert what I should look into ?
It is a limitation of ContentProvider. If you are not exposing your data to other applications you can use your custom database adapter implementation with methods and queries straight hitting your requirements.
bulkInsert() won't help in this situation as it inserts rows only into one table at once. But take a look at ContentProvider.applyBatch() method and ContentProviderOperation, ContentProviderOperation.Builder classes (you may need withValueBackReference() for one-to-many inserting).
These links should help you understand how to use them:
http://www.grokkingandroid.com/better-performance-with-contentprovideroperation/
http://www.grokkingandroid.com/androids-contentprovideroperation-withbackreference-explained/
What are the semantics of withValueBackReference?
But notice, using ContentProviderOperation is much slower than bulkInsert() if you are inserting many rows at once, as it parses Uri (string comparisions) each time the operation is going to be performed. Doing this way you still have to expose Uri for inserting into child table.
If you decide to use applyBatch(), overwrite it in your provider so it performs all operations in one transaction, so you retain consistency in data and speed up database operations:
#Override
public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
throws OperationApplicationException {
final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
db.beginTransaction();
try {
ContentProviderResult[] results = super.applyBatch(operations);
db.setTransactionSuccessful();
return results;
} finally {
db.endTransaction();
}
}
You are free to insert to multiply tables as long as the values needed are provided.
For example:
ContentValues v = new ContentValues();
v.put("title","post1");
v.put("tag","tag1");
getProvider().insert(POST_URI,v);
In the implementation of insert, you could check if fields (tag) belongs to other table exists.If it does , it means that you should do extra works - insert tag first if it does not exist, set up correct association between the tag and the post just inserted.
You can check the source code of android contacts for the reference.
UPDATE:
To insert multiply tags, one hack-y way is to insert a comma separated string. THis is not elegant but it works.
Just to get this right: You want to have one URI and insert a post and all its tags with one insert call to the ContentProvider? Correct?
The problem is, that you need to have all values in the ContentValues object. There is a reason for normalization in database. Nevertheless it might be doable. For tags this should be easy. Just use one String for all tags. For example "android, ios, bada, wp7" and parse this string in your insert method.
You could also use a naming plus integer convention. And as long as there is a tag1, tag2,... tagX you would read these values from within your ContentProvider's insert method.
Neither is elegant, but would work.
In this case bulkInsert or applyBatch have no place in your code. They come only into play, if you want to use multiple calls to your ContentProvider at once and within one transaction.
But I think the better solution would indeed be to actually use multiple operations as described by biegleux.
Since you are going to be inserting into multiple tables the normal SQLiteDatabase.insert helper functions will not work. But this is completely doable in a performant and nice way.
You need to look at this from the endpoint of the user who is going to be inserting into you ContentProvider, even if it is only yourself. So first define the names or keys for all of you fields. Since you won't be using SQLiteDatabase.insert you don't actually need to name them the same as the database fields. None of the names should be duplicate. If for example you have fields in two different tables overlap perhaps tag in TableA and in TableB you could define the name for those field as TableA.tag and TableB.tag. Or use semantic naming for more descriptive names that don't collide.
Next you need to create your insert queries using SQLiteStatement per this answer. Make sure the names you use in createInsert are the same ones that the callers of the ContentProvider use as keys in the ContentValues.