SoFunction
Updated on 2025-04-09

Simple use of conditional database Android:sqlite


 public class DBHelper {

    private static DatabaseHelper mDbHelper;
    private static SQLiteDatabase mDb;

    private static final String   DATABASE_NAME    = "life";

    private static final int      DATABASE_VERSION = 1;

    private Context               mCtx;

    private static class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
//Create a table structure
            ("CREATE TABLE life_history (id INTEGER PRIMARY KEY AUTOINCREMENT,type TEXT, city TEXT, company TEXT, pucNum TEXT, pucName TEXT);");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        }
    }

    public DBHelper(Context ctx) throws SQLException {
        = ctx;
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = ();
    }

    /**
* Close the data source
     */
    public void closeConnection() {
        if (mDb != null && ())
            ();
        if (mDbHelper != null)
            ();
    }

    /**
* Insert data Parameters
* @param tableName table name
* @param initialValues ​​The corresponding value of the column to be inserted
     * @return
     */
    public long insert(String tableName, ContentValues initialValues) {

        return (tableName, null, initialValues);
    }

    /**
* Delete data
* @param tableName table name
* @param deleteCondition
* @param deleteArgs The value corresponding to the condition (if there is a "?" sign in deleteCondition, it will be replaced by the values ​​in this array, one by one)
     * @return
     */
    public boolean delete(String tableName, String deleteCondition, String[] deleteArgs) {

        return (tableName, deleteCondition, deleteArgs) > 0;
    }

    /**
* Update data
* @param tableName table name
* @param initialValues ​​Column to be updated
* @param selection Updated conditions
* @param selectArgs Update the value corresponding to "?" in the condition
     * @return
     */
    public boolean update(String tableName, ContentValues initialValues, String selection, String[] selectArgs) {
        return (tableName, initialValues, selection, selectArgs) > 0;
    }

    /**
* Get a list
* @param distinct Do you want to repeat it
* @param tableName table name
* @param columns to return
* @param selection condition
* @param selectionArgs Parameter value of "?" in the condition
* @param groupBy Group
* @param having group filtering conditions
* @param orderBy sort
     * @return
     */
    public Cursor findList(boolean distinct, String tableName, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit) {

        return (distinct, tableName, columns, selection, selectionArgs, groupBy, having, orderBy, limit);
    }

    /**
* Obtain a single line record
* @param tableName table name
* @param columns Get array of columns
* @param selection condition
* @param selectionArgs The value corresponding to "?" in the condition
* @param groupBy Group
* @param having grouping conditions
* @param orderBy sort
* @param limit Data interval
* @param distinct Do you want to repeat it
     * @return
     * @throws SQLException
     */
    public Cursor findOne(boolean distinct, String tableName, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit) throws SQLException {

        Cursor mCursor = findList(distinct, tableName, columns, selection, selectionArgs, groupBy, having, orderBy, limit);

        if (mCursor != null) {
            ();
        }
        return mCursor;

    }

    /**
* Execute SQL (with parameters)
     * @param sql
* @param args The parameter value of "?" in SQL
     */
    public void execSQL(String sql, Object[] args) {
        (sql, args);

    }

    /**
* Execute SQL
     * @param sql
     */
    public void execSQL(String sql) {
        (sql);

    }

    /**
* Determine whether a table exists
* @param tabName table name
     * @return
     */
    public boolean isTableExist(String tableName) {
        boolean result = false;
        if (tableName == null) {
            return false;
        }

        try {
            Cursor cursor = null;
            String sql = "select count(1) as c from sqlite_master where type ='table' and name ='" + () + "'";
            cursor = (sql, null);
            if (()) {
                int count = (0);
                if (count > 0) {
                    result = true;
                }
            }

            ();
        }
        catch (Exception e) {
        }
        return result;
    }

    /**
* Determine whether a certain field exists in a certain table (Note, this method cannot determine whether the table exists, so it should be applied together with isTableExist)
* @param tabName table name
* @param columnName columnname
     * @return
     */
    public boolean isColumnExist(String tableName, String columnName) {
        boolean result = false;
        if (tableName == null) {
            return false;
        }

        try {
            Cursor cursor = null;
            String sql = "select count(1) as c from sqlite_master where type ='table' and name ='" + () + "' and sql like '%" + () + "%'";
            cursor = (sql, null);
            if (()) {
                int count = (0);
                if (count > 0) {
                    result = true;
                }
            }

            ();
        }
        catch (Exception e) {
        }
        return result;
    }

}