android笔记--Sqlite使用
package com.example.storedate; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; public class SQLiteUtil extends SQLiteOpenHelper { private final static String TAG = SQLiteUtil.class.getName(); private String SQL_CREATE = "create table dictionary(_id integer primary key autoincrement," + "word, detail)"; public SQLiteUtil(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub db.execSQL(SQL_CREATE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub Log.i(TAG, "db version update "); } }
package com.example.storedate; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import android.R.integer; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.util.Log; public class SQLiteManager { private SQLiteUtil util; private int VERSION = 1; public SQLiteManager(Context context) { this.util = new SQLiteUtil(context, "dict.db", null, VERSION); } public boolean addWord(String word, String detail) { String sql = "insert into dictionary values(null,?,?)"; String[] bindArgs = new String[] { word, detail }; SQLiteDatabase db = util.getWritableDatabase(); db.execSQL(sql, bindArgs); return true; } public ArrayList<Map<String, String>> convertToList() { ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>(); String sql = "select _id,word,detail from dictionary"; SQLiteDatabase db = util.getReadableDatabase(); Cursor cursor = db.rawQuery(sql, null); while (cursor.moveToNext()) { Map<String, String> map = new HashMap<String, String>(); map.put("word", cursor.getString(1)); map.put("detail", cursor.getString(2)); // Log.i("test [0]","---"+cursor.getString(0)); // Log.i("test [1]","---"+cursor.getString(1)); // Log.i("test [2]","---"+cursor.getString(2)); list.add(map); } return list; } public Cursor getCursor() { String sql = "select _id,word,detail from dictionary"; SQLiteDatabase db = util.getReadableDatabase(); Cursor cursor = db.rawQuery(sql, null); return cursor; } public Map<String, String> getById(int index) { String sql = "select _id,word,detail from dictionary where _id=?"; SQLiteDatabase db = util.getReadableDatabase(); Map<String, String> map = new HashMap<String, String>(); String[] selectionArgs = new String[] { index + "" }; Cursor cursor = db.rawQuery(sql, selectionArgs); if (cursor.moveToNext()) { map.put("word", cursor.getString(1)); Log.i("Dest", cursor.getString(1)); map.put("detail", cursor.getString(2)); } return map; } }
使用:插入数据
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_add); dbManager = new SQLiteManager(this); Button button = (Button) findViewById(R.id.button1); final TextView textView1 = (TextView) findViewById(R.id.editText1); final TextView textView2 = (TextView) findViewById(R.id.editText2); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub String word = textView1.getText().toString(); String detail = textView2.getText().toString(); if (!"".equals(word) && !"".equals(detail)) { dbManager.addWord(word, detail); Toast.makeText(AddActivity.this, "word saved", Toast.LENGTH_SHORT).show(); finish(); } } }); }
使用:查询
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sqlite); dbManager = new SQLiteManager(this); listView = (ListView) findViewById(R.id.listView1); cursorAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, dbManager.getCursor(), new String[] { "word", "detail" }, new int[] { android.R.id.text1, android.R.id.text2 }, 1); listView.setAdapter(cursorAdapter); listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub Bundle bundle = new Bundle(); bundle.putInt("index", position); Intent intent = new Intent(SQLiteActivity.this, DetailActivity.class); intent.putExtra("dic", bundle); startActivity(intent); } }); }