我如何在SQlite DataBase中使用Fire UpdateQuery

时间:2017-09-22 12:13:35

标签: android database sqlite mysqli

这是我的主要性

      DataBaseHelper myDb;
        TextView roti, bread, biscuit, egg, dosa, fruits;
        TextView groti, gbread, gbiscuit, gegg, gdosa, gfruite;
        TextView used1, used2, used3, used4, used5, used6;
        Spinner spinner, spinner2;
        String[] listofitem = {"Roti", "Bread", "Biscuit", "Egg", "Dosa", "Fruits"
        };
        String[] value = {"Select", "1", "2"};
        protected static TextView dispalycuerrentdate;
        protected static TextView dispalycuerrenttime;
        //for spinner string
        String item, quantity;
        Button submit;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            //TEXVIEW INITIALIZER
            submit = (Button) findViewById(R.id.submitbutton);
            roti = (TextView) findViewById(R.id.roti);
            bread = (TextView) findViewById(R.id.bread);
            biscuit = (TextView) findViewById(R.id.biscuit);
            egg = (TextView) findViewById(R.id.egg);
            dosa = (TextView) findViewById(R.id.dosa);
            fruits = (TextView) findViewById(R.id.fruits);
            groti = (TextView) findViewById(R.id.groti);
            gbread = (TextView) findViewById(R.id.qbread);
            gbiscuit = (TextView) findViewById(R.id.gbiscuit);
            gegg = (TextView) findViewById(R.id.qegg);
            gdosa = (TextView) findViewById(R.id.qdosa);
            gfruite = (TextView) findViewById(R.id.gfruits);
            used1 = (TextView) findViewById(R.id.used1);
            used2 = (TextView) findViewById(R.id.used2);
            used3 = (TextView) findViewById(R.id.used3);
            used4 = (TextView) findViewById(R.id.used4);
            used5 = (TextView) findViewById(R.id.used5);
            used6 = (TextView) findViewById(R.id.used6);
            //Spinner Initializer
            spinner = (Spinner) findViewById(R.id.Spinner1);
            spinner2 = (Spinner) findViewById(R.id.Spinner2);
    //Initializing db
            myDb = new DataBaseHelper(this);
            ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.mtype, android.R.layout.simple_selectable_list_item);
            spinner.setAdapter(adapter);
            ArrayAdapter adapterr = ArrayAdapter.createFromResource(this, R.array.mmtype, android.R.layout.simple_selectable_list_item);
            spinner.setAdapter(adapter);
            spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    item = parent.getSelectedItem().toString();
                    Log.e("String", item);


                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });
            spinner2.setAdapter(adapterr);
            spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    quantity = parent.getSelectedItem().toString();

                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });
            //Date and time Picker
            dispalycuerrentdate = (TextView) findViewById(R.id.date);
            Button displayTimeButton = (Button) findViewById(R.id.select_time);
            assert displayTimeButton != null;
            displayTimeButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    DatePickerFragment datePickerFragment = new DatePickerFragment();
                    datePickerFragment.show(getFragmentManager(), "Select");
                }
            });

    //for time
            dispalycuerrenttime = (TextView) findViewById(R.id.timeee);
            Button displayTimeButtonn = (Button) findViewById(R.id.select_timee);
            assert displayTimeButtonn != null;
            displayTimeButtonn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    TimePicker mTimePicker = new TimePicker();
                    mTimePicker.show(getFragmentManager(), "Select time");
                }
            });

            //Button click

            submit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    insertintodb();
                   // Updateintodb();
                    Toast.makeText(MainActivity.this, "ITEm:-" + item + "Quantity:-" + quantity, Toast.LENGTH_SHORT).show();
                }
            });


        }

        public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
            @Override
            public Dialog onCreateDialog(Bundle savedInstanceState) {
                final Calendar c = Calendar.getInstance();
                int year = c.get(Calendar.YEAR);
                int month = c.get(Calendar.MONTH);
                int day = c.get(Calendar.DAY_OF_MONTH);
                return new DatePickerDialog(getActivity(), this, year, month, day);
            }

            public void onDateSet(DatePicker view, int year, int month, int day) {
                dispalycuerrentdate.setText("" + String.valueOf(year) + "/" + String.valueOf(month) + "/" + String.valueOf(day));
            }
        }

        public static class TimePicker extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
            @Override
            public Dialog onCreateDialog(Bundle savedInstanceState) {
                final Calendar c = Calendar.getInstance();
                int hour = c.get(Calendar.HOUR_OF_DAY);
                int minute = c.get(Calendar.MINUTE);
                return new TimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity()));
            }

            @Override
            public void onTimeSet(android.widget.TimePicker view, int hourOfDay, int minute) {
                dispalycuerrenttime.setText(" " + String.valueOf(hourOfDay) + " : " + String.valueOf(minute));
            }
        }

        public void insertintodb() {
            ContentValues insertvalue = new ContentValues();
            Log.e("INSERTVALUE", insertvalue.toString());
            insertvalue.put(myDb.ITEM, item.toString());
            insertvalue.put(myDb.MAX, 2);
            insertvalue.put(myDb.USED, quantity.toString());
            insertvalue.put(myDb.DATE, String.valueOf(dispalycuerrentdate));
            insertvalue.put(myDb.TIME, String.valueOf(dispalycuerrenttime));
            myDb.db.insert(myDb.TABLE_NAME, null, insertvalue);
        }

        //    public int Updateintodb(String oldvalue, String newUsed) {
    ////UPDATE "main"."fOOD_dIARY" SET "Used" = ?1 WHERE  "ID" = 2
    //        ContentValues contentValues = new ContentValues();
    //        contentValues.put(myDb.USED, newUsed);
    //        String[] where = {oldvalue};
    //        int count = myDb.db.update(myDb.TABLE_NAME, contentValues, myDb.USED + " =? ", where);
    //        Log.e("COUNT",String.valueOf(count));
    //        return count;
    //
    //    }
