android数据库连接

时间:2011-03-18 10:24:41

标签: android

嗨我正在为Android手机开发一个简单的应用程序我第一次创建它正在工作,但我想在其他项目中同样的事情,但我无法得到像无法打开数据库的错误

package com.exampleHelloText;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;

import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;
import android.util.Log;

public class DataBaseHelper extends SQLiteOpenHelper{
    //private static String DB_PATH = "/data/data/com.drager/databases/";
    private static String DB_PATH = Environment.getDataDirectory()+"/data/com.exampleHelloText/databases/";
    final static String DB_NAME = "myDBName";
    private SQLiteDatabase myDataBase=null;
    private final Context myContext;
    private DataBaseHelper myDbHelper;
    private static String TAG ="MyActivity";

    public DataBaseHelper(Context context){
        super(context, DB_NAME, null, 1);
        this.myContext = context;

    }

    public DataBaseHelper createDataBase() throws IOException{
        boolean dbExist =checkDataBase();
        //SQLiteDatabase db_read =null;
        Log.i(TAG,"############value of dbExist"+dbExist+"##########");
        if (dbExist){
            //db must exist
        }
        else{
        myDbHelper = new DataBaseHelper(myContext);
        myDataBase = myDbHelper.getReadableDatabase();
        myDataBase.close();
        //this.getReadableDatabase();
            //db_read.close();

            try {
                copyDataBase();
            } catch (IOException e) {
                throw new Error("error copying database");
            }
        }
        return this;

    }



    public void copyDataBase() throws IOException{
        // open db as input stream
        InputStream myInput;
        //open empty db as output stream
        OutputStream myOutPut;
        try {
            myInput = myContext.getAssets().open(DB_NAME);

            //path to newly created db
            String outFileName =DB_PATH + DB_NAME;

            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);
            }
            myOutPut.flush();
            myOutPut.close();
            myInput.close();
            }
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }




    }

    private boolean checkDataBase() {
        SQLiteDatabase checkDB = null;

        String myPath = DB_PATH + DB_NAME;

        try {
            checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
        } catch (SQLException e) {

            e.printStackTrace();
            return false;
        }

        if (checkDB != null){
            checkDB.close();
        }
        return true;
        //return checkDB !=null ? true : false;
    }

    public void openDataBase()throws SQLException{
        //open the database
        String myPath = DB_PATH + DB_NAME;
        myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
    }

    @Override
    public synchronized void close(){
        if(myDataBase != null){
            myDataBase.close();
        }
        super.close();
    }

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

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }
}

请帮帮我 提前谢谢

2 个答案:

答案 0 :(得分:0)

您有 onCreate onUpgrade 方法的空体。
确保您正在调用方法createDataBase()并通过myDataBase获取数据库。

答案 1 :(得分:0)

对于数据库,您可以参考this ..下载Zip文件...