天天看點

android 本地資料庫sqlite的封裝

 單機android   sqlite資料庫的實作,這個資料庫可與程式一起生成在安裝包中

一、下載下傳sqlite3.exe檔案

二、運作 cmd 轉到sqlite3.exe 所在目錄  運作 sqlite3.exe 資料庫名.db

    然後會出現sqlite>的指令提示符

    輸入建立表的語句, create table 表名(‘列’,‘列’。。。);(注意: 要在結束部分加  分号 )

    此時會在sqlite3.exe 所在目錄,出現所建資料庫的檔案

三、如果想在Android中運作的話,需要在資料庫中增添

public class TestSqlDatabase{

    private static final String DATABASE_PATH = "/data/data/your.package.name/databases";     //此處不要改動,這個為資料庫在手機上的實體位址

    private static final int DATABASE_VERSION = 0;

    private static final String DATABASE_NAME = "test.db";  //此處為資料庫名稱

    private static String outFileName = DATABASE_PATH + "/" + DATABASE_NAME;

    private Context context;

     private SQLiteDatabase database;

    public TestSqlDatabase(Context context) {

        this.context = context;

        File file = new File(outFileName);

        if (file.exists()) {

            database = SQLiteDatabase.openOrCreateDatabase(outFileName, null);

            if (database.getVersion() != DATABASE_VERSION) {

                database.close();

                file.delete(); 

            }

        }

        try {

            buildDatabase();

        } catch (Exception e) {

            e.printStackTrace();

    }

    private void buildDatabase() throws Exception{

        InputStream myInput = context.getResources().openRawResource(R.raw.test);

        File dir = new File(DATABASE_PATH);

        if (!dir.exists()) {

            if (!dir.mkdir()) {

                throw new Exception("建立失敗");

        if (!file.exists()) {          

            try {

                OutputStream myOutput = new FileOutputStream(outFileName);

                byte[] buffer = new byte[1024];

                int length;

                while ((length = myInput.read(buffer))>0){

                    myOutput.write(buffer, 0, length);

                }

                myOutput.close();

                myInput.close();

            } catch (Exception e) {

                e.printStackTrace();

/**

 * 查找

 * @return

 */

public Cursor select() {

     database = SQLiteDatabase.openOrCreateDatabase(outFileName, null);

     String sql = "select * from note_table";

     Cursor cursor = database.rawQuery(sql, null);

     return cursor;

}

 * 插入

 * @param word

 * @param note

public long insert(String word, String note) {

      database = SQLiteDatabase.openOrCreateDatabase(outFileName, null);

      ContentValues cv = new ContentValues();

      cv.put("word", word);

      cv.put("note", note);

      long result = database.insert("note_table", null, cv);   

      return result;

 * 更新

     */

    private int update(String word, String note) {                            //參數 word 為修改條件   note為修改内容

        database = SQLiteDatabase.openOrCreateDatabase(outFileName, null);

        ContentValues cv = new ContentValues();

        cv.put("note", note);

        int result = database.update("note_table", cv, "word=?", new String[]{word});    

        return result;

 * 删除

public int deleteNote(String word) {

      int result = database.delete("note_table", "word=?", new String[]{word});

public void close() {

      database.close();

本文轉自xsster51CTO部落格,原文連結:http://blog.51cto.com/12945177/1930153 ,如需轉載請自行聯系原作者