單機android sqlite數(shù)據(jù)庫的實現(xiàn),這個數(shù)據(jù)庫可與程序一起生成在安裝包中
達坂城ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!
一、下載sqlite3.exe文件
二、運行 cmd 轉到sqlite3.exe 所在目錄 運行 sqlite3.exe 數(shù)據(jù)庫名.db
然后會出現(xiàn)sqlite>的命令提示符
輸入創(chuàng)建表的語句, create table 表名(‘列’,‘列’。。。);(注意: 要在結束部分加 分號 )
此時會在sqlite3.exe 所在目錄,出現(xiàn)所建數(shù)據(jù)庫的文件
三、如果想在Android中運行的話,需要在數(shù)據(jù)庫中增添
CREATE TABLE "android_metadata" ("locale" TEXT DEFAULT 'zh_CN')
INSERT INTO "android_metadata" VALUES ('zh_CN') 四、將數(shù)據(jù)庫 復制到 Android項目中res/raw中 五、下面是代碼:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | public class TestSqlDatabase{ private static final String DATABASE_PATH = "/data/data/your.package.name/databases"; //此處不要改動,這個為數(shù)據(jù)庫在手機上的物理地址 private static final int DATABASE_VERSION = 0; private static final String DATABASE_NAME = "test.db"; //此處為數(shù)據(jù)庫名稱 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("創(chuàng)建失敗"); } } 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) { //參數(shù) 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(); } } |
網(wǎng)站欄目:android本地數(shù)據(jù)庫sqlite的封裝
當前路徑:http://muchs.cn/article28/geppjp.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導航、定制開發(fā)、品牌網(wǎng)站制作、網(wǎng)站收錄、用戶體驗、App開發(fā)
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)