如何在Android

时间:2017-07-11 04:19:31

标签: android android-sqlite

我不知道在哪里插入选择查询。我在项目中导入了一个外部数据库(Sqlite浏览器),我不知道如何将数据从我的数据库显示到listview。

public class DatabaseHelper extends SQLiteOpenHelper {

    String DB_PATH = null;
    public static String DB_NAME = "extenalDBS";
    private SQLiteDatabase myDataBase;
    private final Context myContext;


    public DatabaseHelper(Context context) {
        super(context, DB_NAME, null, 1);
        this.myContext = context;
        this.DB_PATH = "/data/data/" + context.getPackageName() + "/" + "databases/";
        Log.e("Path 1", DB_PATH);
    }


    public void createDataBase() throws IOException {
        boolean dbExist = checkDataBase();
        if (dbExist) {
        } else {
            this.getReadableDatabase();
            try {
                copyDataBase();
            } catch (IOException e) {
                throw new Error("Error copying database");
            }
        }
    }

    public boolean checkDataBase() {
        SQLiteDatabase checkDB = null;
        try {
            String myPath = DB_PATH + DB_NAME;
            checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
        } catch (SQLiteException e) {
        }
        if (checkDB != null) {
            checkDB.close();
        }
        return checkDB != null ? true : false;
    }

    private void copyDataBase() throws IOException {
        InputStream myInput = myContext.getAssets().open(DB_NAME);
        String outFileName = DB_PATH + DB_NAME;
        OutputStream myOutput = new FileOutputStream(outFileName);
        byte[] buffer = new byte[1];
        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 + 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) {
        if (newVersion > oldVersion)
            try {
                copyDataBase();
            } catch (IOException e) {
                e.printStackTrace();

            }
    }

    public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy) {
        return myDataBase.query("wordbank", null, null, null, null, null, null);
    }



}

1 个答案:

答案 0 :(得分:0)

将它放在数据库助手类

Cursor getDatafromDB(){ return getWritableDatabase().rawQuery("SELECT 
* FROM TABLE_NAME",null);}

并在java类中读取它

List<Bean> list = new ArrayList<>();
Bean bean;
Cursor cursor = databaseHelperInstance.getDatafromDB();
if(cursor.moveToFirst())
 {
 do{ 
    bean = new Bean();
    String a = cursor.getString(0);
    String b = cursor.getString(1);
    String c = cursor.getString(2);
    String b = cursor.getString(3);

    bean.setA(a);
    bean.setA(b);
    bean.setA(c);
    bean.setA(d);

    list.add(bean);

   }while(cursor.moveToNext());
 }

这将为列表视图的自定义适配器准备一个列表。