在android中使用简单的SQLiteDatabase

时间:2014-11-14 11:32:30

标签: android

我的目的是在MainActivity上显示TABLE_CB中的最后一个KEY_CASH值

如果MainActivity部分与getCash()方法无关,则可以正常工作。

你能告诉我getCash()方法有什么问题吗?

这是我第一次使用SQLite,因此很难找到答案。

//TABLE_CB has two columns (long)KEY_ID and (integer)KEY_CASH
//CashBalanceTable class is just an description of a database to move data
package com.example.money;

public class CashBalanceTable {

    private int id;
    private int cash;

    public CashBalanceTable() {}
    public CashBalanceTable(int id, int cash)
    {
        this.id = id;
        this.cash = cash;
    }
    public CashBalanceTable(int cash)
    {
        this.cash = cash;
    }

    public void setCash(int cash)
    {
        this.cash = cash;
    }

    public int getId()
    {
        return id;
    }

    public int getCash()
    {
        return cash;
    }
}



//Main Activity
public class MainActivity extends ActionBarActivity {

    TextView textView1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);    
        setContentView(R.layout.activity_main);

        textView1 = (TextView)findViewById(R.id.textView1);        
        DatabaseHelper dbHelper = new DatabaseHelper(this);
        CashBalanceTable cb = new CashBalanceTable();
        cb = dbHelper.getCash();
        textView1.setText("Cash : " + cb.getCash() + " won");
    ////////////////////////////////////////////////////////////     

//In DatabaseHelper getCash() method
public CashBalanceTable getCash()
    {
        SQLiteDatabase db = this.getReadableDatabase();
        String[] columns = new String[] { KEY_ID, KEY_CASH };
        Cursor cursor = db.query(TABLE_CB, columns, "cash=?", null, null, null, null);
        cursor.moveToLast();
        int cash = cursor.getInt(cursor.getColumnIndex(KEY_CASH));
        CashBalanceTable cb = new CashBalanceTable(cash);
        return cb;

    }

// DatabaseHelper     公共类DatabaseHelper扩展了SQLiteOpenHelper {

    //database version
    private static final int DATABASE_VERSION = 1;

    //database name
    private static final String DATABASE_NAME = "APP_DATABASE";

    //table names
    private static final String TABLE_CB = "cashBalanceTable";

    //common column names
    private static final String KEY_ID = "id";
    private static final String KEY_CASH = "cash";

   //cashBalanceTable creation statement
    private static final String CREATE_TABLE_CB = "CREATE TABLE " + TABLE_CB + " (" + KEY_ID + "           I       INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_CASH + " INTEGER );";
    //ingore the I on the left;;

    private Context context;

    //constructor
    public DatabaseHelper(Context context) 
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.context = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) 
    {
        try {
            // creating required tables
            db.execSQL(CREATE_TABLE_CE);
            db.execSQL(CREATE_TABLE_CA);
            db.execSQL(CREATE_TABLE_AE);
            db.execSQL(CREATE_TABLE_AA);
            db.execSQL(CREATE_TABLE_CB);
            db.execSQL(CREATE_TABLE_AI);
        } catch (SQLException e) {
            Message.message(context, ""+e);
        }

    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        try {
            //on upgrade drop older tables
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_CE);
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_CA);
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_AE);
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_AA);
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_CB);
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_AI);
        } catch (Exception e) {
            Message.message(context, ""+e);
        }
        //create new tables
        onCreate(db);
    }

    //Insert initial cash value to cashBalanceTable (input class)
    public long insertCash(CashBalanceTable cb)
    {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(KEY_CASH, cb.getCash());

        //insert a row
        long id = db.insert(TABLE_CB, null, values);    
        return id;
    }


    public CashBalanceTable getCash()
    {
        SQLiteDatabase db = this.getReadableDatabase();
        String[] columns = new String[] { KEY_ID, KEY_CASH };
        Cursor cursor = db.query(TABLE_CB, columns, "cash=?", null, null, null, null);
        cursor.moveToLast();
        int cash = cursor.getInt(cursor.getColumnIndex(KEY_CASH));
        CashBalanceTable cb = new CashBalanceTable(cash);
        return cb;

    }

}

1 个答案:

答案 0 :(得分:0)

我认为,您还没有在方法中传递selectionArgs,请修改您的代码。

public CashBalanceTable getCash()
{
    SQLiteDatabase db = this.getReadableDatabase();
    String[] columns = new String[] { KEY_ID, KEY_CASH };
    Cursor cursor = db.query(TABLE_CB, columns, "cash=?", new String[]{selectionArgs }, null, null, null);
    cursor.moveToLast();
    int cash = cursor.getInt(cursor.getColumnIndex(KEY_CASH));
    CashBalanceTable cb = new CashBalanceTable(cash);
    return cb;

}