SQLiteOpenHelper的使用范例
2012-08-19 20:41
513 查看
创建数据库
对数据库进行增加数据
对数据库进行查询和删除
public class DBHelper extends SQLiteOpenHelper { private static final String DB_NAME = "coll.db"; private static final String TBL_NAME = "CollTbl"; private static final String CREATE_TBL = " create table " + " CollTbl(_id integer primary key autoincrement,name text,url text, desc text) "; private SQLiteDatabase db; DBHelper(Context c) { super(c, DB_NAME, null, 2); } @Override public void onCreate(SQLiteDatabase db) { this.db = db; db.execSQL(CREATE_TBL); } public void insert(ContentValues values) { SQLiteDatabase db = getWritableDatabase(); db.insert(TBL_NAME, null, values); db.close(); } public Cursor query() { SQLiteDatabase db = getWritableDatabase(); Cursor c = db.query(TBL_NAME, null, null, null, null, null, null); return c; } public void del(int id) { if (db == null) db = getWritableDatabase(); db.delete(TBL_NAME, "_id=?", new String[] { String.valueOf(id) }); } public void close() { if (db != null) db.close(); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
对数据库进行增加数据
String name = et1.getText().toString(); String url = et2.getText().toString(); String desc = et3.getText().toString(); ContentValues values = new ContentValues(); values.put("name", name); values.put("url", url); values.put("desc", desc); DBHelper helper = new DBHelper(getApplicationContext()); helper.insert(values); Intent intent = new Intent(AddActivity.this, QueryActivity.class); startActivity(intent);
对数据库进行查询和删除
public class QueryActivity extends ListActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setTitle("浏览收藏信息"); final DBHelper helpter = new DBHelper(this); Cursor c = helpter.query(); String[] from = { "_id", "name", "url", "desc" }; int[] to = { R.id.text0, R.id.text1, R.id.text2, R.id.text3 }; SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.row, c, from, to); ListView listView = getListView(); listView.setAdapter(adapter); final AlertDialog.Builder builder = new AlertDialog.Builder(this); listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { final long temp = arg3; builder.setMessage("真的要删除该记录吗?").setPositiveButton("是", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { helpter.del((int)temp); Cursor c = helpter.query(); String[] from = { "_id", "name", "url", "desc" }; int[] to = { R.id.text0, R.id.text1, R.id.text2, R.id.text3 }; SimpleCursorAdapter adapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.row, c, from, to); ListView listView = getListView(); listView.setAdapter(adapter); } }).setNegativeButton("否", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { } }); AlertDialog ad = builder.create(); ad.show(); } }); helpter.close(); } }
相关文章推荐
- MonoDroid学习笔记(十一)—— 使用SQLiteOpenHelper实现简易备忘录
- 使用SQLiteOpenHelper的正确姿势
- Android SQLiteOpenHelper使用
- 使用SQLiteOpenHelper插入数据到数据库并读取数据库中的内容
- Android:SQLite使用方法 SQLiteOpenHelper操作 .
- Android-SQLiteOpenHelper使用示例
- SQLiteOpenHelper使用之英文生词本
- SQLiteOpenHelper的使用
- 使用SQLiteOpenHelper需要注意的问题
- android SQLiteOpenHelper使用示例
- Android SQLiteOpenHelper的使用心得
- 使用SQLiteOpenHelper和单例模式操作SQLite数据库
- android SQLiteOpenHelper使用示例
- Android中使用SQLiteOpenHelper管理SD卡中的数据库
- android SQLiteOpenHelper和ContentProvider的结合使用
- SQLiteOpenHelper的正确使用
- Sqlite SQLiteOpenHelper 的继承使用
- Android中使用SQLiteOpenHelper管理SD卡中的数据库
- SQLiteOpenHelper基础使用
- 安卓开发-Sqliteopenhelper使用