0

I have a problem, I want to retrieve multiple images path where all the names assigned to each image path are all the same.

I assigned the name of the image path to be the same because I want to make it like an album where the all the image path having the same name will be retrieved.

I tried doing this query:

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;

    }



}

This is the Intent that I passed in Flip.java

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

        }
    }
}

And then I tried to retrieve the results using the following code, but the problem here is that only one image path is showing. How can I make all the image path show? Please help, Im stuck.

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

I selected all the rows in table this is what I got.. enter image description here

UPDATE:

The path in row 0 and row1 now shows all the images path. However, the last index or row in the recyclerview makes the app crash.

  • you need to put String locate = dbList.get(index).getPath(); into loop and concat the output in locate variable. – Sandeep Sep 09 '16 at 06:26
  • Could it be that this is a SQL question? Have you tried this: `String query = "select * from "+ TBNAME + " where name like '%"+ name +"%';" ;` – gus27 Sep 09 '16 at 06:31
  • I see that you updated the SQL statement and included the `LIKE` term now. What is the output of `Log.d("student data", modelList.toString());`? – gus27 Sep 09 '16 at 06:45
  • What is the statement and the output of the query running in the Mozilla's SQLite plugin? – gus27 Sep 09 '16 at 06:47
  • @gus42 I posted the image above. That is the result of the query – Jake Aarons Sep 09 '16 at 06:56
  • Ok. You can change the SQL statement back to `=` instead of `LIKE`. This has nothing to do with the problem. – gus27 Sep 09 '16 at 06:58
  • What is the output of `Log.d("student data", modelList.toString());`? – gus27 Sep 09 '16 at 06:59
  • Put some more Log output in `searchFromDB`. Let it show `query` and each `name` and `path`. – gus27 Sep 09 '16 at 07:03
  • @gus42 k W/student data: [com.luminous.pick.GettersSetters@41b401b0, com.luminous.pick.GettersSetters@41b40340, com.luminous.pick.GettersSetters@41b40438] this is what the log shows – Jake Aarons Sep 09 '16 at 07:18

2 Answers2

1

In your method just change if condition block as below, it will work

  String locate = "";

  if(dbList.size() >0)
  {
      for(GettersSetters currentClass : dbList)
      {
         locate += currentClass.getPath() + ",";           
      }
  }
  txtLoc.setText(locate);
Vickyexpert
  • 3,147
  • 5
  • 21
  • 34
  • still only one image path appears – Jake Aarons Sep 09 '16 at 06:29
  • also check result you got from db query, is there more than one record or only one – Vickyexpert Sep 09 '16 at 06:33
  • I try querying this on the Mozilla's SQLite plugin and it show many rows – Jake Aarons Sep 09 '16 at 06:35
  • @Jake Aarons: Please put the statement and the result of your SQLite query in your question that we can get an idea of what the content of your database looks like. – gus27 Sep 09 '16 at 06:39
  • @JakeAarons do one thing just before set text to textview using Log check the value of locate, what you got ? – Vickyexpert Sep 09 '16 at 06:48
  • @Vickyexpert when I try running the app Row 0 in the recyclerview shows all the path of the images but in Row 1 it only show 1 path of the image and the last row producs error – Jake Aarons Sep 09 '16 at 07:04
  • Put Your Entire code how you fill up the recycleview, as from above code it is clear that you are trying to display all path in one simple text view not in list view so put your entire code for better help – Vickyexpert Sep 09 '16 at 07:07
0

check all data in your sqlite table because you are using like query, it will return all then names that relates in table raw.

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

this is your query means when you have table names like below

admin
administrator
superadmin
mainuser

when you use this query in for select data

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

then its return first three data from your table raw.

you have to use where with condition close if you want to retrive specific data.

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

then its only one result found.

  • This is asking for SQL injection. Best to use the `query` method that accepts `whereArgs` – OneCricketeer Sep 09 '16 at 06:57
  • this is for only sqlite query information not any kind of injection meaning. as you display in your table you have duplicate names in the table so you have to use id instead of the name to get particular data – Android Devs Sep 09 '16 at 07:09
  • Not sure what you mean, but using `rawQuery` with a concatenated string is a security risk. That's all I'm saying – OneCricketeer Sep 09 '16 at 07:13
  • @cricket_007 i don't know more about security of sql and sql injection but you have only two choice to get data from database is direct sql query or use function rawQuery – Android Devs Sep 09 '16 at 08:55
  • Yes the query needs written in some way, but those aren't the only two ways. String concatenation can be bad, that's my point. http://stackoverflow.com/a/9857412/2308683 – OneCricketeer Sep 09 '16 at 12:40