1

I am using sqlite i want to print the query executed in db to insert

here is my code

// for SAving Ocean/Air sales
public int saveOrder(Order odr) throws SQLException  {
    SQLiteDatabase db = con.getWritableDatabase();
    int ordrId = 0;

    ContentValues values = new ContentValues();         
    values.put("cr_usr", odr.getCrUsr());
    values.put("cr_ts", odr.getCrTs().toString());
    values.put("eat_mst_cust_id", odr.getEatMstCustId());
    values.put("ordr_dt", odr.getOrdrDt().toString());  
    String selectQuery = "SELECT last_insert_rowid()";  
    try {

        // Inserting Row
        db.insertOrThrow("eat_ordr", null, values);---getting error here for constraint failed
        Cursor cursor = db.rawQuery(selectQuery, null);
        cursor.moveToFirst();
        ordrId = cursor.getInt(0);
        db.close();     
    } finally {
        db.close();
    }
return ordrId;
}

I am not getting any error but row is failed to insert bcz it returns 0 for idvalue so i want to see executed query how to get that query?

here is my table structure

CREATE TABLE "eat_ordr" ("eat_ordr_id" INTEGER PRIMARY KEY AUTOINCREMENT  NOT NULL , 
"eat_mst_cust_id" VARCHAR NOT NULL  REFERENCES "eat_mst_cust"("eat_mst_cust_id"), 
"ordr_no" VARCHAR NOT NULL  UNIQUE , 
"ordr_dt" DATETIME NOT NULL , 
"ordr_stat" VARCHAR NOT NULL ,
"last_sync_ts" DATETIME, 
"cr_ts" DATETIME DEFAULT CURRENT_TIMESTAMP, "md_ts" DATETIME, "cr_usr" VARCHAR, "md_usr" VARCHAR)
1
  • the code comment says getting error here while your description says I am not getting any error. if you want the reason for the error: ordr_no VARCHAR NOT NULL UNIQUE is not met you have not provided a value for it. Commented Jul 16, 2014 at 14:53

1 Answer 1

2

The insertOrThrow documentation says:

Returns
the row ID of the newly inserted row

So this can be done much easier:

ordrId = db.insertOrThrow("eat_ordr", null, values);
Sign up to request clarification or add additional context in comments.

2 Comments

You are not doing that, and the separate query does not work. You can check the return value.
I have not changed here.. but yes I get id value if success else catch (IllegalStateException ise) { ise.getStackTrace(); } gives me query string @ thanks CL for help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.