Using a db string as a query - android

I am making a list activity that will contain 'achievements'. Each achievement is a record in a sqlite db in the app. In each record, I have a column with a query string stored. For each record in the db, I am using the query string against another user generated db to determine which achievements have been accomplished...
The query strings I have been using are working correctly in my sqlite manager program... however in my app, it appears that the query is being ignored and returns the entire user generated db. I'm sure there is the potential for other general errors (like null query string returned etc) but I couldn't find any, and right now I don't have my code here to post.
Are there any pitfalls I am falling into by executing a query from a string extracted from the achievement db? This was the most straightforward way I could envision doing the achievements without a whole lot of if-then clauses.
EDIT: In the end I found an error in the call, passing the wrong argument. Pitfall in the end was working too bleary-eyed.

So basically your data is denormalised. This makes it harder to change, if you ever need to change the format for example. It will also be harder to do a variety of things with your data, e.g. query the number of people with a given achievement.

Related

How to query for Documents where the Fieldpath is always different

I have an App where there are Documents for each car. I somehow need to query the Documents in a Collection to give me the ones where a User has worked on some Tasks.
The Problem is however, the Fieldpath in each Document is different so I cant query with a static Fieldpath.
Here is the Example Structure of an Document, and yes I know it is one of the worst ways to do it but I already made it a long time ago.
So what would be the best way to get the worker Array Queried if the FieldPath is not static?
The Problem is however, the Fieldpath in each Document is different so I cant query with a static Fieldpath.
If you don't know the full path of a field value to query, you simply can't query it. There are no wildcards for document fields in a query. It sounds like you will have to change your document structure if you want to perform the query, or at least just duplicate some data into another field that lets you make the query you want. Duplicating data to satisfy queries is common in NoSQL type databases.

Android-Contacts and table joins not possible at-all?

Currently i'm developing a dialer-app for the Android OS.
It shows callogs, contacts in ListViews.
A SearchView is used to search contacts by: surname, name, nickname and all it's numbers. If the search string represents a substring of the four key-kinds, the list shall be shrinked to the matching entries.
i'm using CursorAdapter and CursorLoader to fill the list.
This is very important for the sake of performance.
Till that point everything is working and absolute clear.
Now the problem:
I need to join 2-3 tables from default contacts db (contacts2.db),
in one single sql query.
So my app should not use it's own, private db.
But existing ContentProviders do not offer acces to more than one table.
--> PROBLEM!
Implementing my own ContentProvider, it is possible to do joins utilising the SQLiteQueryBuilder.
But at a first glimpse that seems to work only for private databases.
When I try to directly access the contacts2.db, located in:
/data/data/com.android.providers.contacts/databases/contacts2.db
I get following error:
Failed to open database '/data/data/com.android.providers.contacts/databases/contacts2.db'.
android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (Sqlite code 14): Could not open database, (OS error - 13:Permission denied)
Which is somehow expected, because every app has it's own storage, using an appropriate uid in the underlying linux filesystem.
So, how can I get read-acces to this database?
Can I request that db-object somehow from the android system or utilizing the Context object? Or can I gain those acces-rights somehow?
Would it be possible to use the same AUTHOTITY_NAME as the contacts App does?? Like this:
com.android.providers.contacts
I'm not interested in joining columns using java code, that's UGLY, EXTREMELY SLOW, and I need a Cursor instead of a List or similar stuff anyway
I don't need help in SQL
I don't need help in how to implement android stuff
All you Android-Pros out there - is that possible at-all?
Or is the android api really that crappy?
Do i really have to "join" the needed info by using CursorJoiner and generate
somehow a Cursor object from the first one?
Thank you very much in advance, folks!
You don't. You can't even assume that's the name of the contact db on a given device. The only way to access the contacts db across all devices is to use the ContentResolver.
It sounds like you're looking for Match a Contact By Any Type of Data as shown by the Retrieving a List of Contacts official Android lesson.
You'll need to follow all 4 steps:
Request Permission to Read the Provider
Match a Contact by Name and List the Results
Match a Contact By a Specific Type of Data
Match a Contact By Any Type of Data
Because the 4 step is based on the code created by the previous steps.
Basically, it's using a built-in search feature in Android's Contacts framework: Contacts.CONTENT_FILTER_URI:
Use Contacts.CONTENT_FILTER_URI as the base URI, and append your
search string to it by calling Uri.withAppendedPath(). Using this URI
automatically triggers searching for any data type

User downloads all events, then favourites a few

