The ContentValues class contains a method that allows Booleans to be put into the values collection. AFAIK, SQLite does not contain a native Boolean format that Android could push the boolean values into. So, what magic does Android do behind the scenes to store these values?
Also, why is there no complimentary getBoolean method on a Cursor? To me, this appears to be a pretty awkward design oversight since there seems to be no "safe" way of retrieving a boolean value that was put into the DB via ContentValues. What am I missing?
This question may seem a bit frivolous since I suspect that the boolean's are stored as a 1 or 0 integer, but why would Android commit to developers making that assumption? Its not even documented as far as I am aware.
The ContentValues class contains a
method that allows Booleans to be put
into the values collection. AFAIK,
SQLite does not contain a native
Boolean format that Android could push
the boolean values into. So, what
magic does Android do behind the
scenes to store these values?
From reading this document, it sounds like the boolean to integer conversion is done by SQLite.
Also, why is there no complimentary
getBoolean method on a Cursor? To me,
this appears to be a pretty awful
design oversight since there seems to
be no "safe" way of retrieving a
boolean value that was put into the DB
via ContentValues. What am I missing?
If you're reading from a cursor, then you know what columns should be returned from the query, so you presumably know the data types of the columns that were requested. I agree that having a getBoolean method would be better, but it's not hard to work around.
UPDATE
Google has patched the previously mentioned bug, though it's not implemented yet at the time of this post:
https://code.google.com/p/android/issues/detail?id=232274
It's worth noting that the current API is dangerous and could break your app if anything changes under the hood.
Additionally, ContentValues.getBoolean has a major issue that if you create the ContentValues with DatabaseUtils.cursorRowToContentValues it will treat EVERY field as a string:
values.put(columns[i], cursor.getString(i));
When you then retrieve the field via ContentValues.getBoolean you will ALWAYS get false:
if (value instanceof CharSequence) {
return Boolean.valueOf(value.toString());
Since value is "0" or "1" this conversion fails:
private static boolean toBoolean(String name) {
return ((name != null) && name.equalsIgnoreCase("true"));
So I highly recommend that you create your own getter and setter so your behavior is well defined.
Related
I was wondering what the difference between database.getReference("foo/bar/123") and database.getReference("foo").child("bar").child("123") is?
I'm assuming that the later one will load the complete "foo" object whereas database.getReference("foo/bar/123") just loads the "123" object?
Is my assumption correct or what is the correct / most efficient way to only load data of "123"?
The two are equivalent. You can inspect this manually this by printing the toString() format for both References.
References are cheap - there's nothing inefficient about either solution. Neither one has yet loaded any data. A Reference is just a pointer to a location in the database.
It should not make a difference, a reference is not actually accessed when instantiated. This is the most relevant document I can find,
https://firebase.google.com/docs/reference/node/firebase.database.Reference
The docs don't say it explicitly, but requests are only performed when using the .set() or .on() methods
When using copyToRealmOrUpdate it also overrides fields with existing values. I would expect it would only update the fields I gave and use the existing values for the other fields.
I saw this issue for createOrUpdateFromJson: https://github.com/realm/realm-java/issues/933
cmelchior says this:
It is impossible to tell the difference between an value not set and
it's default value, so there it should override all properties.
I wanted to create an issue with label enhancement for realm, but instead I ask it here first. Is it really impossible? Because it would be a great improvement to me.
Thanks!
Note there is difference between using Realm.copyToRealmOrupdate(RealmObject) and Realm.createOrUpdateFromJson(Json)
The answer I gave is true for copyToRealmOrUpdate() eg. you cannot tell the difference between the following in Java:
boolean bool1;
boolean bool2 = false;
It is different for JSON where you can tell if a property is missing altogether. However the current implementation doesn't work that way. We are currently in process of merging a Pull Request that actually has the behaviour you are looking for. You can follow the progress here: https://github.com/realm/realm-java/pull/1022
I'm looking into using greenDAO for my Android app, but I noticed it doesn't seem to support any kind of data validation other than "not null", "unique", and foreign keys, either on the SQL level (constraints defined when creating tables) or the Java level (validation logic in setter methods). "Keep sections" don't seem like they would be helpful in this case because you can't have them within individual methods. Am I missing something, or would I really need to add yet another layer on top of the generated Java objects if I wanted to validate input data? (I'm somewhat confused how the framework could be useful without providing any place to include validation logic.)
1.
You can write a method
boolean check ();
in KEEP-SECTION of the entity which you call manually before INSERT or UPDATE.
2.
Another possibility is to extend the sourcecode of greendao generator to support checks: In property.java you could add a method to Property.Builder
public Property.Builder check (String expr) {
property.checkConditon = expr;
}
Of course you would have to introduce the String checkCondition = ""; and use it for generating the dao in the dao-template.
Problem:
With new versions of greendao your changes would be lost (but then again new version may already contain such a feature)
3.
A third possibility is to copy the generated CREATE TABLE statement, modify it to fit your needs and call your modified statement instead of the original one or to drop the original table and call your statement.
Problem:
If your table changes you will have to repeat this.
I made an android game and I want to know how to save player data such as money, level reached, etc. I think that XML is not a really convenient way to do this. Is there any good way to do this? Does android provide some method to save the data?
A database is probably overkill for the type of data you need to track. I would suggest using Android's SharedPreferences APIs. See here for a tutorial.
Here's an official overview of Android data persistence methods including SharedPreferences and SQLite.
Check out the getStringSet API for storage of arrays/collections.
For non-existent vals for the string set:
Returns the preference values if they
exist, or defValues. Throws
ClassCastException if there is a
preference with this name that is not
a Set.
For non-existent value data such as string, bool, float etc.:
Returns the preference value if it
exists, or defValue. Throws
ClassCastException if there is a
preference with this name that is not
a String.
Use a SQlite database. ( Data storage on developers.android )
Here is a tutorial that shows the functionality:
http://p-xr.com/android-tutorial-simple-but-persistent-data-storage/
hi i have found Uri as immutable reference i dont know what it is the exact meaning of immutable reference... can anyone help me?
It's a variable that cannot be changed once set. Very useful when you have multithreaded code since being able to change a variable's value might be a source of many hard to find problems in your code.
If it's immutable, it's usually good.
A good example of an immutable class within the .NET Framework is System.String. Once you create a String object, you can’t ever change it. There’s no way around it; that’s the way the class is designed. You can create copies, and those copies can be modified forms of the original, but you simply cannot change the original instance for as long as it lives, without resorting to unsafe code. If you understand that, you’re probably starting to get the gist of where I’m going here: For a referencebased object to be passed into a method, such that the client can be guaranteed that it won’t change during the method call, it must itself be immutable.
In a world such as the CLR where objects are held by reference by default, this notion of immutability becomes very important. Let’s suppose that System.String was mutable, and let’s suppose you could write a method such as the following fictitious method:
public void PrintString( string theString )
{
// Assuming following line does not create a new
// instance of String but modifies theString
theString += ": there, I printed it!";
Console.WriteLine( theString );
}
Imagine the callers’ dismay when they get further along in the code that called this method and now their string has this extra stuff appended onto the end of it. That’s what could happen if System. String were mutable. You can see that String’s immutability exists for a reason, and maybe you should consider adding the same capability to your design.
EX: string is immutable...
if u have for ex string s =" whatever" and u output it with uppercase letter..for ex
Console.Write(s.ToUpper())the console will print u WHATEVER...but the string s will still be whatever... unlike the mutable type which will change the string from whatever to WHATEVER
"immutable" means "can't change the value"
"mutable" == "changeable"
"immutable" == "not changeable"
In java , every thing is treated as String and object , Now try to think that if have created a program of 10000 lines and in this there you have added "public" 100 times so do you think that every time this public is created in storage . else what we can do , we can created something like that when ever we find something like this we will fetch it from there there ( String pool )