paul
paul

Reputation: 95

Check if query was successful in sqlite for Android

In order to insert data into a sqlite table named mytable in android I use query:

db.execSQL("INSERT INTO MYTABLE VALUES('','"+name+"',NOW());");

I want to check whether this query was successfully inserted into the table or not. In php (with mysql) you could easily do

if($result) 
    echo 'success';
else        
    echo 'not inserted';

But what is the equivalent code in android (with sqlite) to check whether the query has been successfully executed without any error?

Upvotes: 5

Views: 14342

Answers (1)

Erik
Erik

Reputation: 2264

According to the documentation execSQL throws an SQLException so I think your best bet is to surround your execSQL call with a try catch. You can then deal with whatever errors occurs in the catch. So something like this is what you want

try {
  db.execSQL("INSERT INTO MYTABLE VALUES('','"+name+"',NOW());");
} catch (SQLException e) {
  ...deal with error
}

Upvotes: 11

Related Questions