无法从Android的资源文件夹中检索数据



我正在创建一个在assets文件夹中检索sqlite数据库的应用程序。当我运行程序时,在编译SELECT * FROM students时,它说没有这样的表:students。资产文件夹中有学生。Sqlite和有一个名为students的表,它由id、姓名和性别组成。我将在下面显示代码。

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

}

尝试在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()将打开数据库进行所需的读操作。

希望它能清除图片!!

相关内容

  • 没有找到相关文章

最新更新