Database Knowledge SQLite

Source: Internet
Author: User
Tags sqlite



Mysqliteopenhelper-Sqlitedatabase


import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class AppLockOpenHelper extends SQLiteOpenHelper {
// Set the database name version number
     public AppLockOpenHelper (Context context) {
         super (context, AppLockConstants.DB_NAME, null, AppLockConstants.DB_VERSION);
     }
    // Create database table structure operation
     @Override
     public void onCreate (SQLiteDatabase db) {
         db.execSQL (AppLockConstants.CREATE_TABLE);
     } 


public static final String create_table = "CREATE TABLE" + table_name
+ "(" + ID + "Integer primary key AutoIncrement," + PackageName
+ "varchar (100))";



// when the database version has changed


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

    private AppLockOpenHelper openHelper;
    private Context mContext;

    public AppLockDao (Context context) {
        openHelper = new AppLockOpenHelper (context);
        this.mContext = context;
    }
public boolean add (String packageName) {
        SQLiteDatabase database = openHelper.getWritableDatabase ();
        ContentValues values = new ContentValues ();
        // parameter 1: the name of the field in the table
        // Parameter 2: the value corresponding to the field
        values.put (AppLockConstants.PACKAGENAME, packageName);
        // Parameter 1: table name
        // Parameter 2: When adding data, you can add null data
        // Parameter 3: added data
        long insert = database
                .insert (AppLockConstants.TABLE_NAME, null, values);

        // Notify the content observer that the database has been updated
        ContentResolver contentResolver = mContext.getContentResolver ();
        // notify the content observer that the data has changed
        Uri uri = Uri.parse ("content: //com.z.mobliesafe.unlock");
        // Parameter 1: uri address
        // Parameter 2: which content observer to notify, if it is null, it means to notify all content observers registered through uri
        contentResolver.notifyChange (uri, null);

        // determine whether the addition was successful
        return insert! = -1;
    }
public boolean delete (String packageName) {
        SQLiteDatabase database = openHelper.getWritableDatabase ();
        // Parameter 2: query condition where id = ...
        // Parameter 3: parameter of query condition
        int delete = database.delete (AppLockConstants.TABLE_NAME,
                AppLockConstants.PACKAGENAME + "=?",
                new String [] {packageName});

        // Notify the content observer that the database has been updated
        ContentResolver contentResolver = mContext.getContentResolver ();
        // notify the content observer that the data has changed
        Uri uri = Uri.parse ("content: //com.z.mobliesafe.unlock");
        // Parameter 1: uri address
        // Parameter 2: which content observer to notify, if it is null, it means to notify all content observers registered through uri
        contentResolver.notifyChange (uri, null);

        // determine whether the deletion was successful
        return delete! = 0;
    }
// Query a single data
    / **
     * Check whether the package name of the application is in the database and determine whether the application is locked. True: locked, false: unlocked (no lock)
     * 3:09:23 pm
     * /
    public boolean queryOne (String packageName) {
        boolean isHave = false;
        SQLiteDatabase database = openHelper.getReadableDatabase ();
        // Parameter 2: Set which data to query
        // Parameter 3,4: query conditions and parameters of query conditions
        // Parameter 5: grouping
        // Parameter 6: deduplication
        // parameter 7: sort
        Cursor cursor = database.query (AppLockConstants.TABLE_NAME,
                new String [] {AppLockConstants.PACKAGENAME},
                AppLockConstants.PACKAGENAME + "=?",
                new String [] {packageName}, null, null, null);
        // If there is only one data query, you can use if without using while
        if (cursor.moveToNext ()) {
            // Because you only need to know whether the package name of the application is in the database, you don't need to use the query data
            isHave = true;
        }
        // close the database
        cursor.close ();
        database.close ();
        return isHave;
    }
// Query a single data
    / **
     * Check whether the package name of the application is in the database and determine whether the application is locked. True: locked, false: unlocked (no lock) 2016-11-21
     * 3:09:23 pm
     * /
    public boolean queryOne (String packageName) {
        boolean isHave = false;
        SQLiteDatabase database = openHelper.getReadableDatabase ();
        // Parameter 2: Set which data to query
        // Parameter 3,4: query conditions and parameters of query conditions
        // Parameter 5: grouping
        // Parameter 6: deduplication
        // parameter 7: sort
        Cursor cursor = database.query (AppLockConstants.TABLE_NAME,
                new String [] {AppLockConstants.PACKAGENAME},
                AppLockConstants.PACKAGENAME + "=?",
                new String [] {packageName}, null, null, null);
        // If there is only one data query, you can use if without using while
        if (cursor.moveToNext ()) {
            // Because you only need to know whether the package name of the application is in the database, you don't need to use the query data
            isHave = true;
        }
        // close the database
        cursor.close ();
        database.close ();
        return isHave;
    }
// query all data
    / **
     * Query all locked applications
     *
     * 2016-11-21 3:19:38 PM
     * /
    public List <String> queryAll () {

        SystemClock.sleep (2000);

        List <String> list = new ArrayList <String> ();

        SQLiteDatabase database = openHelper.getReadableDatabase ();
        // Parameter 2: Set which data to query
        // Parameter 3,4: query conditions and parameters of query conditions
        // Parameter 5: grouping
        // Parameter 6: deduplication
        // parameter 7: sort
        Cursor cursor = database.query (AppLockConstants.TABLE_NAME,
                new String [] {AppLockConstants.PACKAGENAME}, null, null,
                null, null, null);
        while (cursor.moveToNext ()) {
            String packageName = cursor.getString (0);

            // In order to facilitate listview display data, save the bean class to the collection
            list.add (packageName);
        }
        // close the database
        cursor.close ();
        database.close ();
        return list;
    } 




Database Knowledge SQLite


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.