SQLCipher的导入和基本使用

   SQLCipher是分为社区版和商业版,我这里现在只介绍社区版怎么使用:

   官网,里面可以看到应该导入什么依赖SQLCipher官网

      

import android.content.Context;

import net.sqlcipher.database.SQLiteDatabase;
import net.sqlcipher.database.SQLiteOpenHelper;

public class MyDatabaseOpenHelper extends SQLiteOpenHelper {

    public MyDatabaseOpenHelper(Context context) {
        super(context, "chenye.db", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table if not exists Person(_id Integer PRIMARY KEY AUTOINCREMENT,NAME varchar(32),AGE varchar(32))");
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
}

import android.content.ContentValues;
import android.content.Context;
import android.util.Log;

import net.sqlcipher.Cursor;
import net.sqlcipher.database.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

public class DbManager {

    private static final String TAG = "cy===DbManager";
    private SQLiteDatabase mDb;

    private DbManager() {

    }

    private static final class Holder {
        private static final DbManager INSTANCE = new DbManager();
    }

    public static DbManager getInstance() {
        return Holder.INSTANCE;
    }

    public DbManager init(Context context) {
        SQLiteDatabase.loadLibs(context);
        MyDatabaseOpenHelper helper = new MyDatabaseOpenHelper(context);
        mDb = helper.getWritableDatabase("YEDASHEN");
        return this;
    }

    public boolean insert(List persons) {
        boolean flag = true;
        ContentValues values = new ContentValues();
        for (Person person : persons) {
            values.clear();
            values.put("name", person.getName());
            values.put("age", person.getAge());
            long person1 = mDb.insert("Person", null, values);
            flag = person1 > 0;
        }
        return flag;
    }

    public List query() {
        List persons = new ArrayList<>();
        Cursor cursor = mDb.rawQuery("select * from Person", null);
        while (cursor.moveToNext()) {
            Person person = new Person();
            try {
                person.setName(cursor.getString(cursor.getColumnIndex("NAME")));
                person.setAge(cursor.getString(cursor.getColumnIndex("AGE")));
                persons.add(person);
            } catch (Exception e) {
                Log.i(TAG, e.toString());
            }
        }
        cursor.close();
        return persons;
    }
}

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "cy===MainActivity";
    private DbManager mDbManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDbManager = DbManager.getInstance().init(this);
        insertData();
        query();
    }

    private void insertData() {
        List persons = new ArrayList<>();
        persons.add(new Person("CY", "25"));
        persons.add(new Person("YL", "24"));
        persons.add(new Person("HY", "24"));

        if (mDbManager.insert(persons)) {
            Toast.makeText(this, "插入数据成功", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "插入数据失败", Toast.LENGTH_SHORT).show();
        }
    }

    public void query() {
        List persons = mDbManager.query();
        Log.i(TAG, persons.toString());
    }
}


你可能感兴趣的:(数据库)