Android can't make copy of database - java

I am trying to set up a Tasker task that will delete all alarms in the clock. I had it setup and working great on my old phone. What I did was made a copy of the alarms database when there were no alarms. When I want to delete the alarms that are currently set, Tasker runs a shell command to delete the current database with the alarms in it. Then Tasker runs a shell command to copy the empty database and rename it. Then some commands to change permissions and owner.
When I tried to move this all over to my new phone is when I ran into some trouble. The new phone is a One Plus 3 for what it's worth. So I copied everything thing over and the only thing that looked like it needed to be changed was the package name for the shell commands. I changed that and tried the Tasker task. No luck. The clock app froze up. I restored everything from my backups. I decided to do some testing to see where the issue is.
I used the Root Explorer app to copy the alarms database. When I tried to open the copied database I got the following error message:
An error occurred while opening the database. Attempt to invoke
virtual method 'android.database.Cursor
android.database.sqlite.SQLiteDatabase.query(java.lang.String,
java.lang.String[], java.lang.String, java.lang.String[],
java.lang.String, java.lang.String, java.lang.String)' on a null
object reference
I can still open the original database just fine. But it appears something is happening when I make a copy of it. Any insight would be a big help. Thanks

Problem as I understand it:
You say "When I copy a database I get an error", if it's in the copy process then it's trivial, if you don't mean that, then say that. Moving on...
You have copied an sqlite database from somewhere, to some where (you don't say where, it may matter), and you don't permission to open it [maybe you do, then say so, give ALL permissions] (but you can open the original). File Permissions comes to mind (user permissions on the file).
This method works on an old phone but now does NOT work a new one (API versions would be useful, these methods have changed a LOT).
The error is about Cursor queries.
see my answer about Cursor's, and cursor management
Do not assume your original code was perfect because it worked. More detail required.
Post some minimal code the is involved in the error. We are not trying to steal your code, we cannot answer your question, without seeing the code that makes it fall over, how can we do that ?. ALSO include the catlog. You never know, we might give you code that works ;O)

Related

Checking existence of a sqlite db in Android

When my app runs it first checks for the existence of the db. The first time it runs the db should not exist and if that's the case it will create the dB's tables, then it will populate specific tables with various support data. So, in testing this works fine. So I then delete the db through adb shell. Then I rerun the app and it determines that the db still exists!! I have 2 different methods that checks existence, or not,and both behave in the same aberrant way.
Method 1 simply tries to open the db as a Java file and then uses the exists method to check. Method 2 is a bit more elaborate using the db path and name as args to thedb.open database method.
Both methods fail to determine that the db does not exist, after I delete it in adb shell.
I can provide the code if needed, but thought I'd see if there are some ideas for this behavior. I have cycled the genymotion emulator but this did not fix it.
Thanks for any suggestions.
No. I stop the app, delete the db, then restart (All in Android Studio). By the way. Do you know why the db path, as reported by Gene motion, is not the same as my apps db path? Both tho do point to the same db, as shown in adb.

How can a system app read the entire logcat programmatically in CyanogenMod 11?

