无法从资产文件夹Android

时间:2015-10-26 07:51:45

标签: android sqlite android-studio android-assets

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

  

DatabaseHelper类

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;
    }
}
  

学生班

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;
    }



}
  

主要活动类

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;
        }

    }

}

1 个答案:

答案 0 :(得分:2)

尝试在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()&amp;的 dbhelper.openDatabase() 只需检查数据库是否已从assets文件夹中复制。

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

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

希望它清除图片!!