Check if the query was successful in sqlite for Android

To insert data into sqlite table named mytable in android, I use the query:

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

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

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

But what is the equivalent code in android (with sqlite) to check if the query was successfully executed without errors ?

+4
source share
1 answer

According to the documentation, execSQL throws a SQLException , so I believe it is best to surround your execSQL call with a catch attempt. You can then deal with what errors occur in the trick. So, something like what you want

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

Source: https://habr.com/ru/post/1436625/


All Articles