如何修改SQL表以使其包含具有TRUE / FALSE值的列?

时间:2012-03-27 16:03:20

标签: android sql

我正在尝试修改SQL表以获得真正的错误值。我使用类型BIT为1或0.我试图改变表但它似乎没有正常工作。列索引为-1。

对于我的sqlitehelper类,我尝试将其用于我的onupgrade方法:

   @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
    {

        Log.d(TAG,"inner onupgrade called");
        Log.w(TAG, "Upgrading database from version " 
                + oldVersion + " to "
                + newVersion + ", which will destroy all old data");
        //db.execSQL("DROP TABLE IF EXISTS " + TableMetaData.TABLE_NAME);
        db.execSQL("ALTER TABLE " + TableMetaData.TABLE_NAME 
                + " ADD COLUMN " + TableMetaData.HAS_UPLOADED + " BIT DEFAULT 0"
                + ";");
           //onCreate(db);
    }

当我尝试在我的应用程序中稍后读取该列时,我收到错误,并且我发现列索引为-1。我正确地修改了桌子吗?

以下是我从logcat获得的错误:

03-27 17:48:37.111: E/CursorWindow(2884): Bad request for field slot 0,-1. numRows = 1, numColumns = 5
03-27 17:48:37.111: D/AndroidRuntime(2884): Shutting down VM
03-27 17:48:37.111: W/dalvikvm(2884): threadid=1: thread exiting with uncaught exception (group=0x400259f8)
03-27 17:48:37.131: E/AndroidRuntime(2884): FATAL EXCEPTION: main
03-27 17:48:37.131: E/AndroidRuntime(2884): java.lang.IllegalStateException: get field slot from row 0 col -1 failed
03-27 17:48:37.131: E/AndroidRuntime(2884):     at android.database.CursorWindow.getString_native(Native Method)
03-27 17:48:37.131: E/AndroidRuntime(2884):     at android.database.CursorWindow.getString(CursorWindow.java:329)
03-27 17:48:37.131: E/AndroidRuntime(2884):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:49)

2 个答案:

答案 0 :(得分:0)

简短回答,sqlite3 http://sqlite.org/datatype3.html

中没有BIT数据类型

答案 1 :(得分:0)

在contentprovider中,我没有在hashmap中添加列。

public class ThoughtProvider extends ContentProvider
{
//Logging helper tag. No significance to providers.
private static final String TAG = "ThoughtProvider";

//Projection maps are similar to "as" construct
//in an sql statement where by you can rename the 
//columns.
private static HashMap<String, String> sBooksProjectionMap;
static 
{
    sBooksProjectionMap = new HashMap<String, String>();
    sBooksProjectionMap.put(TableMetaData._ID, 
                                TableMetaData._ID);

//I needed to add:


    sBooksProjectionMap.put(TableMetaData.HAS_UPLOADED,
                            TableMetaData.HAS_UPLOADED);