考点总结:
- 创建数据库
- 数据库增删改查(2方法)
- 使用execSQL()方法通过SQL语句进行增删改查
- 使用SQLiteDatabase对象的增删改查方法
- SQLite中的事务
- ListView控件 和 数据适配器BaseAdapter使用
//考点一、创建数据库
public class MyHelper extends SQLiteOpenHelper{//创建一个类,继承SQLiteOpenHelper类public myHelper(Context context){//写该类的构造方法super(context, "itcast.db", null, 1);//4个参数:上下文对象、数据库名称、null、数据库版本}public void onCreate(SQLiteDatabase db){//重写onCreate方法db.execSQL//创建表information,表中含递增主键id,name,price("CREATE TABLE information (_id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(20), price INTEGER)");}public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){//数据库版本更新方法}}
//考点二、数据库增删改查(2 方法)
//方法1.使用execSQL()方法通过SQL语句进行增删改查
public void insert (String name, String price){SQLiteDatabase db = myHelper.getWritableDatabase();//获取可读写SQLiteDatabase对象db.execSQL("insert into information (name, price) values (?,?)",new Object[]{name,price});db.close();}
db.execSQL("delete from information where name = ?", new String[]{name});
db.execSQL("update information set name = ? where price = ?",new Object[]{name, price});
Cursor cursor = db.rawQuery("select * from information where name = ?",new String[] {name});
//方法2.使用SQLiteDatabase对象的增删改查方法
public void insert(String name, String price){SQLiteDatabase db = myHelper.getWritableDatabase(); //step 1.ContentValues values = new ContentValues(); //step 2.values.put("name", name); //step 3.values.put("price", price);long id = db.insert("information", null, values); //step 4.db.close(); //step 5.}
public int update(String name, String price){SQLiteDatabase db = myHelper.getWritableDatabase();ContentValues values = new ContentValues();values.put("name", name);int number = db.update("information", values, "price = ?",new String[]{price});db.close();return number;}
public int delete(int id){SQLiteDatabase db = myHelper.getWritableDatabase();int number = db.delete("information", "_id = ?", new String[]{id+""});db.close();return number;}
public boolean find(long id){SQLiteDatabase db = myHelper.getReadableDatabase();Cursor cursor = db.query("information", null, "_id=?",new String[]{id+""}, null, null, null);boolean result = cursor.moveToNext();cursor.close();db.close();return result;}
//考点三、SQLite中的事务
PersonSQLiteOpenHelper myHelper =new PersonSQLiteOpenHelper(getContext()); //step 1.SQLiteDatabase db = myHelper.getWritableDatabase();db.beginTransaction();//step 2.try{//转出db.execSQL("update person set account = account-1000 where name = ?",new Object[]{"wangwu"});//转入db.execSQL("update person set account = account+1000 where name = ?",new Object[]{"lisi"});db.setTransationSuccessful();}catch(Exception e){Log.i("事务处理失败", e.toString());}finally{db.endTransaction();//step 3.db.close();//step 4.}
//考点四、ListView控件 和 数据适配器BaseAdapter使用
<ListViewandriod:id = "@+id/lv"andriod:layout_width = "wrap_content"andriod:layout_height = "match_parent"</ListView>
private ListView myListView;private String[] names = {"京东", "qq", "淘宝",};private int[] icons = {R.drawable.jd, R.id.qq, R.id.tb}//step 1.myListView = (ListView) findViewById(R.id.lv);//step 2.MyAdapter myAdapter = new MyAdapter();//step 3.myListView.setAdapter(myAdapter);class MyAdapter extends BaseAdapter{public int getCount(){return names.length();}public Object getItem(int position){return names[position];}public long getItemId(int position){return position;}public View getView(int position, View convertView, ViewGroup parent){View view = View.inflate(Main_Activity.this, R.layout.list_item, null);//将List_item.xml文件找出来并转换成View对象TextView myTextView = (TextView) view.findViewById(R.id.item_tv);myTextView.setText( names[position] );ImageView myImageView = (ImageView) view.findViewById(R.id.item_iv);myImageView.setBackgroundResource( icons[position] );return view;}}
