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

标签 java android database sqlite sql-update

我想在我的数据库中创建一个新表(这意味着我将有两个表)。在该表中,我只有一个且唯一的值,并且该唯一值的用途是我将得到它,我将执行加法或减法,并再次插入/更新它以获取已减去或添加的新值(基本上是它)是一个整数值或 boolean 小数)。我还是 sqlite 的新手,也是 Android 开发的初学者。

我的问题是,默认情况下我将如何在表中只拥有/插入一个值,以及如何获取和更新它。因为当我在 Activity 本身或按钮/fab 中添加 INSERT 查询时,每次我打开 fragment 本身或按下按钮/fab 时,它都会添加新值。默认情况下,我如何在其中只有一个数据,以及如何获取它,以便我可以向其中添加或减去值并将其更新为新值?

我创建了两个表:

@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);
        }

最佳答案

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

     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);  

关于java - 在 Sqlite 中仅创建和更新单个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54937881/

相关文章:

java - 添加带有二维数组的文本文件

java - 有一个专门的锁对象有用吗?

java - onPrepareOptionsMenu 没有在 Fragments 中被调用

java - 如何部分更新 Realm 对象

java - Tomcat 中配置数据源失败

mysql - 自动提供数据库中的唯一ID

java - Netty ByteToMessageDecoder 运行的次数超出了它必须的次数

java - Firestore 无法添加 DocumentId

sql - 从大型 XML 数据创建 SQL 数据库表

java - 使用 VisualVM 通过防火墙连接到远程 jstatd 实例