-1

I would like to get the last inserted ID of my SQLite table, when i have this id i can add it to my naming convention to create unique and corresponding names for my jpg.

This is my function for creating the image.

private String saveToInternalStorage(Bitmap bitmapImage){
        ContextWrapper cw = new ContextWrapper(getApplicationContext());
        // path to /data/data/yourapp/app_data/imageDir
        File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
        // Create imageDir
        int id = 999;
        id = helper.getID();
        File mypath=new File(directory,"Photo" + id +".jpg");

        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(mypath);
            // Use the compress method on the BitMap object to write image to the OutputStream
            bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return directory.getAbsolutePath();
    }

Now i want to increment the ID depending on the rows of my SQLite table. So if row 7 gets inserted i would like to name my picture like Photo7.jpg

And this is my datahelper class where i call the insert:

package com.example.testapplication;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

import static android.icu.text.MessagePattern.ArgType.SELECT;

public class DatabaseHelper extends SQLiteOpenHelper {
    public static final String COL_1 = "Image";
    public static final String COL_2 = "Note";
    public static final String COL_3 = "Sort";
    public static final String COL_4 = "Weight";
    public static final String COL_5 = "Length";
    public static final String COL_6 = "Pond";

    public static final String Database = "Catch.db";
    public static final String Table = "CatchInformation";


    public DatabaseHelper(Context context){
        super(context, Database, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE "+ Table+ "(Id Integer PRIMARY KEY AUTOINCREMENT, IMAGE TEXT, NOTE TEXT, SORT TEXT, WEIGHT TEXT, LENGTH TEXT, POND TEXT)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS "+Table+"");
        onCreate(db);
    }

    public boolean insertData(String image, String note, String sort, String weight, String length, String pond)throws SQLiteException{
        long result;

        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();

        contentValues.put(COL_1, image);
        contentValues.put(COL_2, note);
        contentValues.put(COL_3, sort);
        contentValues.put(COL_4, weight);
        contentValues.put(COL_5, length);
        contentValues.put(COL_6, pond);
        result = db.insert(Table, null, contentValues);

        if(result == -1)
            return false;
        else
            return true;
    }

    public int getID(){
        int receivedID = 0;

        receivedID = ???;
        return receivedID;
    }

}

What should i fill in on the "???"

Thanks in advance!

1 Answers1

0

In your SQLite code, you should execute a query with the following SQL immediately after the query to insert the row, the query should return an integer (maybe a long integer), you should then return that as a return value in your getID method.

select seq from sqlite_sequence where name="table_name"
Michael Dougan
  • 1,698
  • 1
  • 9
  • 13