How to retrieve data from multiple Parse.com Tables/Classes - android

I have two tables (Classes):
StudentInformation: with columns RollNumber, address, name, school
StudentMarks : with columns RollNumber, Marks1, Marks2, Marks3
I've been able to save the data from a single form into these two simultaneously, but not getting a clue on how to put a query while retrieving into a listview or any other view something like
'return rows (from both tables together) where roll number = 1234' / 'return rows (from both tables together) where Marks2 > 50'
I'm using Parse.com backend for Android
Kindly help
Thanks

First, the UI aspect of showing in a ListView is provided by ParseQueryAdapter. https://parse.com/docs/android_guide#ui-queryadapter
Regarding the query, I do not think you can join tables in the way you want. Instead, you could create a pointer in StudentMarks to StudentInformation.
Then you can query something like:
ParseQuery<ParseObject> query = ParseQuery.getQuery("StudentMarks");
query.include('studentInformation'); // include the pointer to get StudentInformation
query.whereEqualTo("RollNumber", 1234);
query.whereGreaterThan("Marks2", 50);
... // perform query
In the results StudentInformation will be available like this:
List<ParseObject> objects; // the result from the query
ParseObject studentMark = objects.get(0); // example using first object
ParseObject studentInformation = studentMark.get("studentInformation");
String studentName = studentInformation.get("name");
String studentAddress = studentInformation.get("address");
... // etc
Alternatively you could also store a Relation of StudentMarks on StudentInformation, just to let you know that this is also an option, though I do not feel like it fits your current need as well as the solution presented above.

Related

get name from fom two table with foreignkey

