ContentProvider How call method - android

I have a contentprovider, which i am using. Within the contentProvider, I call a method in oncreate(), which sets a lot of default values for the content provider.
I am using the content provider by another table for each entry. So, when I create a new entry in the other table, I would like to override the settings of the content provider. How can I call the method within the contentProvider, which sets default values? Since the onCreate is not called again, I do not get the default settings.
I also thought about the following approach, but this seems to be difficult as well.
Is it possible to generically copy all columns of a row and change only one column? I want to specify the column, which should be replaced, not all of the other ones...
As I have mentioned, I want to insert default values, so I am not planning to copy too much rows, which would be bad database design.
Example Table MyFavorites blob bla, String link, ....., String nameToReplace, ...
Lets say I have 20 columns. I want to copy anything, and I just want to change the nametoReplace.
Best Regards,
Patrick

Related

What is the right way of checking conditions in content providers?

I have sqlite database and content provider which wraps it. There is the table of dictionaries and the table of words. Eeach word belongs to one of dictionaries. Also each dictionariy has constant capacity. So my content provider should allow to insert only limited amount of words in each dictionary.
"scheme"
Dictionaries
|-id (read-only)
|-capacity
Words
|-id (read-only)
|-dictionaryId (write-once)
I have a few options:
1) For each new dictionary I can create trigger that will raise() error if amount of words is gather than capacity. But it will make query for each insertion that is redurant in some situations.
2) I can check this condition in the provider's insert(). (same problem as above)
3) I can pass this check to users of provider. For example check the condition in the activity which adds new words into dictionary. This is most optimized method because I don't need to make query each time I add new word. I can query the amount of words in the dictionary at the start of activity and then increment it and be aware of the relevant value without queries. But here I have another problem: what if i forget to check this condition or make mistake and condition won't work.
So what is the right way of checking conditions in content providers?
I've decided to extract all logic of working with data from database and use db just like mere container. The reason is that it frees me from doing tests for database that is not easy as I see. The better I think is to consider that database is something robust and based on that create tests for application components.

What is the use of '_id' in ContentUris.withAppendedId

I have been struggling with the update of only 1 row in an SQlite databasetable.
Although I provided the _id value of the row that I wanted to update in the ContentUris.withAppendedId Uri, I still have to check in the 'WHERE'-part of the update statement whether the id of the record is the same.
When I leave the 'WHERE'-part of the update statement to 'null', the update statement tries to update ALL rows of the table instead of only the row with the id that was provided.
URIs define a resource. They do not define what operations can be made on that resource.
With regards to a ContentProvider (since you've included that in the tags for your question), you need to consider that the app with the ContentProvider and the app that wishes to access or modify its data may not be the same app. If you are implementing a ContentProvider, you would need to recognize URI of the kind you mentioned and adjust the WHERE clause accordingly, assuming you support that operation on that resource.
The other use for this is with insert operations, where the typical thing to return is a URI for the item that was just inserted. E.g. a successful insert on the URI content://authority/directory would return something like content://authority/directory/_id, where _id is the ID of the newly inserted row.

Using sqlite to dynamically create tables in android

So my fundamentals of creating and manipulating databases are a bit messed up. My aim here is that whenever the app is launched, the user is allowed to specify a table name, and whatever data is then collected is put into that table.
However, I'm confused as to how to do this. Do I simply pass the value of a user entered variable as the table name in my contentprovider class and execute sqlite statements to create it?
I've read/reading the documentation already, so if anyone has any insight or clarity, or even better, code snippets, it would be great.
Why not simply use one table, and create a value that stands for the current app-session, and insert that value with each row. This would make your code simpler, and would still allow you to segregate/filter out the values from a particular app-session. If you want to give the user the ability to enter the value (as you are giving them the ability to choose the table name) you'd just want to check to see if that value had already been used, just as you would have to see if the table-name had already been used.

Content Provider filtering query, filtering Cursor

I got following problem, I need to use a Content Provider to read a
Database of an other App.
first I want all rows, and after analyzing the data only e.g. the rows
from _id = 1, 3 and 5.
how can I call a Content provider and select only these rows?
or is it possible to create a subset Cursor form an given Cursor?
Thanks in advance.
If you're talking to another app, I assume you're querying the other app's ContentProvider to get the data from them in the first place.
In this situation, the cleanest answer seems not to build your own ContentProvider that filters/wraps theirs. Instead query their ContentProvider from your application directly, and use the select clause in your query() to specify the conditions that define the subset of data you want to be given.

Android Widgets: Where would the 'insert' step for a database occur?

I have a widget that currently takes a random string from an array and sets it to text view on update. The issue here is that the same item can be re-used multiple times in a row due to the string being 'random'
In order to solve this I was going to create a table that held String text, and int viewednum and increment the viewed number each time 'get text' was called. (on update in the widget).
My Question: If I put the insert statements in the widget, won't the data be inserted every time 'on update' is called?
Would it be better for it to go in the DBadapter class somewhere? I'm just unsure about the best way to make sure I don't enter duplicate data. If there is a better alternative like saving a csv file somewhere and using that I'm open to it, it seemed like a sqlite database was the way to go.
Thank you for your time.
That depends on what your onUpdate method does. If each time onUpdate is called it gets a random String from the database, then that would be the place to put it. However, if you are not getting the String during onUpdate, then you should put it in the method where you are accessing your database. I think your confusion is about the purpose of onUpdate. onUpdate doesn't get called every time the user scrolls by the homepage and sees your widget; it gets called regularly on a timescale you specify, and the whole purpose of it is, in a case like yours, to get a new String from the database.
As for your second question, yes, SQlite databases are the way to do it :) I haven't tried saving a csv file or something like that, but I imagine that would be a lot more complex than just using a database.
Declare your database with a UNIQUE constraint on the columns you want to keep unique, then set the desired behaviour via ON CONFLICT in the INSERT statement. ON CONFLICT REPLACE... means the most recent INSERT overwrites. ON CONFLICT IGNORE... keeps the older version.

Categories

Resources