⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 walletdbadapter.java

📁 一个简单的android手机程序
💻 JAVA
字号:
package com.android;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;


public class WalletDbAdapter {

    public static final String KEY_ID = "_id";
    public static final String KEY_DATE = "wDate";
    public static final String KEY_CLASS = "wClass";
    public static final String KEY_ABSTRACT = "wAbstract";
    public static final String KEY_MONEY = "wMoney";
    public static final String KEY_SURPLUS = "wSurplus";

    private static final String TAG = "WalletDbAdapter";
    private DatabaseHelper mDbHelper;
    private SQLiteDatabase mDb;
    
    /**
     * Database creation sql statement
     */

    private static final String DATABASE_NAME = "walletdb";
    private static final String DATABASE_TABLE = "wallet";
    private static final int DATABASE_VERSION = 2;
    private static final String DATABASE_CREATE =
            "create table wallets ("
                    + "_id integer primary key autoincrement, "
                    + "wDate       varchar not null, "
                    + "wClass      varchar not null,"
                    + "wAbstract   varchar not null,"
                    + "wMoney      varchar not null,"
                    + "wSurplus    varchar);";

    private final Context mCtx;

    private static class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {

            db.execSQL(DATABASE_CREATE);
        }

		@Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS wallets");
            onCreate(db);
        }
    }

    /**
     * Constructor - takes the context to allow the database to be
     * opened/created
     * 
     * @param ctx the Context within which to work
     */
    public WalletDbAdapter(Context ctx) {
        this.mCtx = ctx;
    }

    /**
     * Open the wallets database. If it cannot be opened, try to create a new
     * instance of the database. If it cannot be created, throw an exception to
     * signal the failure
     * 
     * @return this (self reference, allowing this to be chained in an
     *         initialization call)
     * @throws SQLException if the database could be neither opened or created
     */
    public WalletDbAdapter open() throws SQLException {
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }
    
    public void close() {
        mDbHelper.close();
    }


    /**
     * Create a new wallet using the params provided. If the wallet is
     * successfully created return the new rowId for that wallet, otherwise return
     * a -1 to indicate failure.
     * 
     * @return rowId or -1 if failed
     */
    public long createItem(String wDate, String wClass,String wAbstract, String wMoney,String wSurplus) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_SURPLUS, wSurplus);
        initialValues.put(KEY_MONEY, wMoney);
        initialValues.put(KEY_ABSTRACT, wAbstract);
        initialValues.put(KEY_CLASS, wClass);
        initialValues.put(KEY_DATE, wDate);

        return mDb.insert(DATABASE_TABLE, null, initialValues);
    }

    /**
     * Return a Cursor over the list of all items in the database
     * 
     * @return Cursor over all wallets
     */
    public Cursor fetchAllItems() {
        return mDb.query(DATABASE_TABLE, new String[] {KEY_ID, KEY_DATE,
                KEY_CLASS,KEY_ABSTRACT,KEY_MONEY,KEY_SURPLUS}, null, null, null, null, null);
    }
    
    
    /**
     * Return a Cursor over the list of items in the database by param
     * param KEY_DATE  of item to retrieve
     * param KEY_CLASS  of item to retrieve
     * @return Cursor over all wallets
     */
    public Cursor fetchItemsByParam(String iDate,String iClass) {
        return mDb.query(true,DATABASE_TABLE, new String[] {KEY_ID, KEY_DATE,
                KEY_CLASS,KEY_ABSTRACT,KEY_MONEY,KEY_SURPLUS}, 
                KEY_DATE +"  like  '%" + iDate + "%' and " + KEY_CLASS + "='" + iClass +"'",
                null, null, null, null,null);
    }
    

    
    
    /**
     * Return a Cursor over the list of all items in the database
     * 
     * @return Cursor over all wallets
     */
    public Cursor fetchAllWallets() {
        return mDb.query(DATABASE_TABLE, new String[] {KEY_ID,KEY_DATE,
                KEY_CLASS,KEY_MONEY}, null, null, null, null, null);
    }
    
    /**
     * Delete all wallet
     * 
     * @return true if deleted, false otherwise
     */
    public boolean deleteAllWallets() {

        return mDb.delete(DATABASE_TABLE,null, null) > 0;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -