创建库
package com.example.asus.myapplication; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; /** * Created by asus on 2016/9/17. */ public class SqliteOpenHelper extends SQLiteOpenHelper { public SqliteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { super(context, name, factory, version); } public SqliteOpenHelper(Context context) { super(context, "shop.db", null, 1); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table shopping(id integer primary key autoincrement,img varchar(50),price double(50),num integer,shop_id varchar(50),name varchar(50))"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
//创建dao包
package com.example.asus.myapplication.Utils; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import com.example.asus.myapplication.Bean.GouWuCheBean; import com.example.asus.myapplication.SqliteOpenHelper; import java.util.ArrayList; /** * Created by asus on 2016/9/19. */ public class SqliteDao { public SqliteOpenHelper helper; public SqliteDao(Context context){ helper=new SqliteOpenHelper(context); } //添加 public void add(GouWuCheBean bean) { SQLiteDatabase db = helper.getWritableDatabase(); if (query(bean) != null) { GouWuCheBean b = query(bean); String id = b.getShop_id(); int num = b.getNum(); if (bean.getShop_id().equals(id)) { update((bean.getNum() + num), id); } } else { ContentValues values = new ContentValues(); values.put("name", bean.getName()); values.put("img", bean.getImg()); values.put("price", bean.getPrice()); values.put("shop_id", bean.getShop_id()); values.put("num", bean.getNum()); db.insert("shopping", null, values); } db.close(); } //根据商品id查询 public GouWuCheBean query(GouWuCheBean b) { GouWuCheBean bean = null; SQLiteDatabase db = helper.getReadableDatabase(); Cursor cursor = db.rawQuery("select * from shopping where shop_id=?", new String[] { b.getShop_id() }); while (cursor.moveToNext()) { String id = cursor.getString(cursor.getColumnIndex("shop_id")); int num = cursor.getInt(cursor.getColumnIndex("num")); GouWuCheBean g=new GouWuCheBean(); g.setShop_id(id); g.setNum(num); bean=g; } return bean; } //修改 public void update(int count, String id) { SQLiteDatabase db = helper.getWritableDatabase(); db.execSQL("update shopping set num=? where shop_id=?", new Object[] { count, id }); db.close(); } //全部查询 public ArrayList<GouWuCheBean> quaryAll() { ArrayList<GouWuCheBean> data = new ArrayList<GouWuCheBean>(); SQLiteDatabase db = helper.getReadableDatabase(); Cursor cursor = db.rawQuery("select * from shopping", null); while (cursor.moveToNext()) { String name = cursor.getString(cursor.getColumnIndex("name")); String img=cursor.getString(cursor.getColumnIndex("img")); Double price=cursor.getDouble(cursor.getColumnIndex("price")); String id=cursor.getString(cursor.getColumnIndex("shop_id")); int num=cursor.getInt(cursor.getColumnIndex("num")); GouWuCheBean bean=new GouWuCheBean(img, price, num, id, name); data.add(bean); } cursor.close(); db.close(); return data; } //删除 public void delete(ArrayList<String> id){ SQLiteDatabase db = helper.getWritableDatabase(); for(String s:id){ db.delete("shopping", "shop_id=?", new String[]{s}); } db.close(); } }