无法读取数据库android

时间:2012-11-16 12:37:31

标签: android database sqlite

我将数据库从资产复制到设备,但无法从数据库中读取数据 这里是代码

package ta.ex;

import java.io.IOException;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    MyDatabase mydb;
    ListView listtema;
    SQLiteDatabase db;
    Cursor cursor;
    SimpleCursorAdapter adapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mydb = new MyDatabase(this);
        try {
            mydb.createDataBase();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        db = db.openDatabase("data/data/ta.ex/databases/ensiklopedia.sqlite", null, db.OPEN_READONLY);

        listtema = (ListView)findViewById(R.id.listTema);
        String[] columns = new String[]{"_id","tema"};       
        cursor = db.query("tema", columns, null, null, null, null, null);
        if (cursor != null){
            adapter = new SimpleCursorAdapter(this,
                    android.R.layout.simple_list_item_1,cursor,
                    new String[]{"tema"},new int[]{android.R.id.text1});
            listtema.setAdapter(adapter);
        }


    }
}

MyDatabase.java

package ta.ex;

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;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

public class MyDatabase extends SQLiteOpenHelper{   
    private static final String DB_PATH = "/data/data/ta.ex/databases/";
    private static final String DB_NAME = "ensiklopedia.sqlite";
    private SQLiteDatabase sqldb;
    private final Context myContext;


    public MyDatabase(Context context) {
        super(context,DB_NAME,null,1 );
        // TODO Auto-generated constructor stub
        this.myContext = context;
    }

     public void createDataBase() throws IOException{

            boolean dbExist = checkDataBase();

            if(dbExist){
                //do nothing - database already exist
            }else{

                //By calling this method and empty database will be created into the default system path
                   //of your application so we are gonna be able to overwrite that database with our database.
                this.getReadableDatabase();

                try {

                    copyDataBase();

                } catch (IOException e) {

                    throw new Error("Error copying database");

                }
            }

        }

        /**
         * Check if the database already exist to avoid re-copying the file each time you open the application.
         * @return true if it exists, false if it doesn't
         */
        boolean checkDataBase(){

            SQLiteDatabase checkDB = null;
            try{
                String myPath = DB_PATH + DB_NAME;
                checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
            }
            catch(SQLiteException e){
                //database doesn't exist yet.
            }
            if(checkDB != null){
                checkDB.close();
            }

            return checkDB != null ? true : false;
        }

        /**
         * Copies your database from your local assets-folder to the just created empty database in the
         * system folder, from where it can be accessed and handled.
         * This is done by transfering bytestream.
         * */
        private void copyDataBase() throws IOException{

            //Open your local db as the input stream
            InputStream myInput = myContext.getAssets().open(DB_NAME);

            // Path to the just created empty db
            File outFileName = myContext.getDatabasePath(DB_NAME);

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

            //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 boolean isDatabaseExist() {
            File dbFile = new File(DB_PATH+DB_NAME);
            return dbFile.exists();
        }

        public void openDataBase(){
            //Open the database
            String myPath = DB_PATH + DB_NAME;
            sqldb = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
        }

        @Override
        public synchronized void close() {

                if(sqldb != null)
                    sqldb.close();

                super.close();

        }
    @Override
    public void onCreate(SQLiteDatabase arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
        // TODO Auto-generated method stub

    }

}

log cat

11-16 19:18:12.182: ERROR/AndroidRuntime(5818): java.lang.RuntimeException: Unable to start activity ComponentInfo{ta.ex/ta.ex.MainActivity}: java.lang.NullPointerException
11-16 19:18:12.182: ERROR/AndroidRuntime(5818):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-16 19:18:12.182: ERROR/AndroidRuntime(5818):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-16 19:18:12.182: ERROR/AndroidRuntime(5818):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-16 19:18:12.182: ERROR/AndroidRuntime(5818):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-16 19:18:12.182: ERROR/AndroidRuntime(5818):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-16 19:18:12.182: ERROR/AndroidRuntime(5818):     at android.os.Looper.loop(Looper.java:123)
11-16 19:18:12.182: ERROR/AndroidRuntime(5818):     at android.app.ActivityThread.main(ActivityThread.java:4627)
11-16 19:18:12.182: ERROR/AndroidRuntime(5818):     at java.lang.reflect.Method.invokeNative(Native Method)
11-16 19:18:12.182: ERROR/AndroidRuntime(5818):     at java.lang.reflect.Method.invoke(Method.java:521)
11-16 19:18:12.182: ERROR/AndroidRuntime(5818):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-16 19:18:12.182: ERROR/AndroidRuntime(5818):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-16 19:18:12.182: ERROR/AndroidRuntime(5818):     at dalvik.system.NativeStart.main(Native Method)
11-16 19:18:12.182: ERROR/AndroidRuntime(5818): Caused by: java.lang.NullPointerException
11-16 19:18:12.182: ERROR/AndroidRuntime(5818):     at ta.ex.MainActivity.onCreate(MainActivity.java:33)
11-16 19:18:12.182: ERROR/AndroidRuntime(5818):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-16 19:18:12.182: ERROR/AndroidRuntime(5818):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

当我使用createDatabase()数据库复制到系统时,但每当我尝试打开数据库时,它总是强制关闭java.lang.nullpointerexception。 请帮忙

2 个答案:

答案 0 :(得分:3)

在行中使用变量db之前,你没有初始化它。

db = db.openDatabase("data/data/ta.ex/databases/ensiklopedia.sqlite", null, db.OPEN_READONLY);

你可以使用:

db = SQLiteDatabase.openDatabase("data/data/ta.ex/databases/ensiklopedia.sqlite", null, db.OPEN_READONLY);

或使用您的函数openDataBase

public SQLiteDatabase openDataBase(){
        //Open the database
        String myPath = DB_PATH + DB_NAME;
        sqldb = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
        return sqldb;
    }
活动中的

db = mydb.openDataBase();

答案 1 :(得分:1)

我发现了这个问题,

 db = db.openDatabase("data/data/ta.ex/databases/ensiklopedia.sqlite", null, db.OPEN_READONLY);

删除以上行并使用以下

mydb.openDataBase();

在mydatabase.java中创建返回游标的函数。你在mydatabase.java类中有db对象