查询已经存在的数据库将返回空游标

时间:2018-07-17 22:36:07

标签: java android sqlite

enter image description here

我对sql不太熟悉,但我只是想从已创建的数据库文件中获取两个值。“ Balance”和“ VoucherBalance”。当我尝试查询数据库时,在将第三和第四参数添加到查询方法时,我得到了一个空的游标,或者如果将第三和第四参数保留为空,则得到了1的计数,这只是名称我要获取的两列中的一列。

为什么在指定我想要的值时我没有得到值?

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;

import java.util.Arrays;

public class DataBaseReader extends SQLiteOpenHelper
{
    private static final String TAG = "DataBaseReader";
    private static String DB_PATH = "/data/data/PACKAGENAME/databases/DATABAENAME.db";
    private static String DB_NAME = "TABLENAME";
    private SQLiteDatabase database;
    public double balance;
    public int voucher;
    private final Context myContext;
    public String text = "";


    public DataBaseReader(Context context)
    {
        super(context, DB_NAME, null, 1);
        this.myContext = context;
    }

    public void getWireAccountBalance()
    {
        String[] projection = {DatabaseContract.Database.COLUMN_BALANCE, DatabaseContract.Database.COLUMN_VOUCHER};
        String[] selectionArgs = {"Balance", "VoucherBalance"};
        String selection = DatabaseContract.Database.COLUMN_BALANCE + "=?" + " AND " + DatabaseContract.Database.COLUMN_VOUCHER + "=?";
        SQLiteDatabase db = SQLiteDatabase.openDatabase(DB_PATH, null, SQLiteDatabase.OPEN_READONLY);

        Cursor cursor = db.query(DatabaseContract.Database.TABLE_NAME, projection, selection, selectionArgs, null, null, null);


        Log.d(TAG, "Cursor count is " + String.valueOf(cursor.getCount()));
        text = Arrays.toString(cursor.getColumnNames());
        Toast.makeText(myContext, text, Toast.LENGTH_LONG).show();

        if (cursor.moveToFirst())
        {
            while (!cursor.moveToNext())
            {
                balance = cursor.getFloat(cursor.getColumnIndex(DatabaseContract.Database.COLUMN_BALANCE));
                voucher = cursor.getInt(cursor.getColumnIndex(DatabaseContract.Database.COLUMN_VOUCHER));
            }
        } else
        {

            Log.d(TAG, "Cursor count is " + String.valueOf(cursor.getCount()));
        }
    }


    @Override
    public synchronized void close()
    {
        super.close();
        if (database != null)
        {
            close();
        }
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase)
    {

    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1)
    {

    }
}

1 个答案:

答案 0 :(得分:1)

使用HDC hdc = GetDC(NULL); HDC memdc = CreateCompatibleDC(hdc); HBITMAP membmp = CreateCompatibleBitmap(hdc, width, height); LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { TCHAR greeting[] = _T("program_name"); PAINTSTRUCT ps; SelectObject(memdc, membmp); ... } 移至第一行,然后使用moveTofirst,它会返回false(当只有一行时),从而跳过第一行。

我建议使用:-

moveToNext

而不是:-

    while(cursor.moveToNext()) {
        balance = cursor.getFloat(cursor.getColumnIndex(DatabaseContract.Database.COLUMN_BALANCE));
        voucher = cursor.getInt(cursor.getColumnIndex(DatabaseContract.Database.COLUMN_VOUCHER));
    }