无法从sqlite数据库中的第二个表中检索并在listview中显示

时间:2012-10-06 14:52:00

标签: android-listview android-sqlite

DatabaseAdapter

//User Table

public static final String KEY_ROWID = "_id";
public static final String KEY_UNAME = "name";
public static final String KEY_USURNAME = "surname";
public static final String KEY_UUSERNAME = "username";
public static final String KEY_UPASSWORD = "password";
public static final String KEY_UEMAILADDRESS = "emailadd";

//Account Table
public static final String KEY_ROWID1 = "_id1";
public static final String KEY_BANKNAME =" bankname";
public static final String KEY_TYPE = " type";
public static final String KEY_ACCNUM = " accnum";
public static final String KEY_BALANCE = " balance";
public static final String KEY_EXPIRYDATE = " expirydate";

//Database
private static final String DATABASE_NAME = "MMS2";
public static final String DATABASE_TABLE = " Usertb";
    public static final String DATABASE_TABLE1 = " Accounttb";
private static final String DATABASE_TABLE2 = " Transactiontb";
private static final String DATABASE_TABLE3 = " Categorytb";

//Database Version
private static final int DATABASE_VERSION = 1;
private static String Usertb;
private static String Accounttb;

private DbHelper MHelper;
private final Context MContext;
private SQLiteDatabase Mdatabase;

private static final String DATABASE_USER_TABLE = "CREATE TABLE" + DATABASE_TABLE + "  (" +
        KEY_ROWID + " INTEGER PRIMARY KEY, " +
        KEY_UNAME + " TEXT , " +
        KEY_USURNAME + " TEXT , " +
        KEY_UUSERNAME + " TEXT , " +
        KEY_UPASSWORD + " TEXT , " +
        KEY_UEMAILADDRESS + " TEXT );" ;

private static final String DATABASE_ACCOUNT_TABLE1 = "CREATE TABLE" + DATABASE_TABLE1 + " (" +
        KEY_ROWID1 + " INTEGER PRIMARY KEY, " +
        KEY_BANKNAME + " TEXT , " +
        KEY_TYPE + " TEXT , " +
        KEY_ACCNUM + " TEXT , " +
        KEY_BALANCE + " TEXT , " +
        KEY_EXPIRYDATE + " TEXT  );" ;


private static class DbHelper extends SQLiteOpenHelper{



    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL(DATABASE_USER_TABLE);
        db.execSQL(DATABASE_ACCOUNT_TABLE1);


    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_USER_TABLE);
        db.execSQL("DROP TABLE IF EXISTS" + DATABASE_ACCOUNT_TABLE1);
        onCreate(db);

    }
}

public DatabaseAdapter(Context c){
    MContext = c;
}

public DatabaseAdapter open(){
    MHelper = new DbHelper(MContext);
    Mdatabase = MHelper.getWritableDatabase();
    return this;
}

public void close(){

    MHelper.close();
}
    public Cursor fetchListItems() {

        Cursor cursor = Mdatabase.query(DATABASE_TABLE1, new String[] 
                      { KEY_ROWID1, KEY_BANKNAME, KEY_TYPE, KEY_BALANCE}, 
                      null, null, null, null, null);

        if (cursor != null) {
            cursor.moveToFirst();
        }
        return cursor;
    }

}

TransactionListView

DatabaseAdapter dbHelper = new DatabaseAdapter(this);
        dbHelper.open();

        // Get a Cursor for the list items
        Cursor listCursor = dbHelper.fetchListItems();
        startManagingCursor(listCursor);

        // set the custom list adapter
        setListAdapter(new MyListAdapter(this, listCursor));
    }

    private class MyListAdapter extends ResourceCursorAdapter {

        public MyListAdapter(Context context, Cursor cursor) {
            super(context, R.layout.list_item_with_description, cursor);
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {

            TextView title = (TextView) view.findViewById(R.id.item_title);
            title.setText(cursor.getString(
                        cursor.getColumnIndex(DatabaseAdapter.KEY_BANKNAME)));

            TextView details = (TextView) view.findViewById(R.id.item_details);
            StringBuffer detailsText = new StringBuffer();

            int price = cursor.getInt(cursor.getColumnIndex(DatabaseAdapter.KEY_BALANCE));
            if (price > 0){
                detailsText.append("$"+price+".00");
            } else {
                detailsText.append("Price Unavailable");
            }
            String description = cursor.getString(cursor.getColumnIndex(
                                                    DatabaseAdapter.KEY_TYPE));
            if (description != null && description.length() > 0){
                detailsText.append(", "+description);
            }
            details.setText(detailsText.toString());

        }

    }

}

我收到了这个错误

  

10-06 17:52:21.107:I / Database(708):sqlite返回:错误代码= 1,msg =没有这样的列:_id1

0 个答案:

没有答案