I'm writing an app to include in a custom CyanogenMod build that needs to access the full logcat. I've tried the following:
...getRuntime().exec("su -c logcat -c");
but not getting anything from the input stream. I also tried this answer, and su exited with code 0, but it goes through the whole permission thing every time I run the app (when it should only need to do it once), and the code after it (a regular exec("logcat"), without worrying about permissions, which previously was pulling up the app's logs successfully) is no longer working (not getting any content).
My app is in /system/app, which some comments I saw led me to believe could get me the READ_LOGS permission in an easier way somehow, but I don't know how and am having trouble finding out. (I did try just adding the permission to my manifest, which didn't change the log content I was getting.)
(Also possibly worth noting that I'm working on a Nexus 7.)

Java save method stops working when .jar is moved

I’m coding my first java Desktop application using eclipse and I’m having difficulty deploying it. My project uses JavaFX2 and the e(fx)clipse plugin, the latter is in charge of generating the build.xml file.
ABOUT THE APP
The app, amongst other things, provides an interface where the user can create categories and associate these with labels. All modifications are saved within a single file (the data is stored as a serialized object.) and are supposed to be loaded automatically when the app is reopened.
THE PROBLEM
When build as an executable jar using a .xml file (Ant), the project runs fine within the folder where it gets created. I can run my application, modify data, and save everything once I’m done. When I reopen the app everything gets loaded as it should.
However, if I copy the folder elsewhere, I can no longer save any data. Everything else seems to work; the app will even load the data that was saved when it was in its original directory. I assume that this means that the app can still see the data file, but can no longer write to it.
WHAT IVE TRIED
I’ve read that warping the .jar around an installer may fix the issue; however, one of the goals for the app was to make it as portable as possible. Meaning that it should be possible to move it around from one directory/computer to another, ideally in a manner that is cross-platform friendly, without the need of installing it.
I’ve tried various things to get it to work. I’ve shifted the whole project to Netbeans (to produce a different build), I’ve modified the save/load method file path to make sure the right document is targeted, I’ve tweaked the .xml file the best I could, and I even tried to build the project using the javafxpackager. No matter what I do, when the build works, I get the same results.
Right now, I’m thinking that there may still be something wrong with the .xml file but I’ve got a hard time understanding how to modify it. Perhaps the problem is somehow caused by the way the data gets serialized. I know that at one point when I moved things around within my project, both the save and load methods could no longer interact with the data.
What I find strange is that when the project is moved the load method still works. If the problem is caused by changing the file path, how come only the save method ceases to function?
APP FILES AND STRUCTURE
+src
-(Main.java)
+controller
-(misc.javas)
+modelData
-(Library.java) -->the object that is serialized
+modelLogic
-(misc.javas)
+view
-(misc.javas)
+files
-(library.data) -->the file where the serialized object is saved
+lib
-(empty.empty)
So, is their anything I can do to solve my problem?
Thanks in advance.
In the end, the problem was rather simple. When I was moving the app, I was always putting it on the desktop, which, in the case of my PC, sets by default all content as read-only (in relation to the app's privileges). Because of this, the app could not modify any files that were within folders on the desktop.
Therefore, all I had to do was to move the app to another directory, such as C:\randomFolder, and the problem was solved.
So, if anyone has a similar problem, moving the app elsewhere may be the solution. Alternatively, taking full ownership of the folder and its content can also work.
When possible, it is usually better to have the app ask its user for a specific location to save its data.

Backup and Restore MS SQL Server database through Java Program

I have developed an Java Swing Application with MS-SQL Server database and now i want to provide backup and restore option in my java swing application that is on click of a button it should backup the database and restore the database any possibilities of how can i do it through java. please help
Two ways to do that. Backup and restore are just sql commands so you'd do them just the same as as you'd do any other bit of sql, e.g. a sql insert statement, unfortunately you need to know a fair bit about the the system to just do them, and you'll get no progress indicator. Using SQLSMO is another possibility, not sure about hitting a .net dll from java though. You could use SQLDMO (pre.net), but you'll need to install the backwards compatibility tools, and you'll give yourself an upgrade headache as DMO (while it seems to work) is not supported from SQL2012. Both have an event you can tick a progress bar on, course if the back up is fairly quick you could get away with not bothering with that.
However, some more to think about. You can backup while the system is running, but then you'd don't have a clear point where the system was at when you did.
Restore requires exclusive access and a high level of privilege, so it's not something you hand out and you need to get everybody off the databases.
And last but far from least it would be very bad, if you inadvertently restored a version of the database that no longer matched the application....
Personally I'd say the people who were authorised to do this should be able to do it without your tool. Dumbing it down, means a lot of code to make sure they don't restore a sql2000 back up from the trial you put out eight versions ago.
We did something similar back up restore code is a bout 2% of the application. For instance we do a pre-backup check, using dbcc et al, to make sure they aren't successfully backing up a corrupt db...
You can write a simple script to backup your database and invoke it from java.
You really need to put a little effort in here.
Since you can connect to the database server already and send it SQL commands, read this for backing up using TSQL.
For database restore, read this.
You need to have the appropriate permissions and access to the hardware for this to work.

Does Android store data on SQLite permanently or is it temporary storage?

It seems like SQLite (1-st impression) stores it`s data temporary, while executing SQLiteOpenHelper and creating tables there, and inserting rows within onCreate method.
The Q is: Is there an ability to store data permanently and do not create DB and Tables every time the app is start-up? As I understand the data kills, when user exit the app?
Sorry for my English and a lame question - I'm new to Android =)
Thanks in advance.
Your initial impression is incorrect. SQLite tables are persistent in Android - they exist as files in a particular directory based on your application's package id.
The idea of the SQLiteOpenHelper is to encapsulate database creation AND database upgrade in one place, so that the rest of your database-using code doesn't have to worry about such things - it just called the helper and when the helper returns, the database is exactly as it should be.
That's why the helper has an 'onCreate' method (which is used when the database doesn't already exist) and an 'onUpdate' method (which is used when the database already exists, but has a different version number).
The data base is created only if it does not exist. That is the only time that the onCreate method is called. Once created, the data base will remain in existence until you explicitly delete it or until the app is uninstalled.
if you are using SQLiteOpenHelper to create your database it will be create the first time you use the SQLiteOpenHelper object and it will be permanent
Please check by running application on real device. You may be using it on Emulator.
When you start application in emulator the database is created and when you close emulator the temporary environment created by emulator is destroyed thus you will not be able to get it back. Whether running on actual device may store database permanently on your device. So next time when you open your application, it will connect to existing database and you can do operations on it.
You have not provided information regarding whether you are using real device or emulator, please describe that.
Let me know if there is anything more -Thanks.

Categories