仅在Sqlite中创建和更新单个值

问题描述 投票:0回答:2

我想在我的Database中创建一个新表(意思是我将有两个表)。在那个表中,我将只有一个且唯一的值,并且仅使用该值,我将得到它并且我将执行加法或减法并再次插入/更新它已被减去或添加值的新值(基本上它是小数的integer值或boolean)。我还是sqlite的新手,也是android开发的初学者。

我的问题是,我将如何在表DE BYEULT中只插入/插入一个值,以及如何获取和更新它。因为当我在活动本身或按钮/ fab中添加INSERT查询时,每次我打开片段本身或按下按钮/ fab时,它都会添加新值。我如何在那里只有一个数据BY DEFAULT以及如何获取它以便我可以在其中添加或减去值并更新它以获得新值?

我创建了两个表:

@Override
    public void onCreate(SQLiteDatabase db) {
        final String SQL_CREATE_CASHFLOW_TABLE = "CREATE TABLE " + ItemEntry.TABLE_NAME2 + " (" +
                ItemEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                ItemEntry.COLUMN_INCOME + " INTEGER NOT NULL, " +
                ItemEntry.COLUMN_SAVINGS + " INTEGER NOT NULL " +
                ");";

        final String SQL_CREATE_ITEMLIST_TABLE = "CREATE TABLE " + ItemEntry.TABLE_NAME + " (" +
                ItemEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                ItemEntry.COLUMN_LABEL + " TEXT NOT NULL, " +
                ItemEntry.COLUMN_DETAIL + " TEXT, " +
                ItemEntry.COLUMN_AMOUNT + " INTEGER NOT NULL, " +
                ItemEntry.COLUMN_DATE + " TEXT " +
                ");";

        db.execSQL(SQL_CREATE_ITEMLIST_TABLE);
        db.execSQL(SQL_CREATE_CASHFLOW_TABLE);
    }

我从bk7尝试的代码:

public void insertOrUpdateTheIncomeAndSavings(int income, int savings){
        SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();

        Cursor cursor = sqLiteDatabase.rawQuery("SELECT * from "+ItemEntry.TABLE_NAME2,null);
        if(cursor.moveToNext()){
            //update the values of first row here

            //update income
            Cursor cursor2 = sqLiteDatabase.rawQuery("UPDATE "+ItemEntry.TABLE_NAME2+" SET "+ItemEntry.COLUMN_INCOME+
                    " = "+ income + " WHERE "+ItemEntry._ID +" = 1",null);

        }else{
            //insert the value here
            ContentValues cv = new ContentValues();
            cv.put(ItemEntry.COLUMN_INCOME, 0);
            cv.put(ItemEntry.COLUMN_SAVINGS, 0);
        }

        if(cursor!=null){
            cursor.close();
        }
        sqLiteDatabase.close();

    }

我如何在收入java中做到这一点:

package com.example.admin.test2;


import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;


/**
 * A simple {@link Fragment} subclass.
 */
public class Income extends Fragment {

    public Income() {
        // Required empty public constructor
    }

    private EditText textInput;
    private FloatingActionButton fabPlus;
    private FloatingActionButton fabMinus;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =  inflater.inflate (R.layout.fragment_income, container, false);

        textInput = (EditText) view.findViewById(R.id.editText2);
        fabPlus = (FloatingActionButton) view.findViewById(R.id.fabPlus);
        fabMinus = (FloatingActionButton) view.findViewById(R.id.fabMinus);

        fabPlus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            }
        });
        return view;
    }
}

并从此editText获取值:

textInput = (EditText) view.findViewById(R.id.editText2);

我想在这个textView中显示我在数据库中输入的输入但它不起作用..请检查代码:

public void getIncome() {
        Cursor cur = mDatabase.rawQuery("SELECT " + ItemContract.ItemEntry.COLUMN_INCOME + " as Income FROM " + ItemContract.ItemEntry.TABLE_NAME2
                , null);

        if (cur.moveToFirst()) {

            int incomeTotal = cur.getInt(cur.getColumnIndex("Income"));// get final total
            income.setText("₱ " + incomeTotal);
        }
java android database sqlite sql-update
2个回答
1
投票

如果第一行不存在则插入第一行并且存在更新第一行。

     public void insertOrUpdateTheIncomeAndSavings(int income, int savings){
            SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();

          Cursor cursor = sqLiteDatabase.rawQuery("select * from "+ItemEntry.TABLE_NAME2,null);
            if(cursor.moveToNext()){
            //update the values of first row here

            }else{
                //insert the value here
            }

            if(cursor!=null){
            cursor.close();
            }
           sqLiteDatabase.close();

        }

编辑:在Onclick中添加此项

 DBHelper dbhelper = new DBHelper(getActivity());
  int income = Integer.valueOf(incomeEditText.getText().toString());
  int savings = Integer.valueOf(savingsEditText.getText().toString());
  dbhelper.insertOrUpdateTheIncomeAndSavings(income,savings);  

0
投票
public void updateEmployeeName(String empNo,String new_empname) {
    Cursor cursor = null;
    String empName = "";
    try {
        cursor = SQLiteDatabaseInstance_.rawQuery("SELECT EmployeeName 
        FROM Employee WHERE EmpNo=?", new String[] {empNo + ""});
        if(cursor.getCount() > 0) {
            cursor.moveToFirst();
            empName = cursor.getString(cursor.getColumnIndex("EmployeeName"));
            update(empname);
        }else{
            update(new_empname);
        } 
      }finally {
        cursor.close();
     }
 }    

**Update single value use this query:-**
    public void update(){
    ContentValues cv = new ContentValues();
    cv.put(ColumnName, newValue);
    db.update(TABLE_NAME, cv, Column + "= ?", new String[] {rowId});
}

    **delete single value use this query:-**

    public void delete_singlevalue(String newValue) {
            SQLiteDatabase db = getWritableDatabase();          
            db.execSQL("delete from TABLE_NAME where 
            ColumnName="+"'"+newValue+"'");
            db.close();   
      }
© www.soinside.com 2019 - 2024. All rights reserved.