參考文章:http://www.2cto.com/kf/201312/266453.html html
www.runoob.com/sqlite/sqlite-where-clause.html java
http://www.cnblogs.com/ahao214/p/4257280.html sql
在完成保存app的數據的時候直接採用了sqlite進行存儲~ 數據庫
1,繼承於sqliteopenhelper的類 app
public class MySqliteHelper extends SQLiteOpenHelper { public MySqliteHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { super(context, name, factory, version); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table if not exists my_table(_id integer primary key autoincrement,task text,date text,task_type text)"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }2,主程序保存數據
MySqliteHelper mySqliteHelper=new MySqliteHelper(this,"mydata.db",null,1); //mydata.db的數據庫
在這裏自定義insert方法 ide
//保存到數據庫並返回主界面 iv1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String tasktext=et3.getText().toString(); String datetext=et2.getText().toString(); String task_typetext=et1.getText().toString(); ContentValues values=new ContentValues(); //經過key-value對的形式將數據插入到表中 values.put("task",tasktext); values.put("date", datetext); values.put("task_type", task_typetext); Insert(mySqliteHelper, values); Intent intent=new Intent(add_task.this,MainActivity.class); startActivity(intent); finish(); } });
//插入 public void Insert(MySqliteHelper mySqliteHelper,ContentValues values) { SQLiteDatabase db=mySqliteHelper.getWritableDatabase(); //得到可寫數據表 db.insert("my_table", null, values); //用原有的sql語句將數據插入到表中 db.close(); }
3.讀取 ui
Cursor c獲取到的仍是用原生sql語句獲得的 this
//獲取數據庫的全部信息到List public ArrayList<HashMap<String,Object>> getALLlist() { ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String , Object>>(); SQLiteDatabase db = mySqliteHelper.getReadableDatabase(); Cursor c = db.query("my_table", new String[]{"_id", "task", "date", "task_type"}, null, null, null, null, null); if (c != null) { while (c.moveToNext()) { HashMap<String, Object> item = new HashMap<String, Object>(); item.put("_id", c.getInt(c.getColumnIndex("_id"))); item.put("task", c.getString(c.getColumnIndex("task"))); item.put("date", c.getString(c.getColumnIndex("date"))); item.put("task_type", c.getString(c.getColumnIndex("task_type"))); item.put("image", R.drawable.quan1); list.add(item); } } c.close(); return list; }4,修改
db.update("my_table",values,"_id="+String.valueOf(id),null);5.刪除
//刪除 bt2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new AlertDialog.Builder(task_read.this) .setPositiveButton("肯定",new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { db.delete("my_table","_id="+String.valueOf(id),null); Intent intent1=new Intent(task_read.this,MainActivity.class); startActivity(intent1); } }).setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }).setTitle("是否要刪除").create().show(); } });關鍵語句都是用sql 語句實現的。。