无论如何都无法创建列

时间:2014-01-01 22:58:13

标签: android sqlite android-sqlite

我正在创建数据库:

public static final String TABLE_MARKETS = "MARKET";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_PRICE = "price";
public static final String COLUMN_SUBSCRIBED = "sub";
public static final String COLUMN_MARKET_ID = "marketid";
public static final String COLUMN_DATE = "date";

private static final String DATABASE_CREATE = "create table "
        + TABLE_MARKETS + "(" + COLUMN_ID
        + " integer primary key autoincrement, "
        + COLUMN_DATE + " text not null, "
        + COLUMN_MARKET_ID + " INTEGER, "
        + COLUMN_PRICE + " REAL, "
        + COLUMN_SUBSCRIBED + " INTEGER);";

检查我有哪些列的方法:

private Market cursorToMarket(Cursor cursor) {
    Market market = new Market();
    Log.d("X", "0: " +cursor.getColumnName(0));
    Log.d("X", "1: " +cursor.getColumnName(1));
    Log.d("X", "2: " +cursor.getColumnName(2));
    Log.d("X", "3: " +cursor.getColumnName(3));//Null - not even printed out
    Log.d("X", "4: " +cursor.getColumnName(4));//Null - not even printed out
    market.marketid = cursor.getInt(0);
    market.setDate(cursor.getString(2));
    market.price = cursor.getFloat(1);
    return market;
}

在游标上调用方法:

Cursor cursor = database.query(MySQLiteHelper.TABLE_MARKETS, allColumns, null, null, null, null, null);
Market mark = cursorToMarket(cursor);

无论SQLite仅创建_id,price和date列。我犯过什么错误的想法?

2 个答案:

答案 0 :(得分:2)

您最近是否添加了一些列?添加新列时,如果您使用onCreate()方法创建数据库,则必须取消暂停应用并再次安装。

另外,请确保allColumns参数包含所有列。

答案 1 :(得分:2)

您应该验证allColumns实际上是否包含数据库中的所有列。这是我能想到这会失败的唯一原因。