无法从资产文件夹 Android 检索数据

Cant retrieve the data from assets folder Android

您好,我正在创建一个应用程序来检索资产文件夹中的 sqlite 数据库。当我 运行 程序时,它说没有这样的 table: students while compiling SELECT * FROM students. assets 文件夹有 students.sqlite 和 table 名学生,由 id、name 和 gender 组成。我将在下面显示我的代码。

DatabaseHelper class

public class DatabaseHelper extends SQLiteOpenHelper {

    private static String DB_PATH = "/data/data/com.example.user.students/databases/";
    private static String DB_NAME = "students.sqlite";
    private SQLiteDatabase myDataBase;
    private Context myContext = null;


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

    public void createDataBase() throws IOException {
        boolean dbExist = this.checkDataBase();
        if (!dbExist) {
            this.getReadableDatabase();

            try {
                this.copyDataBase();
            } catch (IOException e) {
                throw new Error("Error");
            }
        }
    }

    public void copyDataBase() throws IOException {
        InputStream myInput = this.myContext.getAssets().open(DB_NAME);
        String outFileName = DB_PATH + DB_NAME;
        FileOutputStream myOutput = new FileOutputStream(outFileName);
        byte[] buffer = new byte[1024];

        int length;
        while ((length = myInput.read(buffer)) > 0) {
            myOutput.write(buffer, 0, length);
        }

        myOutput.flush();
        myOutput.close();
        myInput.close();
    }

    public boolean checkDataBase() {
        SQLiteDatabase checkDB = null;

        try {
            String e = DB_PATH + DB_NAME;
            checkDB = SQLiteDatabase.openDatabase(e, null, 0);
        } catch (SQLiteException e) {
            ;
        }

        if (checkDB != null) {
            checkDB.close();
        }

        return checkDB != null;
    }

    public void openDataBase() throws SQLException {
        String myPath = DB_PATH + DB_NAME;
        this.myDataBase = SQLiteDatabase.openDatabase(myPath, null, 0);
    }

    public synchronized void close() {
        if (this.myDataBase != null) {
            this.myDataBase.close();
        }

        super.close();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

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

    public List<Students> getAll() {
        List<Students> sList = new ArrayList<Students>();
        {
            String selectQuery =
                    "SELECT * FROM students";
            Log.e("students query: ", selectQuery);
            SQLiteDatabase db = this.getWritableDatabase();
            Cursor cursor = db.rawQuery(selectQuery, null);

            // looping through all rows and adding to list
            if (cursor.moveToFirst()) {
                do {
                    Students si = new Students();
                    si.setid(Integer.parseInt(cursor.getString(0)));
                    si.setname(cursor.getString(1));
                    si.setgender(cursor.getString(2));

                    sList.add(si);
                } while (cursor.moveToNext());
            }

            db.close();
        }
        return sList;
    }
}

Students class

public class Students {

    //private variables
    int id;
    String name;
    String gender;

    // Empty constructor
    public Students(){

    }
    // constructor
    public Students(int id, String name, String gender){

        this.id = id;
        this.name = name;
        this.gender = gender;

    }

    // getting ID
    public int getid(){
        return this.id;
    }

    // setting id
    public void setid(int id){
        this.id = id;
    }

    // getting name
    public String getname(){
        return this.name;
    }

    // setting name
    public void setname(String name){
        this.name = name;
    }

    // getting gender
    public String getgender(){
        return this.gender;
    }

    // setting gender
    public void setgender(String gender){
        this.gender = gender;
    }



}

Main Activity class

public class MainActivity extends Activity {

    List<Students> GetAll;
    DatabaseHelper db = new DatabaseHelper(this);

    Context context = this;
    ListView list_doctors;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        list_students = (ListView) findViewById(R.id.list);
        GetAll = db.getAll();
        list_students.setAdapter(new ViewAdapter());
    }



    public class ViewAdapter extends BaseAdapter {

        LayoutInflater mInflater;

        public ViewAdapter() {
            mInflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return GetAll.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.list_item,null);
            }

            final TextView names = (TextView) convertView.findViewById(R.id.name);
            final TextView gender = (TextView) convertView.findViewById(R.id.gender);

            names.setText(GetAll.get(position).getname());
            gender.setText(GetAll.get(position).getgender());

            return convertView;
        }

    }

}

尝试在 MainActivity 的 onCreate() 方法中添加以下行:

public class MainActivity extends AppCompatActivity {
ListView lv;
ArrayList<Students> GetAll;
DatabaseHelper dbhelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    dbhelper = new DatabaseHelper(MainActivity.this);
    //Add below lines to your original code
    try{
        dbhelper.createDataBase();
    }
    catch(IOException e){
        e.printStackTrace();
    }
    try {
        dbhelper.openDataBase();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    //Till here
    GetAll = dbhelper.getAll();
    lv = (ListView) findViewById(R.id.lv);
    lv.setAdapter(new ViewAdapter());

}

基本上加了两行dbhelper.createDataBase() & dbhelper.openDatabase() 只需检查数据库是否已从资产文件夹复制。

如果 createDatabase() 方法发现数据库在内部数据库路径中不存在(在您的情况下它是 /data/data/com.example.user.students/databases/ ) ,然后它只是将数据库从资产文件夹复制到内部数据库位置,然后简单地调用 OpenDatabase() 将打开数据库以进行所需的读取操作。

并且如果 createDatabase() 方法发现数据库已存在于 DB_PATH 中,则不会执行其余方法,然后简单调用 OpenDatabase() 将打开数据库以进行所需的读取操作。

希望图片清晰!!