I want to copy a premaded database in user's device. then use it in the android App.
but I don't know where is the best place for that. (a place that I can copy file to it)
for example:
I have a database named "FoodsDB.db" in assets folder
When application starts for first time, it copies the DB from assets to user's Device
Then in DatabaseHelper address it and use it
Right off the bat without knowing exactly your code I'd try and guess that when you say "I don't want to submit changes" means you shouldn't invoke SaveChanges(). Shouldn't you try to completely discard the entities, then? I doubt it's that simple, but I post this nonetheless.
After you've added the example code I can see it's different then I thought. You won't be able to use a rollback (see unit of work pattern). What you need here is a PARTIAL undo, which is different then the full-scale undo I initially proposed by throwing away the entire context.
Look here for an excellent solution to your problem:
Entity Framework .Remove() vs. .DeleteObject()
Related
I am very new to ORMs and presently trying to implement green Dao to manage by DB operations.
I want few good examples to work upon to understand its working but it seems I get stuck after DaoGeneration.
After this process I am able to see my generator files inside the desired directory. Can anyone tell me what to be done next. Or please suggest me some good documentation.
This is a good place to start. It's the examples provided by greenDAO themselves.
https://github.com/greenrobot/greenDAO/tree/master/DaoExample/src/main/java/de/greenrobot/daoexample
You can notice that except for NoteActivity.java, all other java files were generated by the DaoGenerator.
Some common practice:
These lines can be put in your Application scope. Means you can have one session for the whole application life cycle, according to this answer from greendao
Save your Query to reuse to gain performance.
Some Common errors:
When deleting record from DB, make sure to detach it from session. Otherwise the object is still in cache and next time you want to get record by ID and you thought this is already overwritten by a new object. Nope.
I am developing an Android App and thought of using an ORM to leverage some of the database work, i've seen a few and they all seem good for the job, now I only have one main issue left, i haven't found a way of setting the database location on runtime.
For example, im gonna talk about a common (and in this case, very real) scenario lets suppose your app needs to work completely offline, and the user downloads the database the app needs, a common guideline is to provide some sort of file explorer so the user can specify the data directory.
Well, let's suppose im using ActiveAndroid ORM, i've set up everything according to the guide, and I specify the name of the database in the AndroidManifest, i can even set a static path in there, say for example, "/mnt/sdcard/data/data.db" and it will work on using the existing database, i just haven't found the way of doing something like
ActiveAndroid.location = customLocation;
where customLocation is the path selected by the user on the Preferences, namely, the custom location of the database.
Any help appreciated.
I found a workaround that works for me, its nothing too complicated but i haven't seen this particular issue addressed nowhere.
Instead of initializing your ActiveAndroid connection in the AndroidManifest via the
android:name
attribute, don't specify any name or metadata attribute there, now, the official documentation states you can initialize ActiveAndroid on runtime via
ActiveAndroid.initialize(this)
Well, there is a pretty handy class called Builder which can be accessed with a Configuration class, the one in ActiveAndroid, not the one in android.content.res, and with a Builder object you can specify a few things, namely, the
setDatabaseName(String name)
attribute, where you can then use the custom path of your database, and then initialize ActiveAndroid like this
ActiveAndroid.initialize(builder.create())
where the create() method returns a Configuration object
And that does it! Now you can use that pre-populated database with ActiveAndroid wherever your users decided to place it.
I need to ship an app that uses read-only access to several preexisting SQLite3 DB's that each are a couple of 100MB's, total combines size > 1GB. The databases are created on a Mac, and are currently used in a shipping iOS app. I am pretty proficient in Java, but new to Android.
This leads to the following questions:
1) Will I need to modify the databases? I only plan to use them with SQLiteDatabase::rawQuery queries, so no nee for bindings and metadata I hope.
2) It it really correct that even if the DB's will only be used as read-only, I'll have to copy them out of the app bundle or download them to user directory on first start-up?
3) The queries can be slow. I want to run them in a thread and provide data via a callback. Is this done the way it's done in normal Java (Runnable/Thread), or will I have to use another method?
4) Is there anything else that's obvious to the Androidan that I have clearly missed?
1) No, it should work fine.
2) Yes, if you want to ship an APK that is over 50Mb you will need to use an expansion file.
3) For easy background tasks with a call back you could use an ASyncTask.
for a decent example of a sqlite helper class look here
you shouldnt need to edit the database. my sqlite databases work the same wether I access them via sqlite3 or with the android my sqlite helper class
once you copy them you can read and write
not really sure about this answer. i will say though that the database helper class above seems to work just fine (fast) but my db is smaller (500kb)
dont think so
EDIT: Assume a rooted phone for this post.
I deleted a previous question I posted on this topic because none of the answers even came close to answering the question. Long story short, I need to open a database and modify an existing record. I do not want to use a "helper class" because I actually want to see and understand what is going on in a few lines of code rather than an unnecessary (for my purposes) class that contains 100 lines of code. So please don't tell me to "use the notepad tutorial." I have, and it doesn't explain what I need.
To simplify, here is what I am doing:
SQLiteDatabase myDB = this.openOrCreateDatabase("/data/data/MY_APP/databases/settings.db", MODE_PRIVATE, null);
myDB.execSQL("INSERT INTO my_table (SOME_FIELD) VALUES ('SOME_VALUE');");
This works very nicely. However it fails if I try to open/edit a database in a different path. For example I might want to edit a database that another app uses. How can I do this? Is it a simple matter of permissions? Should it work if my app requests and gets root access?
EDIT: There are tons of apps I can install on my phone that are capable of editing every single database on the system so obviously this CAN be done.
Regarding Android security, you cannot access others' app DB directly. If other applications create ContentProvider then you can access theirs DBs (if exist) through its Providers. Otherwise, there's no way out AFAIK.
I don't know if you are still looking for this answer. I was looking to do the same but couldn't really find anything. I knew I needed to use root for the process, but again, couldn't find anything. I started messing around and just trying a lot of random things, and finally found a way to do it.
The short version that worked for me is you need to, as root, change the permissions of the database, access it directly (not through an sqlite helper), do whatever you wish, and then put the permissions back. I detail all of this on my blog:
http://rratmansky.wordpress.com/?p=259&preview=true
I am new to android. I am creating ToDoTasks application in android. i have crated Gui of the application and it is working perfectly. I am able to add tasks in it. Now i want to know that i want to save 'task list' in an area so that every time user comes on it , than it should maintain the list of previous tasks which were added in it. What is the best way to do this ?
Whether i should go for database in android or is there any other way to do this ?
Please suggest me. Please don't mind , i know this is a silly question but i have no other way to solve it.....
You have multiple options with varying degree of complexity.
Do you foresee sharing your todolist with another application. If yes then you need to host your data as a content provider.But I digress.
The most simplest option is Shared Preferences. The api is very simple to use and you do not need to write a whole lot of plumbing code. You can directly store an list of string in the shared preference of your activity.
The more elaborate solution is using sqllite. If you foresee your domain model to become more complex than just a list of strings, then you should see if the additional complexity is worth it.
Look here for more details. (I will not worry about the file options, the other two mentioned here are superior to that solution)
You can use sqlLite db for android. See this or this for example.
Here is another example of an SQLite implementation:
http://p-xr.com/android-tutorial-simple-but-persistent-data-storage/