单机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中
五、下面是代码:
| 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 | publicclassTestSqlDatabase{        privatestaticfinalString DATABASE_PATH = "/data/data/your.package.name/databases";     //此处不要改动,这个为数据库在手机上的物理地址    privatestaticfinalintDATABASE_VERSION = 0;    privatestaticfinalString DATABASE_NAME = "test.db";  //此处为数据库名称        privatestaticString outFileName = DATABASE_PATH + "/"+ DATABASE_NAME;        privateContext context;     privateSQLiteDatabase database;        publicTestSqlDatabase(Context context) {        this.context = context;                File file = newFile(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();        }            }    privatevoidbuildDatabase() throwsException{        InputStream myInput = context.getResources().openRawResource(R.raw.test);        File file = newFile(outFileName);                File dir = newFile(DATABASE_PATH);        if(!dir.exists()) {            if(!dir.mkdir()) {                thrownewException("创建失败");            }        }                if(!file.exists()) {                       try{                OutputStream myOutput = newFileOutputStream(outFileName);                                byte[] buffer = newbyte[1024];                intlength;                while((length = myInput.read(buffer))>0){                    myOutput.write(buffer, 0, length);                }                myOutput.close();                myInput.close();            } catch(Exception e) {                e.printStackTrace();            }                }    }/** * 查找 * @return */publicCursor select() {     database = SQLiteDatabase.openOrCreateDatabase(outFileName, null);     String sql = "select * from note_table";             Cursor cursor = database.rawQuery(sql, null);     returncursor;}/** * 插入 * @param word * @param note * @return */publiclonginsert(String word, String note) {      database = SQLiteDatabase.openOrCreateDatabase(outFileName, null);      ContentValues cv = newContentValues();      cv.put("word", word);      cv.put("note", note);                  longresult = database.insert("note_table", null, cv);          returnresult;}  /** * 更新 * @param word * @param note * @return     */    privateintupdate(String word, String note) {                            //参数 word 为修改条件   note为修改内容        database = SQLiteDatabase.openOrCreateDatabase(outFileName, null);                ContentValues cv = newContentValues();        cv.put("note", note);                intresult = database.update("note_table", cv, "word=?", newString[]{word});                     returnresult;    } /** * 删除 * @param word */publicintdeleteNote(String word) {      database = SQLiteDatabase.openOrCreateDatabase(outFileName, null);      intresult = database.delete("note_table", "word=?", newString[]{word});      returnresult;}     publicvoidclose() {      database.close();}} | 
android 本地数据库sqlite的封装,布布扣,bubuko.com
原文:http://www.cnblogs.com/lizhiyan-world/p/3740351.html