0

I want to write database from txt to database if it does not exist:

File database=getApplicationContext().getDatabasePath("commments.db");

                if (!database.exists()) {
                    // Database does not exist so copy it from assets here
                    try {
                        txtToDb();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    Log.i("Database", "Not Found");
                } else {
                    Log.i("Database", "Found");
                }

It's not working, and the database gets filled over and over again. What am I doing wrong?

UPDATE: txtToDb():

public void txtToDb () throws IOException {

        InputStream is =getResources().openRawResource(R.raw.data);
        BufferedInputStream bis = new BufferedInputStream(is);

        ByteArrayBuffer baf = new ByteArrayBuffer(50);

        int current = 0;

        while ((current = bis.read()) != -1) {

            baf.append((byte) current);

        }

        byte[] myData = baf.toByteArray();
        String dataInString = new String(myData);
        String[] lines = dataInString.split("\n");

        for (int i=0; i<lines.length; i++){
            comment = datasource.createComment(lines[i]);
            // adapter.add(comment);
        }

        writeDbToSd();

    }

DbToSd() writes DB to SDCARD so that I can use adb pull to view it. Both functions work fine on their own. createComment adds parsed string to database in another function.

Ahmed Zafar
  • 665
  • 2
  • 10
  • 24
  • Can you add whats in the txtToDb method? Current info isn't enough! – A M Aug 22 '13 at 22:42
  • Your method looks right to me except that I dont know if you are repeating something in writeDbToSd(). – A M Aug 22 '13 at 22:53
  • It's probably a better idea to do your database access using an [`SQLiteOpenHelper`](http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html). That way, you can get rid of that first snippet and simply call `txtToDb` inside `onCreate` (after creating the tables). As for your problem, have you tried stepping through `txtToDb` with your debugger? It's a wild guess, but since you're reading raw bytes: make sure your resource has the proper character encoding. If the input is in ASCII, `new String(byte[])` will bork up since the default charset is UTF-8. – Mattias Buelens Aug 22 '13 at 22:57
  • @mat- I am using an SQLiteOpenHelper! How do I tally that with this please? Plus before checking if database exists txtToDb works fine. It just appends the database with the same. Everything works fine instead of this. I don't get the ASCII and UTF thing I'm not a pro programmer could you please elaborate thanks. I have the database, it's perfectly written through txtToDb, only thing is whenever I run the app again through Eclipse the database expands to have the same old. – Ahmed Zafar Aug 22 '13 at 23:03
  • @mat- I called txtToDB inside onCreate(). Doesn't seem to work and the app crashes. As txtToDB was in a seperate class I had to create a class instance x and had to call x.txtToDb(). Not working. Any idea as to why? EDIT: Also you were right, my encoding for the txt is ANSI and not UTF-8, but the thing is that it was creating the database alright and I checked it by adb pulling it and opening it in an SQLite Browser. So surely that's not an issue? – Ahmed Zafar Aug 23 '13 at 09:03
  • Please help with this http://stackoverflow.com/questions/20728808/android-reading-stored-sqlite-database – gandharv09 Dec 22 '13 at 11:33

0 Answers0