如何显示WHERE子句

时间:2016-09-09 06:15:38

标签: android sqlite

我遇到了问题,我想要检索多个图像路径,其中分配给每个图像路径的所有名称都是相同的。

我将图像路径的名称指定为相同,因为我想将其设置为将检索具有相同名称的所有图像路径的相册。

我尝试过这样的查询:

DatabaseHandler.java

public class DatabaseHandler extends SQLiteOpenHelper {
    public static final int VERSION = 1;
    public static final String DBNAME = "timelife";
    public static final String TBNAME = "album";

    private static DatabaseHandler sInstance;

    private static synchronized DatabaseHandler getInstance(Context context) {
        if (sInstance == null) {
            sInstance = new DatabaseHandler(context.getApplicationContext());
        }
        return sInstance;
    }


    public DatabaseHandler(Context context) {
        super(context, DBNAME, null, VERSION);
    }

    public void onCreate(SQLiteDatabase db) {
        String query = "CREATE TABLE IF NOT EXISTS " +
                TBNAME + " ( name varchar(255) , path VARCHAR(255) );";
        db.execSQL(query);
    }

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        String query2 = "DROP TABLE IF EXISTS " + TBNAME + " ;";
        db.execSQL(query2);
        onCreate(db);
    }

    public void insert(String[] apath, String title) {
        ArrayList<String> strings = new ArrayList<String>();


        for (String string : apath) {
            strings.add(string);
        }

        Iterator iterate = strings.iterator();

        String query = "INSERT INTO " + TBNAME + " VALUES (?,?);";
        SQLiteDatabase db = this.getWritableDatabase();
        db.beginTransaction();
        SQLiteStatement statement = db.compileStatement(query);

        while (iterate.hasNext()) {
            statement.clearBindings();
            statement.bindString(1, title);
            statement.bindString(2, iterate.next().toString());
            statement.execute();
        }

        db.setTransactionSuccessful();
        db.endTransaction();
        db.close();

        Log.w("Inserting", "Successful");
    }

    public List<GettersSetters> getDataFromDB(){
        List<GettersSetters> modelList = new ArrayList<GettersSetters>();
        String query = "select * from "+ TBNAME + " group by name;";

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(query,null);

        if (cursor.moveToFirst()){
            do {
                GettersSetters model = new GettersSetters();
                model.setName(cursor.getString(0));
                model.setPath(cursor.getString(1));

                modelList.add(model);
            }while (cursor.moveToNext());
        }
        Log.d("student data", modelList.toString());
        cursor.close();
        db.close();
        return modelList;
    }

    public List<GettersSetters> searchFromDB(String name){
        List<GettersSetters> modelList = new ArrayList<GettersSetters>();
        String query = "select * from "+ TBNAME + " where name like '%" + name + "%';"  ;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(query,null);

        if (cursor.moveToFirst()){
            do {
                GettersSetters model = new GettersSetters();
                model.setName(cursor.getString(0));
                model.setPath(cursor.getString(1));
                modelList.add(model);
            }while (cursor.moveToNext());
        }
        Log.d("student data", modelList.toString());

        return modelList;

    }



}

这是我传递给 Flip.java的Intent

RecyclerAdapter.java

        @Override
        public void onClick(View v) {
            String location = name.getText().toString();
            Intent goFlip = new Intent(RecyclerAdapter.context, FlipActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("path", location);
            bundle.putInt("pos", getAdapterPosition());
            goFlip.putExtras(bundle);
            context.startActivity(goFlip);

        }
    }
}

然后我尝试使用以下代码检索结果,但问题是只显示一个图像路径。如何显示所有图像路径?请帮忙,我卡住了。

Flip.java

txtLoc = (TextView) findViewById(R.id.samp);

Intent goFlip = getIntent();
Bundle bundle = goFlip.getExtras();
String getLocation = bundle.getString("path");
index =bundle.getInt("pos");

db = new DatabaseHandler(this);
dbList = new ArrayList<GettersSetters>();
dbList = db.searchFromDB(getLocation);

if(dbList.size() >0){
    String locate = dbList.get(index).getPath();
    txtLoc.setText(locate);
}

我选择了表中的所有行,这就是我得到的...... enter image description here

更新

第0行和第1行中的路径现在显示所有图像路径。但是,recyclerview中的最后一个索引或行会导致应用程序崩溃。

2 个答案:

答案 0 :(得分:1)

在你的方法中只需更改if条件阻塞如下,它将起作用

{ "data" : {
   "ms12345678" : { 
       "name" : "John Doe",
       "age"  : 40,
       "email" : "johndoe@domain.com"
    }
}

答案 1 :(得分:0)

检查sqlite表中的所有数据,因为您正在使用类似查询,它将返回表格中相关的所有名称。

String query = "select * from "+ TBNAME + " where name like '%" + name + "%';"

这是您的查询意味着您拥有如下表格

admin
administrator
superadmin
mainuser

将此查询用于选择数据时

String query = "select * from "+ TBNAME + " where name like '%admin%';"

然后从表raw返回前三个数据。

如果您想要检索特定数据,则必须使用条件关闭的位置。

String query = "select * from "+ TBNAME + " where name = 'admin';"

然后找到了它唯一的一个结果。