android存储之SQLite数据库
来源:互联网 发布:转发软件拿货 编辑:程序博客网 时间:2024/04/30 12:24
特点
1.轻量级 一个动态库、单文件
2.独立性 没有依赖、无需安装
3.隔离性 全部在一个文件夹中
4.多语言接口 支持众多编程语言
5.跨平台 支持众多系统操作
6.安全性 事务
关于事务处理的安全性问题:
-通过数据库上的独占性和共享锁来实现独立事务处理
-多个进程可以在同一时间从同一数据库读取数据,但只有一个可以写入数据
关于SQLite的数据类型
-SQLite支持NULL、INTEGER、REAL、TEXT、BLOB数据类型
-依次代表:空值、整型值、浮点值、字符串值、二进制对象
动态数据类型(弱引用)
-当某个值插入到数据库时,SQLite将会检查它的类型,如果该类型与关联的列不匹配,SQLite则会尝试将该值转换成该列的类型,如果不能转换,则该值将作为本身的类型存储。
SQLiteOpenHelper类
SQLiteOpenHelper 是Android提供的一个管理数据库的工具类,可用于管理数据库的创建和版本更新。
–synchronized SQLiteDataBase getReadableDatabase():以读写的方式打开数据库对应的SQLiteDatabase对象
–synchronized SQLiteDataBase getWriteableDatabase():以写的方式打开数据库对应的SQLiteDatabase对象
–abstract void onCreate(SQLiteDatabase db):第一次创建数据库时回调该方法
–abstract void onUpgrade(SQLiteDatabase db,int odlVersion,int newVerison):当数据库版本更新时回调该方法
–synchronized void close():关闭所有SQLiteDatabase对象
代码实例:实现生词本的添加和查询功能
public class SDbActivity extends AppCompatActivity { MyDatabaseHelper helper; private Button insert = null; private Button search = null; private EditText etWord, etDetail, etSearch; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sqlite); helper = new MyDatabaseHelper(this, "city.db", 1); etWord = (EditText) findViewById(R.id.et_word); etDetail = (EditText) findViewById(R.id.et_detail); etSearch = (EditText) findViewById(R.id.etSearch); insert = (Button) findViewById(R.id.btAdd); search = (Button) findViewById(R.id.btSearch); insert.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String word = etWord.getText().toString().trim(); String detail = etDetail.getText().toString().trim(); insertData(helper.getReadableDatabase(), word, detail); Toast.makeText(SDbActivity.this, "添加成功", Toast.LENGTH_SHORT).show(); } }); search.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String seekWord = etSearch.getText().toString().trim(); Cursor c = helper.getReadableDatabase().rawQuery("select * from dict where word like ? or detail like?", new String[]{"%" + seekWord + "%", "%" + seekWord + "%"}); if (c != null) { Bundle data = new Bundle(); data.putSerializable("data", conCursorToList(c)); Intent i = new Intent(SDbActivity.this, ResultActivity.class); i.putExtras(data); startActivity(i); } c.close(); } }); } private void insertData(SQLiteDatabase db, String word, String detail) { db.execSQL("insert into dict values(null,?,?)", new String[]{word, detail}); db.close(); } private ArrayList<Map<String, String>> conCursorToList(Cursor c) { ArrayList<Map<String, String>> result = new ArrayList<Map<String, String>>(); while (c.moveToNext()) { Map<String, String> map = new HashMap<>(); map.put("word", c.getString(1)); map.put("detail", c.getString(2)); result.add(map); } return result; }} public class ResultActivity extends AppCompatActivity { private TextView tv1, tv2; ListView listView; SimpleAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_res); tv1 = (TextView) findViewById(R.id.tvWord); tv2 = (TextView) findViewById(R.id.tvDetail); listView = (ListView) findViewById(R.id.show); Intent intent = getIntent(); Bundle data = intent.getExtras(); List<Map<String, String>> list = (List<Map<String, String>>) data.getSerializable("data"); adapter = new SimpleAdapter(this, list, R.layout.line, new String[]{"word", "detail"}, new int[]{R.id.tvWord, R.id.tvDetail}); listView.setAdapter(adapter); }}
XML文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:id="@+id/et_word" android:layout_width="match_parent" android:layout_height="wrap_content" /> <EditText android:id="@+id/et_detail" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btAdd" android:text="添加生词" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <EditText android:id="@+id/etSearch" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btSearch" android:text="查找单词" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout> <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ListView android:id="@+id/show" android:layout_width="wrap_content" android:layout_height="wrap_content"></ListView></LinearLayout> <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="找到的单词" /> <TextView android:id="@+id/tvWord" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="解释" /> <TextView android:id="@+id/tvDetail" android:layout_width="match_parent" android:layout_height="wrap_content" /></LinearLayout>
- Android存储之SQLite数据库
- android存储之SQLite数据库
- Android数据存储之SQLite数据库存储
- Android数据存储之SQLite数据库存储
- Android数据存储之SQLite数据库存储
- Android数据存储之:SQLite数据库存储
- Android 数据存储之SQLite数据库存储
- Android数据存储之SQLite数据库存储
- Android 数据存储之 SQLite嵌入式数据库
- android学习笔记之SQLite数据库存储
- Android数据存储之Sqlite数据库
- android开发之使用SQLite数据库存储
- Android 数据存储 之 SQLite数据库详解
- Android 数据存储 之 SQLite数据库详解
- Android 数据存储 之 SQLite数据库详解
- Android 数据存储 之 SQLite数据库详解
- Android 数据存储 之 SQLite数据库详解
- Android笔记之存储(SQLite数据库)
- zookeeper源码阅读准备工作
- php本地地址改成域名访问
- MySQL由浅入深理解索引的实现
- fileupload上传工具类
- 从一杯果汁浅谈点点医生充值提现模块设计
- android存储之SQLite数据库
- 剑指Offer面试题13[在O(1)时间删除表的结点]
- B
- qt开发环境
- Bit-map空间压缩和快速排序去重
- 新建仓库和上传本地文件到github
- ALLEGRO学习之晶振布局以及布线
- hdu 6125 分组背包+状态压缩
- 8月15集训