android 本地数据库sqlite的封装
生活随笔
收集整理的這篇文章主要介紹了
android 本地数据库sqlite的封装
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?單機android ? sqlite數據庫的實現,這個數據庫可與程序一起生成在安裝包中
一、下載sqlite3.exe文件
二、運行 cmd 轉到sqlite3.exe?所在目錄 ?運行?sqlite3.exe 數據庫名.db
? ??然后會出現sqlite>的命令提示符
? ? 輸入創建表的語句, create table 表名(‘列’,‘列’。。。);(注意: 要在結束部分加 ?分號 )
? ??此時會在sqlite3.exe?所在目錄,出現所建數據庫的文件
三、如果想在Android中運行的話,需要在數據庫中增添
?CREATE?TABLE?"android_metadata"?("locale"?TEXT?DEFAULT?'zh_CN')INSERT?INTO?"android_metadata"?VALUES?('zh_CN')四、將數據庫?復制到?Android項目中res/raw中五、下面是代碼:| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 | 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 file =?new?File(outFileName);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* @return*/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;}/*** 更新* @param word* @param note* @return*/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;}/*** 刪除* @param word*/public?int?deleteNote(String word) {database = SQLiteDatabase.openOrCreateDatabase(outFileName,?null);int?result = database.delete("note_table",?"word=?",?new?String[]{word});return?result;}public?void?close() {database.close();}} |
轉載于:https://blog.51cto.com/12945177/1930153
總結
以上是生活随笔為你收集整理的android 本地数据库sqlite的封装的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 小白的一周学习汇总!
- 下一篇: 数据库监控框架 oneproxy-mon