0

我为我的项目创建了自己的数据库,但在将其推送到数据/数据文件夹时遇到了麻烦。
我首先在我的手机上对其进行了测试,并成功推送了我创建的数据库。
现在我在另一部手机上测试它时遇到问题。我收到一个错误

android.database.sqlite.SQLiteException: no such table: tblFirstAid: , while compiling: SELECT faName FROM tblFirstAid

我将我的数据库放在资产文件夹中,它在我的手机上运行良好。
我将为其添加什么代码以从资产文件夹中复制文件并将其推送到 /data/data 目录中。
我已经尝试过一个规则设计,但我得到了那个错误。

这是我的数据库助手类

package com.dr.droid.lee;

import java.io.IOException;
import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.http.SslCertificate.DName;
import android.util.Log;

public class DbHelper {

    public static final String Row_id = "_id";
    public static final String Row_Name = "faName";
    public static final String Row_Desc = "faInfo";
    private static final String db_Name = "dbDrDroid";
    private static final String db_Table = "tblFirstAid";
    private static final String db_HTable = "tblHospitals";
    private static final String Row_HosName = "HospitalName";
    private static final String Row_HosAdd = "Address";
    private static final String Row_HosReg = "Region";
    private static final String Row_HosCity = "City";
    private static final String Row_HosContact = "Contact";
    private static final String Row_dName = "dName";
    private static final String Row_dTable = "tblDisease";
    private static final int db_Version = 1;
    private dbhelp ourhelper;
    private static Context ourcontext;
    private SQLiteDatabase ourDB;

    private static class dbhelp extends SQLiteOpenHelper{

        public dbhelp(Context context) {
            super(context, db_Name, null, db_Version);
            // TODO Auto-generated constructor stub
        }

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



            db.execSQL("CREATE TABLE IF NOT EXISTS " + db_Table + " (" + Row_id + " INTEGER PRIMARY KEY AUTOINCREMENT, "
                + Row_Name + " TEXT NOT NULL, "
                + Row_Desc + " TEXT NOT NULL)" );

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
            db.execSQL("DROP TABLE IF EXISTS" + db_Table);
            onCreate(db);

        }

    }

    public DbHelper (Context c){
        ourcontext= c;
    }

    public DbHelper open(){
        ourhelper = new dbhelp(ourcontext);
        ourDB = ourhelper.getWritableDatabase();
        return this;
    }

    public void   close(){
        ourhelper.close();
    }

    public ArrayList<String> getFAData() {
        // TODO Auto-generated method stub
        ArrayList<String> comments = new ArrayList<String>();
        String [] columns = new String[]{Row_Name};
        Cursor c = ourDB.query(db_Table, columns, null, null, null, null, null);
        int iRow = c.getColumnIndex(Row_Name);
        c.moveToFirst();
        while (!c.isAfterLast()) {          
            comments.add(c.getString(iRow));
            c.moveToNext();
        }

        c.close();
        return comments;
    }

    public ArrayList<String> getHData(){
        ArrayList<String>  res = new  ArrayList<String>();
        String [] columns = new String []{Row_HosName};
        Cursor h = ourDB.query(db_HTable, columns, null, null, null, null, Row_HosName);
        int HRow = h.getColumnIndex(Row_HosName);
        h.moveToFirst();
         while (!h.isAfterLast()) {          
                res.add(h.getString(HRow));
                h.moveToNext();
            }

            h.close();
            return res;

    }

    public ArrayList<String> getDData(){
        ArrayList<String>  res = new  ArrayList<String>();
        String [] columns = new String []{Row_dName};
        Cursor d = ourDB.query(Row_dTable, columns, null, null, null, null, null);
        int HRow = d.getColumnIndex(Row_dName);
        d.moveToFirst();
         while (!d.isAfterLast()) {          
                res.add(d.getString(HRow));
                d.moveToNext();
            }

            d.close();
            return res;

    }



}

这是我从规则设计中使用的课程

package com.dr.droid.lee;

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

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

public class DataBaseHelper extends SQLiteOpenHelper{

    //The Android's default system path of your application database.
    private static String DB_PATH = "/data/data/com.dr.droid.lee/databases/";

    private static String DB_NAME = "dbDrDroid";

    private SQLiteDatabase myDataBase; 

    private final Context myContext;

    /**
     * Constructor
     * Takes and keeps a reference of the passed context in order to access to the application assets and resources.
     * @param context
     */
    public DataBaseHelper(Context context) {

        super(context, DB_NAME, null, 1);
        this.myContext = context;
    }   

  /**
     * Creates a empty database on the system and rewrites it with your own database.
     * */
    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 {
                this.close();
                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
     */
    private boolean checkDataBase(){

        SQLiteDatabase checkDB = null;

        try{
            String myPath = DB_PATH + DB_NAME;
            checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

        }catch(SQLiteException e){

            //database does'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
        String outFileName = DB_PATH + 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 void openDataBase() throws SQLException{

        //Open the database
        String myPath = DB_PATH + DB_NAME;
        myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

    }

    @Override
    public synchronized void close() {

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

            super.close();

    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

如果我没有将数据库推送到手机中,我仍然会遇到一些错误。

4

2 回答 2

0

我假设您的 DB_NAME 与您的资产文件夹中存在的数据库不同。

如果您遵循“reigndesign”,请检查此行:

private static String DB_NAME ="myDBName"; 

DB_NAME 这里是您的数据库的名称。假设您在 assets 文件夹中有数据库的副本。您的 DB_NAME 必须与 assets 文件夹中存在的相同。

或者

按照这个

于 2012-10-01T14:57:36.753 回答
0

当你给它一个不存在的文件名时,SQLite 会愉快地创建一个新的空数据库。

确保用于复制和打开数据库的文件名相同。

于 2012-10-01T15:11:52.923 回答