当前位置: 首页 > 知识库问答 >
问题:

在android中无法从SQL数据库中的列中检索数据

南宫凯康
2023-03-14

我一直在做一个应用程序,它有一个SQL数据库。我正在尝试检索SQL数据库“year_details”的特定列“col3”中的数据,但当按下一个按钮时,我得到了很多错误,谁能告诉我,我做错了什么。

public class AddTransaction extends AppCompatActivity {


    int type1 ;
    int type2;
    double amount;
    ArrayList<add_transaction_data> data;
    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_transaction);
        ActionBar actionBar = getSupportActionBar();
        assert actionBar != null;
        actionBar.hide();
        populateList();

        final ImageButton cancel = findViewById(R.id.at_btn_cancel);
        final EditText EditAmount = findViewById(R.id.at_enter_amount);
        ImageButton save = findViewById(R.id.at_btn_save);
        ImageButton income = findViewById(R.id.at_btn_income);
        ImageButton loan = findViewById(R.id.at_btn_loan);
        RecyclerView recyclerView = findViewById(R.id.at_btn_recyclerview);

        recyclerView.setLayoutManager(new GridLayoutManager(this , 5));
        recyclerView.setAdapter(new at_btn_adapter(data, new onClickRecyclerButtion() {
            @Override
            public void onPositionClicked(int position) {
                type1 = 2;
                type2 = position;

            }
        }));


        income.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                type1 = 1;
                type2 = -1;
            }
        });

        loan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                type1 = 1;
                type2 = -2;
            }
        });

        context = this;
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println("it is  all cool here");
                DatabaseHelper1 databaseHelper1 = new DatabaseHelper1(getBaseContext());
                DatabaseHelper2 databaseHelper2 = new DatabaseHelper2(getBaseContext());
                double amount = Double.parseDouble(EditAmount.getText().toString());
                System.out.println(amount);
                Calendar c = Calendar.getInstance();
                int year = c.get(Calendar.YEAR);
                int month = c.get(Calendar.MONTH);
                String m = changeString(month);
                System.out.println(type1);
                double mRamount = -1;
                if(type1 == 1){
                   Cursor cursor = databaseHelper1.getRemainingAmount(m , String.valueOf(year));
                   mRamount= cursor.getDouble(3);
                   System.out.println(mRamount);//printing amount.
                }
                startActivity(new Intent(AddTransaction.this , MainActivity.class));
            }
        });

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(AddTransaction.this , MainActivity.class));
            }
        });


    }


    void populateList(){
        data = new ArrayList<>();
        data.add(new add_transaction_data("Add" , R.drawable.ic_add_black_24dp));
        data.add(new add_transaction_data("Shopping" , R.drawable.at_ic_shopping));
        data.add(new add_transaction_data("Food" , R.drawable.at_ic_food));
        data.add(new add_transaction_data("Traveling" , R.drawable.at_ic_travel));
        data.add(new add_transaction_data("Education" , R.drawable.at_ic_education));
        data.add(new add_transaction_data("Energy" , R.drawable.at_ic_energy));
        data.add(new add_transaction_data("House" , R.drawable.at_ic_home));
        data.add(new add_transaction_data("Fitness" , R.drawable.at_ic_sports));
        data.add(new add_transaction_data("Personal" , R.drawable.at_ic_personal));
        data.add(new add_transaction_data("Other" , R.drawable.at_ic_other));
    }

    String changeString(int month){
        String m;
        switch (month){
            case 0:
                m = "January";
                break;
            case 1:
                m = "Feburary";
                break;
            case 2:
                m = "March";
                break;
            case 3:
                m = "April";
                break;
            case 4:
                m = "May";
                break;
            case 5:
                m = "June";
                break;
            case 6:
                m = "July";
                break;
            case 7:
                m = "August";
                break;
            case 8:
                m = "September";
                break;
            case 9:
                m = "October";
                break;
            case 10:
                m = "November";
                break;
            case 11:
                m = "December";
                break;
            default:
                m = " ";

        }
        return m;
    }

}



public class DatabaseHelper1 extends SQLiteOpenHelper {

    private static final int version = 3;
    private static final String name = "YEAR_DETAILS";
    private static final String COL0 = "YEAR";
    private static final String COL1 = "MONTH";
    private static final String COL2 = "SPENT";
    private static final String COL3 = "REMAINING";
    private static final String TAG = "Database Helper 1";

    public DatabaseHelper1(@Nullable Context context) {
        super(context, name, null, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.d(TAG , "Database 1 is being created");
        String create_table = "CREATE TABLE "+name+"("+
                COL0+" INTEGER, "+
                COL1+" TEXT, "+
                COL2+" DOUBLE, "+
                COL3+" DOUBLE);";
        db.execSQL(create_table);
    }

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

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }

