Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 387
paul Avatar asked Sep 27 '12 14:09

paul


1 Answers

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
}
like image 77
Erik Avatar answered Oct 10 '22 02:10

Erik