Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLiteDatabase insert function returns -1

I have a database which is calling Medicine, there are four columns in it: id (primary key autoincrement), dId (foreign key), Name and Time. I try to add new row using insert function. This function must return id of this adding. But when I check this from debugger, I see that it returned -1.

Code:

        ContentValues values = new ContentValues();
        values.put("RECIPE_ID", recipeId);
        values.put("MED_NAME", etMedName.getText().toString());
        values.put("MED_TIME", etMedTime.getText().toString());
        long mid = sqdb.insert("Medicine", null, values);
like image 586
Askar Zaitov Avatar asked Jan 13 '23 09:01

Askar Zaitov


1 Answers

use

 long mid = 0;

try
    {
        mid = sqdb.insertOrThrow("Medicine", null, values);
    }
    catch(SQLException e)
    {
        // Sep 12, 2013 6:50:17 AM
        Log.e("Exception","SQLException"+String.valueOf(e.getMessage()));
        e.printStackTrace();
    }

It will tell you the probable Exception

like image 117
Trikaldarshiii Avatar answered Jan 21 '23 16:01

Trikaldarshiii