0

I'm going nuts! Maybe someone can help me?! I have an sqlite-database on a running server, which I receive due to an php-script. (To make it clear: I'm calling an php-script which gives me the the database as a response). With the response I'm now trying to "parse" it to an regular *.db file which I later on use for my app.

the app works fine, while placing the *.db into the assets folder. But I need to get the updated database everytime when calling the app. Therefore I need to receive it somehow from the server.

Just for notice: I don't know why they use a php-script for that, but it works perfectly with the iOS-Version of the app. So I am 100% sure that the script does work.

Got any hints or a solution to that? Thanks!

EDIT: here is what I'm trying to do. private void copyDatabase() { InputStream myInputDB = null; OutputStream myOutputDB = null; HttpResponse response = null; // Path to the just created empty db String dbFilePath = DB_PATH + KeyConstants.DB_NAME;

     // Creating HTTP client
    HttpClient httpClient = new DefaultHttpClient();
    // Creating HTTP Post
    HttpPost httpPost = new HttpPost(
            "http://USERNAME:PASSWORD@ADRESS/u/db.php");


    try {
        response = httpClient.execute(httpPost);

        //Open your local db as the input stream
        myInputDB = response.getEntity().getContent();

        //Open the empty db as the output stream
        myOutputDB = new FileOutputStream(dbFilePath);



        //transfer bytes from the inputfile to the outputfile
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInputDB.read(buffer)) > 0){
            myOutputDB.write(buffer, 0, length);
        }

        //Close the streams
        myOutputDB.flush();
        myOutputDB.close();
        myInputDB.close();

    } catch (IOException ioEXC) {
        throw new Error("Problem copying database from resource file.");
    }
njsmecho
  • 45
  • 6

1 Answers1

0

What do You have the problem with? With the Android app or with the PHP script?

I don't understand why there is a whole DB file request-response and not just the data (and better in some lazy reading), but that's not matter of Your case.

If You need to "download" all the DB at every run You should call and implement some method to do so - in the very first Activity call this method within onCreate() method. You can create a simple method within this Activity or in better approach create a simple class for this that will be instanciated and its method called within the first activity's onCreate().

But maybe I just don't understand You question...

EDIT: try reading through this problem: Can I download an SQLite db on /sdcard and access it from my Android app?

Community
  • 1
  • 1
shadyyx
  • 15,825
  • 6
  • 60
  • 95
  • My problem is that I need to download the whole database every time I start the app. To get the database I have to call a php-script which sends me the database. And I somehow have to transfor the response of the php-script into a working sqlite file. – njsmecho Apr 30 '12 at 12:28
  • @njsmecho And what is PHP script returning instead? Is it a binary data of sqlite DB? If it is so you can then only save the response as a sqlite.db file and then just point Your SqliteHelper (or whatever the data manipulation class is called) to that file. Or? – shadyyx Apr 30 '12 at 12:39
  • @njsmecho I still don't see any problem within Your code and Your question. Is the DB file created correctly? Can You access that file and read it as a SQLite DB? Try reading the problem at link provided within my answer. – shadyyx Apr 30 '12 at 12:45
  • thanks for the help! EDIT: yup, it is the binary data of a sqlite DB. And what you described is exactly what I want to do. I want to save the response as a sqlite.db and point to it with my SqliteHelper. But I can't get it done, that the file is being created. my logCat shows the Error that the database is malformed. – njsmecho Apr 30 '12 at 12:57
  • i've already seen the link you mentioned (more than once). But I just can't save on the sdcard neither. – njsmecho Apr 30 '12 at 13:03
  • Did You inserted the neccessary permission to the manifest.xml file? You need permission to write to the external storage: ``. Are You also using ADB and `logcat` to find out whether there is any error while retrieving or saving the data? – shadyyx Apr 30 '12 at 13:23
  • ok, now there are three messages in logcat. 1st - sqlite returned: error code = 11, msg = database corruption found by source line 40112. 2nd - sqlite returned: error code = 11, msg = database disk image is malformed 3rd - sqlite returned: error code = 14, msg = cannot open file at source line 25472 (but the third appears after the malformed error) – njsmecho Apr 30 '12 at 13:45
  • Hmm, I cannot help now. It seems like the data returned by PHP is corrupted (either for SQLite reader or indeed) - You should check whether tha data coming from PHP is in right format or somehow encoded (base64 eg?) - if the PHP script is correct and is done in proper manner the data should be encoded. Try to contact the administrator/creator of PHP script in what format the data is returned. – shadyyx Apr 30 '12 at 14:42
  • I found the "problem". As I found out I must send a parameter with the request which contains the version number. Otherwise the script pushes a 'permission denied' error. So I guess the only benefit of this posting is, to know that the above code is correct. Once again, thanks shadyyx! – njsmecho May 03 '12 at 13:15
  • @njsmecho However this wasn't very helpful, You're welcome :-) – shadyyx May 03 '12 at 13:21