Android数据存储学习笔记

来源:互联网 发布:小说 知乎 编辑:程序博客网 时间:2024/06/10 09:03

1.Sharedpreferences

类型为xml,存储为键值对的形式(key-value)。

如下四行就可以实现,徒手敲的没试过。

SharedPreferences sharedPreferencesMy = getSharedPreferences("wokao(filename)", MODE_PRIVATE);SharePreferences.Editor editorMy = sharedPreferencesMy.edit();editorMy.putString("wokao(key)","niubi(value)");editorMy.commit();

这样会在root/data/data/packagename/shared_prefs/wokao(filename)找到刚才创建的东西,打开后可以看到在<map>节点下找到wokao(key)-niubi(value)。

2.SQLite

用数据库存储信息,不知道支持第几版SQL标准。

如下四行就可以实现,还是按回忆敲的没试过。

SQLiteDatabase wokaoThisDB = openOrCreateDatabase("wokao.db", MODE_PRIVATE, null);//创建执行对象和数据库文件wokaoThisDB.execSQL("CREATE TABLE if not exists wokaoDB (_id integer primary key auto increment, wokao text not null, niubi text not null)");//执行SQL语句创建数据表wokaoThisDB.execSQL("INSERT INTO wokaoDB (wokao, niubi) VALUES ('sure','it is')");//在刚才创建的表格中添加数据项Cursor wokaoContent = wokaoDB.execSQL("SELECT * FROM wokaoDB");//查询刚才插入的数据到Cursor(保存查询结果的东西)while(wokaoContent.moveToNext()){Toast.makeText(this, wokaoContent.getString(wokaoContent.getColumnIndex("wokao")), Toast.LENGTH_SHORT)}//一次只能显示一个

这样会在root/data/data/packagename/databases/wokao.db找到刚才创建的数据库,可以用SQLite Editor.apk在手机上查看,或者用navicat在电脑上查看。





0 0
原创粉丝点击