    private boolean createMonth(String month , String year){
        Log.d(TAG , "month row is being created");
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL0 , year);
        contentValues.put(COL1 , month);
        contentValues.put(COL2 , 0.0);
        contentValues.put(COL3 , 0.0);
        long result = db.insert(name,null , contentValues);
        return result != -1;
    }

    public boolean spentChange(String month , String year , Double newAmount){
        Log.d(TAG , "spent amount is being changed");
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COL2 , newAmount);
        long result = db.update(name, values , "MONTH = ? AND YEAR = ?", new String[]{month , year});
        return  result != -1;
    }

    public boolean remainingChange(String month ,String year, Double newAmount){
        Log.d(TAG , "Remaining amount is being changed");
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues =new ContentValues();
        contentValues.put(COL3 , newAmount);
        long result = db.update(name , contentValues , "MONTH = ? AND YEAR = ?" , new String[]{month , year});
        return result != -1;
    }


    public void checkExistance(String month, String year){
        Log.d(TAG , "Checking if month already exits");
        SQLiteDatabase database = this.getWritableDatabase();
        Cursor cur =  database.query(name , null , "MONTH = ? AND YEAR = ?" , new String[]{month , year} , null , null , null);
        System.out.println(cur);
        if(cur.getCount() <= 0) {
            boolean b = createMonth(month , year);
            if(b)
                Log.d(TAG , "checkExistance: failed to create month");
            else
                Log.d(TAG, "checkExistance: month created successfully");
        }
    }

    public Cursor getdata(){
        SQLiteDatabase db = this.getWritableDatabase();
        String query = "SELECT * FROM YEAR_DETAILS;";
        return db.rawQuery(query , null);
    }

    public Cursor getSpentAmount(String month , String year){
        SQLiteDatabase db = this.getReadableDatabase();
        String query = "SELECT * FROM YEAR_DETAILS WHERE MONTH = " + month+ " AND YEAR = "+year;
        return db.rawQuery(query , null);
    }

    public Cursor getRemainingAmount(String month , String year){
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cur =  db.query(name , new String[]{COL3} , "MONTH = ? AND YEAR = ?" , new String[]{month , year} , null , null , null);
        return cur;
    }


}


共有1个答案

魏彦
2023-03-14

在getData()函数的查询中有一个分号

String query = "SELECT * FROM YEAR_DETAILS;"; 

您应该将其更改为

String query = "SELECT * FROM YEAR_DETAILS";
 类似资料:
  • 我正在两个标记之间画一条路线,我想保存那条路线。为此,我将包含lat和lng的ArrayList保存在Firebase数据库中。但我在取回航路点时遇到了问题。我是这样插入的: 在检索数据时,我尝试执行以下操作:

  • 我想从数据库中检索名称。但我总是收到错误: 数据库Java语言 它不断返回错误 android.database.CursorIndexOutOfBoundsException:请求索引-1,大小为1,字符串名称=helper.getProductNameT(cur); 有人知道我的编码哪里出错了吗?

  • 问题内容: 我正在制作一个程序,可以从我创建的数据库中检索字段的输入数据/值。但是,当我运行它时,输出始终为null。不知道怎么了 这是我的代码: 在我的主班我有以下代码: 我不知道为什么我运行它时总是说null。 问题答案: 您实际上并没有在设置员中“设置”任何东西。 最重要的是:我对getter和setter的理解使您可以在getter中进行查询。

  • 问题内容: 我正在尝试以android应用程序的形式创建一个小型课程聚合程序。 我的课程全部存储在Firebase实时数据库中,该数据库可从firebase控制台查看,并且一切正常。 问题是我已经编写了一个Java方法来连接到DB,从DB检索数据,将数据转换为Custom Java对象,将其附加到另一个Custom Java对象,然后将该对象保存到ArrayList中。 与数据库的连接已成功建立,

  • 我是android的新手,在从服务器上的mysql数据库检索图像url时遇到了问题,我可以使用Hashmap检索字符串,但无法从数据库中检索图像url 我已经使用适配器类链接listview、imageview、textview等 任何人可以检查下面的代码,这样我可以检索图像路径,并链接到imageView和显示图像从数据库插入路径。 mainactivity.java sample.php ac

  • 我试图创建一个简单的程序,从用户的名字,手机号码和电子邮件地址,然后把数据在Firebase实时数据库。 有3个输入框和一个按钮,按一下就可以完成上面的操作。代码如下: 我这样设置了消防基地: