import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.HashMap;
/**
* Created by Administrator on 2016/4/20.
*/
public class DBHelper extends SQLiteOpenHelper{
final static int version = 1;
Cursor cursor;
SQLiteDatabase db;
public DBHelper(Context context) {
super(context, "App.db", null, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
//建表
String CREATE_TABLE_GT = "create table if not exists temp (id INTEGER PRIMARY KEY AUTOINCREMENT,key varchar)";
//购物车
String DB_GOUWUCHE="create table if not exists gouwuche ( id INTEGER PRIMARY KEY AUTOINCREMENT,shopid text," +
"attoneid text,atttwoid text,merchantid text,name text," +
"price text,img text,count text,shuxin1,shuxin2)";
db.execSQL(CREATE_TABLE_GT);
db.execSQL(DB_GOUWUCHE);
}
//添加
public void add(){
db = this.getWritableDatabase();
db.execSQL("insert into temp (key) values('aa')");
cursor = db.rawQuery("select * from temp",new String[]{});
if (cursor.moveToNext()){
String key= cursor.getString(cursor.getColumnIndex("key"));
int id = cursor.getInt(cursor.getColumnIndex("id"));
}
}
//查询
public void query(String sql){
db = getReadableDatabase();
cursor= db.rawQuery(sql,new String[]{});
//
cursor.close();
}
//增加,删除修改
public boolean idu(String sql){
boolean result = true;
try {
db.execSQL(sql);
}catch (SQLException e){
result = false;
}
return result;
}
public void close(){
if (cursor!=null){
cursor.close();
}
if (db!=null&&db.isOpen()){
db.close();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public ArrayList<String> getAll() {
db = this.getReadableDatabase();
String sql = "select * from temp";
cursor = db.rawQuery(sql,new String[]{});
ArrayList<String> list = new ArrayList<String>();
while (cursor.moveToNext()) {
String key= cursor.getString(cursor.getColumnIndex("key"));
int id = cursor.getInt(cursor.getColumnIndex("id"));
list.add(key);
}
return list;
}
public ArrayList<Integer> getId() {
db = this.getReadableDatabase();
String sql = "select * from temp";
cursor = db.rawQuery(sql,new String[]{});
ArrayList<Integer> list = new ArrayList<Integer>();
while (cursor.moveToNext()) {
String key= cursor.getString(cursor.getColumnIndex("key"));
int id = cursor.getInt(cursor.getColumnIndex("id"));
list.add(id);
}
return list;
}
/**
* 向表格temp中添加数据
*
* @param act
*/
public void insertAction(String act) throws Exception {
db = this.getWritableDatabase();
if (act != null) {
if(act.length()<1){
return;
}
String isql = "insert into temp(key) values(?)";
db.execSQL(isql, new Object[] {act});
} else {
}
}
//购物车添加数据
public void addShoping(HashMap<String,String> map){
db = this.getWritableDatabase();
if (map != null) {
if(map.size()<1){
return;
}
cursor= db.query("gouwuche", new String[] { "shopid", "attoneid",
"atttwoid", "merchantid" }, "shopid"+"=? and "+"attoneid"+"=? and "+"atttwoid"+"=? and "+"merchantid"+"=?", new String[]{map.get("shopid"),map.get("attoneid"),map.get("atttwoid"),map.get("merchantid")}, null,null,null,null);
boolean boo=true;
while (cursor.moveToNext()) {
ContentValues values = new ContentValues();
values.put("count",map.get("count"));//key为字段名,value为值
db.update("gouwuche", values, "shopid=?", new String[]{map.get("shopid")});
boo=false;
}
if(boo){
String isql = "insert into gouwuche (shopid,attoneid,atttwoid,merchantid,name,price,img,count,shuxin1,shuxin2) values(?,?,?,?,?,?,?,?,?,?)";
db.execSQL(isql, new Object[] {map.get("shopid"),map.get("attoneid"),map.get("atttwoid"),map.get("merchantid"),map.get("name")
,map.get("price"),map.get("img"),map.get("count"),map.get("shuxin1"),map.get("shuxin2")});
}
} else {
}
}
//根据商品id删除购物车中的数据
public boolean deleteShoping(String id,String attoneid,String atttwoid) throws Exception {
db = this.getWritableDatabase();
// 系统指令不能删除
String dsql = "delete from gouwuche where shopid ="+id+" and attoneid ="+attoneid+" and atttwoid ="+atttwoid;
db.execSQL(dsql);
return true;
}
//修改购物车商品
public int upDataShong(HashMap<String,String> map){
db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("shopid", map.get("shopid"));
values.put("attoneid", map.get("attoneid"));
values.put("atttwoid", map.get("atttwoid"));
values.put("merchantid", map.get("merchantid"));
values.put("name", map.get("name"));
values.put("price", map.get("price"));
values.put("img", map.get("img"));
values.put("count", map.get("count"));
values.put("shuxin1", map.get("shuxin1"));
values.put("shuxin2", map.get("shuxin2"));
String shopid=map.get("shopid");
return db.update("gouwuche", values,"shopid="+ shopid, null);
}
//查询购物车所有物品
public ArrayList<HashMap<String,String>> getAllShoping() {
db = this.getReadableDatabase();
String sql = "select * from gouwuche";
cursor = db.rawQuery(sql,new String[]{});
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
while (cursor.moveToNext()) {
HashMap<String,String> map=new HashMap<>();
String shopid= cursor.getString(cursor.getColumnIndex("shopid"));
String attoneid= cursor.getString(cursor.getColumnIndex("attoneid"));
String atttwoid= cursor.getString(cursor.getColumnIndex("atttwoid"));
String merchantid= cursor.getString(cursor.getColumnIndex("merchantid"));
String name= cursor.getString(cursor.getColumnIndex("name"));
String price= cursor.getString(cursor.getColumnIndex("price"));
String img= cursor.getString(cursor.getColumnIndex("img"));
String count= cursor.getString(cursor.getColumnIndex("count"));
String shuxin1= cursor.getString(cursor.getColumnIndex("shuxin1"));
String shuxin2= cursor.getString(cursor.getColumnIndex("shuxin2"));
map.put("shopid",shopid);
map.put("attoneid",attoneid);
map.put("atttwoid",atttwoid);
map.put("merchantid",merchantid);
map.put("name",name);
map.put("price",price);
map.put("img",img);
map.put("count",count);
map.put("shuxin1",shuxin1);
map.put("shuxin2",shuxin2);
list.add(map);
}
return list;
}
/**
* 删除指令
*/
public boolean deleteAction(int id) throws Exception {
db = this.getWritableDatabase();
// 系统指令不能删除
String dsql = "delete from temp where id ="+id;
db.execSQL(dsql);
return true;
}
public boolean deleteAll() throws Exception {
db = this.getWritableDatabase();
// 系统指令不能删除
String dsql = "delete from temp where 1 = 1";
db.execSQL(dsql);
return true;
}
}