Use SqlCipher in android studio

Source: Internet
Author: User

Use SqlCipher in android studio

Android studio and eclipse are slightly different.
Android studio 1.3
SqlCipher version 3.3.1
1. Copy sqlcipher. jar to the project folder libs;
Replicated to assets;
The structure is as follows:

3. Load the class

@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); ButterKnife. bind (this); SQLiteDatabase. loadLibs (this); // load before using this class, and only load once}

Note that the imported package is: import net. sqlcipher. database. SQLiteDatabase;

4. Pay attention to the details when creating a database
This method cannot open the database

      database = SQLiteDatabase.openOrCreateDatabase(data, 123456, null);        if (database != null) {            database.execSQL(CREATE TABLE IF NOT EXISTS person_student(name VARCHAR(20) NOT NULL , age INT(3)));        }

This is acceptable.

    File file = context.getDatabasePath(data);        file.mkdirs();        database = SQLiteDatabase.openOrCreateDatabase(file, 123456, null); //        if (database != null) {            database.execSQL(CREATE TABLE IF NOT EXISTS person_student(name VARCHAR(20) NOT NULL , age INT(3)));        }

To compare the two methods, you need to manually create a folder.

The usage is basically the same, and its encryption and decryption are all completed internally, which is basically irrelevant to the program we write. The main function is to prevent others from directly viewing the plaintext database through the root permission.

 

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.