其实green到进行了很多优化,增删改查使用合适的数据结构,这里的updata是根据id这是Long型的,是主键。主键必有的,不染你后面很多更新就不能用 了
补充:重要:
在你修改了数据库部分任何代码都要进行卸载应用,在运行安装,不染很烦人的问题整的你不懂,还有数据库升级也是你晕的地方,最直接简单的办法就是卸载充运行。
其实就是你先根据条件拿到对象,在重新set某个属性,再调用update方法,传入相应的对象就ok了,代码展示:
这是工具类:红色标记是调用是update
package com.aa.db; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import com.aa.DaoMaster; import com.aa.DaoSession; import com.aa.data_newDao; import com.example.administrator.myapplication.bean.data_new; import org.greenrobot.greendao.query.QueryBuilder; import java.util.List; /** * Created by Administrator on 2019/5/15 0015. */ public class DBManager { private final static String dbName = "test_db"; private static DBManager mInstance; private DaoMaster.DevOpenHelper openHelper; private Context context; public DBManager(Context context) { this.context = context; openHelper = new DaoMaster.DevOpenHelper(context, dbName, null); } /** * 获取单例引用 * * @param context * @return */ public static DBManager getInstance(Context context) { if (mInstance == null) { synchronized (DBManager.class) { if (mInstance == null) { mInstance = new DBManager(context); } } } return mInstance; } private SQLiteDatabase getReadableDatabase() { if (openHelper == null) { openHelper = new DaoMaster.DevOpenHelper(context, dbName, null); } SQLiteDatabase db = openHelper.getReadableDatabase(); return db; } private SQLiteDatabase getWritableDatabase() { if (openHelper == null) { openHelper = new DaoMaster.DevOpenHelper(context, dbName, null); } SQLiteDatabase db = openHelper.getWritableDatabase(); return db; } /** * 插入一条记录 * * @param data_new */ public void insertUser(data_new data_new) { DaoMaster daoMaster = new DaoMaster(getWritableDatabase()); DaoSession daoSession = daoMaster.newSession(); data_newDao userDao = daoSession.getData_newDao(); userDao.insertOrReplace(data_new); } public void insertUserList(ListBlackInfoCallMIssedBeans) { if (BlackInfoCallMIssedBeans == null || BlackInfoCallMIssedBeans.isEmpty()) { return; } DaoMaster daoMaster = new DaoMaster(getWritableDatabase()); DaoSession daoSession = daoMaster.newSession(); data_newDao userDao = daoSession.getData_newDao(); userDao.insertOrReplaceInTx(BlackInfoCallMIssedBeans); } public void deleteUser(data_new data_new) { DaoMaster daoMaster = new DaoMaster(getWritableDatabase()); DaoSession daoSession = daoMaster.newSession(); data_newDao userDao = daoSession.getData_newDao(); userDao.queryBuilder() .where(data_newDao.Properties.NUMBERING.eq(data_new.getNUMBERING())) .buildDelete() .executeDeleteWithoutDetachingEntities(); } public void updateUser(data_new data_new) { DaoMaster daoMaster = new DaoMaster(getWritableDatabase()); DaoSession daoSession = daoMaster.newSession(); data_newDao userDao = daoSession.getData_newDao(); userDao.update(data_new); } public List queryUserList() { DaoMaster daoMaster = new DaoMaster(getReadableDatabase()); DaoSession daoSession = daoMaster.newSession(); data_newDao userDao = daoSession.getData_newDao(); QueryBuilder qb = userDao.queryBuilder(); // .orderDesc(data_newDao.Properties.Numbering); List list = qb.list(); return list; } /** * 查询用户列表 */ public List queryUserList(String path) { DaoMaster daoMaster = new DaoMaster(getReadableDatabase()); DaoSession daoSession = daoMaster.newSession(); data_newDao userDao = daoSession.getData_newDao(); QueryBuilder qb = userDao.queryBuilder(); qb.where(data_newDao.Properties.NUMBERING.gt(path)); // .orderAsc(data_newDao.Properties.PhoneNumber); List list = qb.list(); return list; } // public List queryUser(String numbering) { DaoMaster daoMaster = new DaoMaster(getReadableDatabase()); DaoSession daoSession = daoMaster.newSession(); data_newDao userDao = daoSession.getData_newDao(); QueryBuilder qb = userDao.queryBuilder(); // qb.where(data_newDao.Properties.NUMBERING.gt(numbering)); qb.where(data_newDao.Properties.NUMBERING.eq(numbering)); // .orderAsc(data_newDao.Properties.PhoneNumber); List list = qb.list(); return list; } //查询已盘数量 public List queryUserInventoried() { DaoMaster daoMaster = new DaoMaster(getReadableDatabase()); DaoSession daoSession = daoMaster.newSession(); data_newDao userDao = daoSession.getData_newDao(); QueryBuilder qb = userDao.queryBuilder(); // qb.where(data_newDao.Properties.NUMBERING.gt(numbering)); qb.where(data_newDao.Properties.Status.eq("已盘")); // .orderAsc(data_newDao.Properties.PhoneNumber); List list = qb.list(); return list; } } 这是Activity,在这里进行调用:红色标记是调用 public class ActivityPropertyCheckActivity extends Activity implements View.OnClickListener { private TextView tvAllQuantity; private TextView tvcheckQuantity; private int anIntAllAmount;//总数量 private int anIntCheckAmount;//已盘点数量 private String mAssertNameing;//资产编号 查询关键字 private EditText getEd1() { return (EditText) findViewById(R.id.ed_1); } private EditText getEd2() { return (EditText) findViewById(R.id.ed_2); } private EditText getEd3() { return (EditText) findViewById(R.id.ed_3); } private EditText getEd4() { return (EditText) findViewById(R.id.ed_4); } private EditText getEd5() { return (EditText) findViewById(R.id.ed_5); } private EditText getEd6() { return (EditText) findViewById(R.id.ed_6); } private EditText getEd7() { return (EditText) findViewById(R.id.ed_7); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_property_check); //获得数据数目 initData(); findViewById(R.id.bt_back).setOnClickListener(this); findViewById(R.id.bt_conrm_check).setOnClickListener(this); tvcheckQuantity = (TextView) findViewById(R.id.tv_check_quantity); tvAllQuantity = (TextView) findViewById(R.id.tv_all_quantity); IntentFilter filter = new IntentFilter(); filter.addAction("com.android.server.scannerservice.broadcast"); this.registerReceiver(mResultReceiver, filter); tvcheckQuantity.setText(anIntCheckAmount + ""); tvAllQuantity.setText(anIntAllAmount + ""); } private void SearchData() { data_new dataNew = new data_new(); dataNew.setNUMBERING(mAssertNameing); if (DBManager.getInstance(ActivityPropertyCheckActivity.this).queryUser(mAssertNameing).size() == 0) { Toast.makeText(this, "数据不存在,请重新扫描", Toast.LENGTH_SHORT).show(); } else { //数据回显 dataNew = DBManager.getInstance(ActivityPropertyCheckActivity.this).queryUser(mAssertNameing).get(0); getEd2().setText(dataNew.getUSER_OF_DEPARTMENT()); getEd3().setText(dataNew.getSPECIFICATIONA()); // getEd7().setText(dataNew.getINVENTORY_AMOUNT()); getEd5().setText(dataNew.getLOCATION()); getEd4().setText(dataNew.getUSER_OF_DEPARTMENT()); getEd6().setText(dataNew.getNOTE()); //盘点结果状更新; //盘点结果状态修改 数据修改 if (Double.parseDouble(dataNew.getBOOK_AMOUNT()) < Double.parseDouble(dataNew.getINVENTORY_AMOUNT())) { dataNew.setINVENTORY_RESULT("盘实");//根据要求进行更改 } else if (Double.parseDouble(dataNew.getBOOK_AMOUNT()) == Double.parseDouble(dataNew.getINVENTORY_AMOUNT())) { dataNew.setINVENTORY_RESULT("盘实");//根据要求进行更改 } else if (Double.parseDouble(dataNew.getBOOK_AMOUNT()) > Double.parseDouble(dataNew.getINVENTORY_AMOUNT())) { dataNew.setINVENTORY_RESULT("盘亏");//根据要求进行更改 } }//其实green到进行了很多优化,增删改查使用合适的数据结构,这里的updata是根据id这是Long型的,是主键。主键必有的。 DBManager.getInstance(ActivityPropertyCheckActivity.this).updateUser(dataNew);//数据更新 } private void initData() { anIntAllAmount = (DBManager.getInstance(ActivityPropertyCheckActivity.this).queryUserList()).size(); anIntCheckAmount = DBManager.getInstance(ActivityPropertyCheckActivity.this).queryUserInventoried().size(); } private BroadcastReceiver mResultReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub if (intent.getAction().equals("com.android.server.scannerservice.broadcast")) { mAssertNameing = intent.getStringExtra("scannerdata"); getEd1().setText(mAssertNameing); List list = DBManager.getInstance(ActivityPropertyCheckActivity.this).queryUser(mAssertNameing); //扫码后进行资产数量修改 // DBManager.getInstance(ActivityPropertyCheckActivity.this).modicationInventoryResult(list.get(0)); list.get(0).setINVENTORY_AMOUNT(Double.parseDouble(list.get(0).getINVENTORY_AMOUNT()) + 1 + ""); list.get(0).setStatus("已盘"); DBManager.getInstance(ActivityPropertyCheckActivity.this).updateUser(list.get(0)); if (mAssertNameing != null) { SearchData(); } refreshData(); } } }; @Override public void onClick(View view) { switch (view.getId()) { case R.id.bt_back: //TODO implement finish(); break; case R.id.bt_conrm_check: refreshData(); //TODO implement break; } } /*刷新盘点数目和未盘点数目*/ private void refreshData() { initData(); anIntCheckAmount = DBManager.getInstance(ActivityPropertyCheckActivity.this).queryUserInventoried().size(); tvAllQuantity.setText(anIntAllAmount + ""); tvcheckQuantity.setText(anIntCheckAmount + ""); } @Override protected void onDestroy() { super.onDestroy(); if (mResultReceiver != null) { this.unregisterReceiver(mResultReceiver); } } }