SQLite初学
来源:互联网 发布:mac怎么免费下载软件 编辑:程序博客网 时间:2024/06/09 14:25
1.打开或者创建数据库(调用API ContextWrapper中的方法)
SQLiteDatabase sdb = openOrCreateDatabase("contacts.db", Context.MODE_PRIVATE,null);
参数分别为:String name, int mode, CursorFactory factory(暂时没有工厂模式)
2.查询数据
String sql = "select * from (String (表名) name)"
Cursor cursor = sdb.rawQuery(sql,null)
3.显示数据
ListView lsv=(ListView) findViewById(R.id.listId);
SimpleCursorAdapter adapter=new SimpleCursorAdapter(
this, android.R.layout.simple_list_item_2,
cursor,
new String[]{"phone","name"},//表中的列名
new int[]{android.R.id.text1,android.R.id.text2},
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
lsv.setAdapter(adapter);
4.注册上下文菜单(删除对象)
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
menu.add(Menu.NONE, 110, 101, "删除");
}
5.处理上下文的点击事件
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo acm=(AdapterContextMenuInfo) item.getMenuInfo();
long id=acm.id;//此id值会对应数据库表中的主键值
if(item.getItemId()==110){
//删除数据
sdb.delete("contact", "_id=?",new String[]{String.valueOf(id)});
//重新查询
cursor=sdb.rawQuery("select * from contact", null);
//通知更新
adapter.changeCursor(cursor);
}
return super.onContextItemSelected(item);
}
6.Activity页面销毁自动执行的方法
@Override
protected void onDestroy() {
super.onDestroy();
if(cursor!=null)cursor.close();
if(sdb!=null)sdb.close();
}
- SQLite初学
- 初学SQLite
- 初学sqlite 锁协议
- 初学Android之SQLite
- 初学安卓路之初识SQLite
- Android学习(二)(初学)SQLite存储方式
- 初学Android,数据存储之使用SQLite数据库(四十五)
- 初学Android,数据存储之使用SQLite数据库(四十六)
- 初学...
- 初学
- 初学
- 初学
- 初学
- 初学
- 初学
- 初学
- SQLite
- SQLite
- React Native控件只TextInput
- nyoj5Binary String Matching
- CSS Sprites(CSS精灵) 的优缺点
- [Crash分析] java.util.ConcurrentModificationException
- Builder设计模式
- SQLite初学
- 第26讲项目4
- HDU 5884 Sort 2016 ACM/ICPC Asia Regional Qingdao Online
- Android RxJava第一弹之原理详解、使用详解、常用场景(基于Rxjava2.0)
- 混合开启服务
- fragment实现横竖屏的切换
- Activity的起步流程分析
- linux du命令
- 【原创】东方耀reactnative 视频21之-ProgressBar