Execute a single SQL statement, eating any exception that occurs. - Android Database

Android examples for Database:SQL Query

Description

Execute a single SQL statement, eating any exception that occurs.

Demo Code


import java.util.ArrayList;
import java.util.Locale;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;

public class Main{
    /**//from  ww  w. j  a va2s .c o m
     * Execute a single SQL statement, eating any exception that occurs.
     *
     * @param db The database against which to execute the query.
     * @param sql The SQL statement to execute.
     * @param bindArgs An array of arguments to the query.
     * @return {@code true} if successful, {@code false} otherwise.
     */
    public static boolean safeExecSql(SQLiteDatabase db, String sql,
            Object[] bindArgs) {
        try {
            if (bindArgs == null) {
                db.execSQL(sql);
            } else {
                db.execSQL(sql, bindArgs);
            }
        } catch (SQLiteException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
    /**
     * Execute a single SQL statement, eating any exception that occurs.
     *
     * @param db The database against which to execute the query.
     * @param sql The SQL statement to execute.
     * @return {@code true} if successful, {@code false} otherwise.
     */
    public static boolean safeExecSql(SQLiteDatabase db, String sql) {
        try {
            db.execSQL(sql);
        } catch (SQLiteException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}

Related Tutorials