Table -article-
id |nam|image|id_categor(FK)|
table category
id |categorieName|
I have two tables in the database. The first table has a foreign key to the second table. Can I get name from the second table by the foreign key ?
need a JSON like
"categories":[
{"categorieName":"test1",
"data":[
{
"name":"wallpaper1",
"image":"tv_101.jpg"
},
{
"name":"wallpaper2",
"image":"tv_102.jpg"
}
]
Welcome, You need to provide a better description with a better code so everyone can understand easily.
If I understand your problem properly then you need a join. You can use SQLite rawQuery to get the desired output.
For Example:
private final String JOIN_QUERY = "SELECT * FROM article a INNER JOIN categories b ON a.id=b.other_id WHERE b.property_id=?";
db.rawQuery(MY_QUERY, new String[]{String.valueOf(propertyId)});
EDIT: I still don't get your question but I think you are using PHP
First of all you cannot directly get the JSON from the database. What you need to do is:
Query your required data using PHP
SELECT * FROM categories then loop it in php and get all articles in each category by querying SELECT * FROM articles WHERE cate_id=loopCategoy_Id
And save it in a key value array
Then convert the array into JSON
Send JSON response
Did you try join ?
Joining two tables by common columns will definitely help here

How to get an specific relation in a Parse.com table/class (Android)

Been trying to figure this out with no avail, let's say each row in a table/class has several relations, how can I reference an specific relation from that row?... so I can do something with the third relation from row 1, for example.
Thanks.
You would just use something like this:
ParseRelation relation = ParseObject.getRelation("relationName");
ParseQuery query = relation.getQuery();
Then from there you can filter out your query results like:
query.whereEqualTo("Name", "Luchito");
And if I'm understanding your question correctly, you could then just take the list returned from query and compare it against another relation using whereEqualTo or whereContainedIn.

Android parse.com I can't get second user from data base

I have two tables in data base. One is user ad the second is transaction that have pointer to the first user, pointer to the second user and int. When I try make and display list I have error. I want to display all rows that CurrentUser is in first column:
ParseUser cu = ParseUser.getCurrentUser();
query.whereEqualTo("first", cu);
and list it with firstUser, secondUser and int:
from Adapter:
ParseObject taskObject = mTask.get(position);
ParseUser first = taskObject.getParseUser("first");
holder.firsthp.setText(first.getUsername());
ParseUser second = taskObject.getParseUser("second");
holder.secondhp.setText(second.getUsername()); //this line make error
int ile = taskObject.getInt("ile");
holder.taskhp.setText(Integer.toString(ile));
return convertView;
This is how transakcja table looks: http://i.stack.imgur.com/yh83p.png
When I saving transaction (when transaction table is clear, don't have any records) and immediately read it works but when I logout and login it crashes.
And here is entire code for toDoActivity Task Adapter and hplayout:
I had problems with pasting code here so I pasted it on pastebin
http://pastebin.com/2wtQLJXE
I think I know the problem. When you are calling your query on the transaction table, the two User's are Pointers to ParseObjects, or ParseUsers in this case. You need to ensure that these objects are FETCHED as well by the ParseQuery in order to properly access their data. They are ParseObjects with data from another table, Parse does not automatically retrieve them so you must tell Parse to do so when you need that data.
Looking at ParseQuery documentation for Android we find the include method.
public ParseQuery include(String key)
Include nested ParseObjects for the provided key.
You can use dot notation to specify which fields in the included object that are also fetched.
You want to use this to include columns names to Pointers of ParseObjects so the query fetches them at the same time as fetching the rest of the data from the table, in this case your transaction table.
Add this:
ParseQuery<ParseObject> query = ParseQuery.getQuery("transakcja");
query.whereEqualTo("first", cu);
query.addDescendingOrder("createdAt");
query.include("first");
query.include("second");
The reason first is having no null issues, is it is the Pointer to the current ParseUser logged in, which doesn't need to be fetched as it's data is accessible. The second one is not fetched, therefore adding the include to the query SHOULD fix this :). Make sure to also include the column "first" because I'm sure your future ParseQuery's will not always be between the current user and non-current second user

GreenDAO QueryBuilder dynamically add conditions

I currently have a list of userIds and I am trying to create a query to get all of those from my DB.
this is what I have in mind, I'm just not that sure that it's possible:
ArrayList<Users> listOfUsers = getCurrentUsers();
// lets assume that by now I have a list of users
QueryBuilder<Users> qb = getUsersDao().queryBuilder();
for(Users usr : listOfUsers) {
qb.where(Properties.userId.eq(usr.getUserId());
}
List result = qb.list();
I haven't seen any documentation about what is the right way of doing this and I want to know if this is the correct way of creating a dynamic query in GreenDAO.
EDIT:
I tried this and the result was a NullPointerException in the line of the declaration on the QueryBuilder
try using the IN query instead, it will run faster + you can cache your Query object.
so lets say you have
List<String> userIds;
you can get the list with:
qb.where(Properties.UserId.in(userIds))
if this is an operation that you do frequently, it is better to cache the Query. to do that, prepare the query as follows for only once:
Query<User> query = qb.where(Properties.UserId.in("?")).build();
then when you need to run it :
query.setParameter(0, userIds);
return query.list();

How to put String list in SQL database?

I have an edittext that allows the user to enter in numbers up to 10.
I want to be able to save this numbers to a SQLite database as a String list.
Does SQL allow this to be done? If so how?
Such as
String [] list = {1020300303,1020303001,0102003020};
You can do it, but you'll be breaking normalization rules.
If you persist that array as a single string (comma-delimited?), you'll have to parse it to look at individual values. Not a good idea.
A normalized schema would use a one-to-many relationship between two tables. One would be the parent, the other the child with one row per value and a primary/foreign key relationship. You'd get the values back using a JOIN.
If you're so intent on doing this, you'll have to concatenate all the Strings in the array into one, with some delimiter in between them that you're sure will never appear in any of the Strings you're combining. Once you have a single delimited String, INSERT it into a String-type column in a table in your SQLite database.
Since you insist on seeing some code, here's what it might look like in Java:
String [] list = { "1020300303", "1020303001", "0102003020" };
StringBuilder concatenatedList = new StringBuilder();
for (String s : list) {
concatenatedList.append(s).append('~'); // Any delimiter will do.
}
PreparedStatement ps = connection.prepareStatement("INSERT INTO MySQLiteTable(stringColumnName) VALUES(?)";
ps.setString(1, concatenatedList.toString());
int numRowsAffected = ps.executeUpdate();
I wouldn't write it this way - no cleanup, no error handling, no good encapsulation. But it shows where you want to go.

Categories

Resources