开发者

Fetch data from external own SQLite database

开发者 https://www.devze.com 2023-03-11 03:48 出处:网络
I have my own database which contains http://i55.tinypic.com/6i8861.jpg (SQLiteBrowser) I have this code, which copies the database from the assets resource folder to the system data/data/package/file

I have my own database which contains http://i55.tinypic.com/6i8861.jpg (SQLiteBrowser) I have this code, which copies the database from the assets resource folder to the system data/data/package/files directory http://pastebin.com/XeNGmrcD and this is how it is implemented into the onCreate() http://pastebin.com/TigXFStF and this is the image where the test.sqlite is placed into the emulator system dir and the error I get when I execute the query below: http://i51.tinypic.com/2mxrk0p.jpg

cursor = dbObj.getReadableDatabase().query(TABLE_NAME, null,开发者_JAVA百科 null, null, null, null, null);

I get that cursor is null since there is no table found. If I execute the same query equivalent to the one above (SELECT * FROM gradovi) in the SQLite Browser. I get results, here I don't. Please tell me how can i fetch data from my database? What I'm doing wrong? Also I want to know how to iterate through the records?


It looks like the database file copying is failing. Your code has a number of swallowed exceptions, so you might not be seeing the errors that occur.

Note that trying to ship an app with a pre-built SQLite database file is wrought with pitfalls, including varying paths and varying SQLite versions.


((Button)findViewById(R.id.button01)).setOnClickListener(
new View.OnClickListener(){

    @Override
    public void onClick(View v) {
        DatabaseHelper myDbHelper = new DatabaseHelper(CopyDbActivity.this);
        try{
            myDbHelper.createDataBase();
        }catch (IOException ioe) {
            throw new Error("Unable to create database");
        }

        try {                
            myDbHelper.openDataBase();
        }catch(SQLException sqle){
            throw sqle;
        }

        Toast.makeText(CopyDbActivity.this, "Success", Toast.LENGTH_SHORT).show();

        c=myDbHelper.query("EMP_TABLE", null, null, null, null,null, null);
        if(c.moveToFirst()){
            do {
                Toast.makeText(CopyDbActivity.this,
                "_id: " + c.getString(0) + "\n" +
                "E_NAME: " + c.getString(1) + "\n" +
                "E_AGE: " + c.getString(2) + "\n" +
                "E_DEPT:  " + c.getString(3),
                Toast.LENGTH_LONG).show();
            } while (c.moveToNext());
        }
    }
});



}


It's my understanding that the destination should be "/data/data/PackageName/databases/mydatabase".

You might also get some help from http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/

0

精彩评论

暂无评论...
验证码 换一张
取 消