This is a theoretical question. I'm creating an app which downloads a list of events around the city from a MySQL DB and displays them in a RecyclerView.
Users should be able to select an event and add to their own list of events they are interested in. At that point, I'm not sure what are the best practices.
So when a user selects an event item from the RecyclerView, what's the best solution, I can only think of that:
Method 1: Add the chosen event to a List<Event> which is then saved in SharedPreferences as a JSON string. Upload the list to the online MySQL DB at a later point.
Any other suggestions?
About the query:
Also, could you give me a pointer on how to do the complex query to the database.
I have these tables Events,Accounts, GuestList. So, GuestList holds the ID of the event and of the account so that I keep track of which events a specific user wants to attend. I'm guessing I'd have to use some kind of JOIN?
You can see that if I want to get the data to display (the event information only for the user who is requesting it) I'd need to first query the GuestList table to get the list of events. Then query the Events table to get the information for all the events with the IDs we grabbed a moment ago. An example SQL statement anyone?
When user select event from your RecyclerView that he wants to attend, you should pass a query to the database storing the event to the user’s private event list. The optimal way is doing it asynchronously…
It seems that you have many to many relationship between your models.
Events, Account and the middle table is GuestList that stores the relations between your models. The best practice is to name it Events_Account and this convension means that this is the connection table.
The examples I will show are on MS SQL but they are pretty much the same and the concept is same too.
So basing on your models description the relations should be looking as something like that.
You can select your data using a basic query like this
When a user wants to grab info for a particular event you can simply add another where clause like this
You can pass a parameters in your method and do it with a pure query. Let's pretend that these are your variables #CurrentUserId and #Particular event. You can pass a values into the method and use them to select your item
This is the WORST practice – to use it as a pure query. The PHP as other languages are giving you prerequisites to shoot yourself in the leg. Using a pure db query in your code and relaying that the user will use properly the application is totally wrong. This way the user can simply send a SQL injection and dump your database. Here comes the ORM /Object Relational Mapping/ Like EntityFramework for .NET. The PHP equivalent is Propelorm. It maps your database and makes you access to the object very easy with the fluent syntax.
The query and also the ORM that is very simpler to use and saves you from SQL injection will both be transpiled into SQL language. The query that they will execute in SQL will be close to the one on the pictures but a bit uglier for a human. Don't worry about the query it will be very fact because this operations are executed on the database level and they will be very fast.
I hope that this will be useful and enough for you and solves your theoretical problem

Adding latest record at the top of Azure Sql table

Iam following this tutorial :- http://azure.microsoft.com/en-us/documentation/articles/partner-xamarin-mobile-services-android-get-started/
to use mobile services with Azure.
Currently when I add items to the table, the data gets stored randomly. I want it to store the items in a such a manner that the latest record comes at the top of the list.
How can I do it?
Thanks
From the looks of that tutorial, the table in the database which has been created is using a UniqueIdentifier (equivalent to a GUID in code) as the Id. (presumably PK).
GUIDs are generated "randomly" and don't sort nicely in chronologically created order. It's not that the data is getting stored randomly. The Guid is randomly generated, and the table/index stores it in exactly the correct place based on it's Id value.
At the end of the day, for such a small amount of information, the order it's stored in the table isn't really important so long as you can retrieve it in the order you want.
From the looks of the demo, the data table also has a _createdAt column on each data entity. Have a look in the TodoItem & TodoAdapter code to find where it actually executes the query against the data store. It should be quite easy to add an OrderBy to that call to force the returned order to sort by _createdAt

store and retrieve Vector in Android sqlite database

I need to store an retrieve a vector of an unknown number of objects in an android sqlite database.
Essentially, the setup is this: I am developing a task management app, where the user can add as many notes as they like to their tasks. My current setup uses one database, with one row per task. This presents a problem when I need to associate multiple notes and their associated information with one task. I can see two approaches: try to store an array of notes or a vector or something as a BLOB in the task's row, or have another notes database in which each row contains a note and it's info, as well the id of the task which the note belongs to. This seems a little easier to implement, as all I would have to do to retrieve the data would be to get a cursor of all notes matching a particular id and then iterate through that to display them to the user. However, it seems a little inefficient to have a whole new database just for notes, and it makes syncing and deleting notes a little more difficult as well.
What do you think? Is it worth it to have a separate notes database? Should I use a BLOB or go for the separate database? If a BLOB, are there any good tutorials out there for storing and retrieving objects as BLOBs?
It sounds like you need another table in your database (not another database). You already have a table for Tasks. Now make one for Notes. Make a column be a foreign key into the Tasks table. That is, Notes.Task_ID would hold the ID of the Task that the Note is for. Then when you want to get all of the notes for a task, query the Notes table.
I think the answer to this question really lies in how you're going to go about updating things should they change. For now, the BLOB route probably seems like a really good idea, but what happens if you want to add some new functionality and you want to store some new property of notes (think of things like starred or importance). What would you need to do in order to update the notes object to add this new field? If it's just a database table, it's quite easy to change the layout of the table and even add a default value. If it's a BLOB, you're going to need to go through each entry, de-serialize the BLOB object, fix it, and re-serialize. That could get tricky.
Also, and this probably isn't as important to a small application using an embedded database, but it's easier to modify the database outside of the application if the object isn't a BLOB. Not to mention the queries you'll be able to write with the separate table. For example, how might someone calculate the number of notes that are attached to a task? If it's separated out in the database, it's a simple query.
Just my two cents.

Categories

Resources