Here is my another class

public class DataBaseHelper extends SQLiteOpenHelper {
    public static final String DATABSE_NAME = "foodDiary.db";
    public static final String TABLE_NAME = "fOOD_dIARY";
    public static final String Details = "Details";

    public static final String VALUE = "Value";

    public static final String ID = "ID";
    public static final String ITEM = "Item";
    public static final String MAX = "Max";
    public static final String USED = "Used";
    public static final String DATE = "Date";
    public static final String TIME = "Time";
    public static final String Quantity = "Time";
    SQLiteDatabase db = this.getWritableDatabase();

    private static final String CREATE_TABLE_TODO_TAG = "CREATE TABLE "
            + TABLE_NAME + "(" + ID + " INTEGER PRIMARY KEY,"
            + ITEM + " TEXT," + MAX + " INTEGER,"
            + DATE + " TEXT," + TIME + " TEXT,"
            + USED + " INTEGER" + ")";

    private static final String CREATE_TABLE = "CREATE TABLE "
            + Details + "(" + ID + " INTEGER PRIMARY KEY,"
            + DATE + " TEXT," + TIME + " TEXT,"
            + USED + " INTEGER" + ")";


    public DataBaseHelper(Context context) {
        super(context, DATABSE_NAME, null, 1);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE_TODO_TAG);
        db.execSQL(CREATE_TABLE);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
        db.execSQL("DROP TABLE IF EXISTS" + CREATE_TABLE);
        onCreate(db);
    }
}

我无法在数据库中更新我的插入查询,只要我点击提交按钮,它就会被添加到表中,而不是在数据库中更新,     我怎么能写一个更新查询,以便我可以用来更新数据库 请提供正确的指导,因为我在谷歌有搜索地段,但无法理解 我有一个微调器,我必须选择Item并在数据库中发送值并希望更新这些值

2 个答案:

答案 0 :(得分:0)

试试这个

    public int UpdateIntodb(String oldValue, String newUsed) {

    ContentValues cv = new ContentValues();
    cv.put(myDb.USED, newUsed);
    return myDb.db.update(myDb.TABLE_NAME, cv, myDb.USED + "=" + oldValue, null);
}

答案 1 :(得分:0)

public void updateBanner(BannerMaster bannerMaster) {
    database = this.getWritableDatabase();
    ContentValues values = new ContentValues();

    values.put(TableBannerMaster.C01_BANNER_ID, bannerMaster.getBannerId());
    values.put(TableBannerMaster.C02_BANNER_NAME, bannerMaster.getBannerName());
    values.put(TableBannerMaster.C03_DISPLAY_ORDER, bannerMaster.getDisplayOrder());
    values.put(TableBannerMaster.C04_BANNER_URL, bannerMaster.getBannerUrl());
    values.put(TableBannerMaster.C05_APPLICABLE_IN, bannerMaster.getApplicableIn());
    values.put(TableBannerMaster.C06_BANNER_IMAGE, bannerMaster.getBannerImage());
    values.put(TableBannerMaster.C07_SHORT_DESC, bannerMaster.getShortDescription());
    values.put(TableBannerMaster.C09_BANNER_ADDED_ON, DateUtils.DateToString(bannerMaster.getAddedOn(), f));
    values.put(TableBannerMaster.C10_BANNER_UPDATE_ON, DateUtils.DateToString(bannerMaster.getUpdatedOn(), f));

    // Inserting Row
    database.update(TableBannerMaster.TABLE_BANNER_MASTER, values, TableBannerMaster.C01_BANNER_ID + " = ?", new String[]{String.valueOf(bannerMaster.getBannerId())});
    database.close(); // Closing database connection
}

示例更新查询