Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用android通过选择查询预填充数据库。怎么做?_Android_Select_Android Sqlite - Fatal编程技术网

使用android通过选择查询预填充数据库。怎么做?

使用android通过选择查询预填充数据库。怎么做?,android,select,android-sqlite,Android,Select,Android Sqlite,这是我第一次使用预填充数据库,这意味着数据库必须显示在oncreate方法中。 我需要使用select查询来执行SQL查询,我读了很多教程,其中显示了获取预填充数据库的步骤 如果有人能帮助我,我将不胜感激 这是我在几个教程中使用的代码 private static class DatabaseHelper extends SQLiteOpenHelper { // ******* to copy database prepopulate the application*****

这是我第一次使用预填充数据库,这意味着数据库必须显示在oncreate方法中。 我需要使用select查询来执行SQL查询,我读了很多教程,其中显示了获取预填充数据库的步骤

如果有人能帮助我,我将不胜感激

这是我在几个教程中使用的代码

private static class DatabaseHelper extends SQLiteOpenHelper {

        // ******* to copy database prepopulate the application*************//
        // ********************************************************************//

        private static String DB_PATH = "/data/data/com.devleb.database/world_cup";
        public static String DATABASE_NAME = "world_cup";
        private SQLiteDatabase db;
        private final Context myContext;

        // ******* to copy database prepopulate the application*************//

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            this.myContext = context;
        }

        public void createDataBase() throws IOException {
            boolean dbExist = checkDataBase();

            if (dbExist) {

            } else {
                this.getReadableDatabase();
                try {
                    copyDataBase();

                } catch (IOException e) {
                    throw new Error("Error copying Data");
                }
            }

        }

        private void copyDataBase() throws IOException {
            // TODO Auto-generated method stub

            // Open your local db as the input stream
            InputStream myInput = myContext.getAssets().open(DATABASE_NAME);

            // Path to the just created empty db
            String outFileName = DB_PATH + DATABASE_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);
            }

            myOutPut.flush();
            myOutPut.close();
            myInput.close();

        }


        public void openDataBase() throws SQLException{
            String myPath = DB_PATH + DATABASE_NAME;
            db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

        }

        public synchronized void  close(){
            if(db != null){
                db.close();
            }
        }
        private boolean checkDataBase() {
            // TODO Auto-generated method stub

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

            } catch (SQLiteException e) {

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

            return checkDB != null ? true : false;

        }

        @Override
        public void onCreate(SQLiteDatabase _db) {
            //_db.execSQL(DATABASE_CREATE_SQL);
        }

        @Override
        public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
            Log.w(TAG, "Upgrading application's database from version "
                    + oldVersion + " to " + newVersion
                    + ", which will destroy all old data!");
            // Destroy old database:
            _db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
            // Recreate new database:
            onCreate(_db);
        }
    }
我需要的是添加一个select查询来选择我需要的内容,因为我有多个表要预填充。

然后为每个表创建模型类(也称为Bean),并为每个表创建类似这样的方法

public static ArrayList<ModelTable1> SelectAll() {
    SQLiteDatabase sqldb = //initialize database here;
    ArrayList<ModelTable1> arrModelList = null;
    Cursor cursor = null;
    String Query = "Select * from " + TableName;
    cursor = sqldb.rawQuery(Query, null);
    if (cursor != null && cursor.moveToFirst()) {
        arrModelList = new ArrayList<ModelTable1>();
        do {
            ModelTable1 model = new ModelTable1();

            model.Id = (cursor.getString(cursor
                    .getColumnIndex("ID")));
            model.name = (cursor.getString(cursor
                    .getColumnIndex("NAME")));
            arrModelList.add(model);
        } while (cursor.moveToNext());
        cursor.close();
    }// end if(cursor!=null)
    return arrModelList;
}

我不明白你的答案,然后创建模型类(也称为Bean)什么是ModelTable1??你能解释一下你的答案吗?
class ModelTable1{
  String Id;
  String name;
}