从外部自己的SQLite数据库中获取数据

时间:2022-01-07 08:18:58

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

我有自己的数据库,其中包含http://i55.tinypic.com/6i8861.jpg(SQLiteBrowser)我有这个代码,它将数据库从资源资源文件夹复制到系统数据/数据/包/文件目录http: //pastebin.com/XeNGmrcD这是如何在onCreate()http://pastebin.com/TigXFStF中实现的,这是test.sqlite放入模拟器系统目录的图像,我得到的错误当我执行以下查询时:http://i51.tinypic.com/2mxrk0p.jpg

cursor = dbObj.getReadableDatabase().query(TABLE_NAME, null, 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?

我得到的光标是null,因为没有找到表。如果我在SQLite浏览器中执行与上面相同的查询(SELECT * FROM gradovi)。我得到了结果,在这里我没有。请告诉我如何从数据库中获取数据?我做错了什么?另外我想知道如何遍历记录?

3 个解决方案

#1


2  

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.

请注意,尝试使用预先构建的SQLite数据库文件发布应用程序会带来陷阱,包括不同的路径和不同的SQLite版本。

#2


1  

((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());
        }
    }
});



}

#3


0  

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

我的理解是目的地应该是“/ 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/

您也可以从http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/获得一些帮助。

#1


2  

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.

请注意,尝试使用预先构建的SQLite数据库文件发布应用程序会带来陷阱,包括不同的路径和不同的SQLite版本。

#2


1  

((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());
        }
    }
});



}

#3


0  

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

我的理解是目的地应该是“/ 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/

您也可以从http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/获得一些帮助。