尝试在我的Android应用程序中加载然后查询和SQLite数据库 - 严重受挫

时间:2011-04-20 20:16:57

标签: android database sqlite

基本上我要做的是从我的assets文件夹加载我的数据库(romskillDB.db),然后查询我想要的信息。我已经尝试了几乎所有可以在网上找到的db帮助程序类,我已经尝试研究所述辅助类调用的每个方法,我甚至采用了示例代码,复制它,然后尝试根据我的需要调整它。一切都无济于事。所以这是我在放弃并将数据硬编码到.xml文件中的扩展列表之前得到我需要的答案的最后努力。请查看下面的代码,非常感谢您花时间提供帮助。

这是显示结果的类。

package com.rom.testdb;

import java.io.IOException;
import com.rom.testdb.DbUtils;
import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.TextView;

public class testDB extends ListActivity {

    SQLiteDatabase db;
    DbUtils dbUtil;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        dbUtil = new DbUtils();

        try {
            DbUtils.createDatabaseIfNotExists(this);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        SQLiteDatabase db = DbUtils.getStaticDb();

        String table = "magegeneral";
        String[] columns = {"_id", "name"};
        Cursor cursor = db.query(table, columns, null, null, null, null, null);
        startManagingCursor(cursor);
        cursor.moveToFirst();  
        int thisId = cursor.getInt(0);
        String thisName = cursor.getString(1);
        cursor.close();

        TextView textview = new TextView(this);
        textview.setText(thisId + " " + thisName);
        setContentView(textview);

    }
}

这是帮助代码。

package com.rom.testdb;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

public class DbUtils {
    private static final String DB_PATH = "/data/data/com.rom.testdb/databases/";
    private static final String DB_NAME = "romskillDB.db";

    public static void createDatabaseIfNotExists(Context context) throws IOException {
        boolean createDb = false;

        File dbDir = new File(DB_PATH);
        File dbFile = new File(DB_PATH + DB_NAME);
        if (!dbDir.exists()) {
            dbDir.mkdir();
            createDb = true;
        }
        else if (!dbFile.exists()) {
            createDb = true;
        }
        else {
            // Check that we have the latest version of the db
            boolean doUpgrade = false;

            // Insert your own logic here on whether to upgrade the db; I personally
            // just store the db version # in a text file, but you can do whatever
            // you want.  I've tried MD5 hashing the db before, but that takes a while.

            // If we are doing an upgrade, basically we just delete the db then
            // flip the switch to create a new one
            if (doUpgrade) {
                dbFile.delete();
                createDb = true;
            }
        }

        if (createDb) {
            // Open your local db as the input stream
            InputStream myInput = context.getAssets().open(DB_NAME);

            // Open the empty db as the output stream
            OutputStream myOutput = new FileOutputStream(dbFile);

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

            // Close the streams
            myOutput.flush();
            myOutput.close();
            myInput.close();
        }
    }

    public static SQLiteDatabase getStaticDb() {
        return SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null, SQLiteDatabase.OPEN_READONLY);
    }
}

再次,谢谢你的时间!

1 个答案:

答案 0 :(得分:0)

有两个问题可能导致这种压榨 - 一个。你在里面运行的列表活动尝试使用getApplicationContext()来启动数据库(或者像我那样忘了确切的术语......) 湾你不能从UI线程执行逻辑操作,使用asynctask来处理后台的数据库查询..

至于第二个,我确信95%,因为我有一个类似的问题,并且(使用asynctask)解决了我的问题