Search code examples
javaandroidsqlitetasker

Android can't make copy of database


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


Solution

  • 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)