2015-03-02 92 views
0

我發現了一些處理這個堆棧溢出的聯繫,但沒有任何解決方案都使這裏爲我工作得好:sqlite的爲Android,如何更新表

我試圖添加一列舉行日期(並且可能需要在將來添加時間列),但自從表已經創建以來遇到了一些麻煩。這裏是數據庫代碼:

public class MyDatabase { 

public static final String KEY_ROWID = "_id"; 
public static final String KEY_DATE = "Date"; 
public static final String KEY_SYS = "Systolic"; 
public static final String KEY_DIA = "Diastolic"; 

private static final String DATABASE_NAME = "Blood Pressures";//Used to reference database 
private static final String DATABASE_TABLE = "bloodPressureTable";//Tables can be stored in database 
// this will be used to store ID, date, systolic and diastolic 
private static final int DATABASE_VERSION = 9; 

private DbHelper ourHelper; 
private final Context ourContext; 
private SQLiteDatabase ourDatabase; 

public String getData() { 
    String[] columns = new String[]{KEY_ROWID, KEY_SYS, KEY_DIA}; 
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null); 
    String result = ""; 

    int iRow = c.getColumnIndex(KEY_ROWID); 
    int iSys = c.getColumnIndex(KEY_SYS); 
    int iDia = c.getColumnIndex(KEY_DIA); 

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) { //if cursor is after position of our last entry, then stop 
     result = result + c.getString(iRow) +  //get ROW_ID column, get name of first row and hotness, create new string 
       " " + c.getString(iSys) + " " + 
       c.getString(iDia) + "\n"; 
    } 

    return result; 
} 

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) { //This is only called when we first create a database 
     // when then we will just cycle through onUpgrade, passes a database in 
     db.execSQL(
       "CREATE TABLE " + DATABASE_TABLE + " (" + //create a table called table name 
         //adds columns inside parenthesise 
         KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + //integer increment automatically, referenced by keyID 
         KEY_DATE + "TEXT NOT NULL, " + //SQL uses the word text rather than string 
         KEY_SYS + " INTEGER, " + 
         KEY_DIA + " INTEGER);" 
     ); 
    } 

    @Override //called if oncreate has already been called 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 


     //db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE); //If table name exists it is going 
     // to drop it and then all we have to do is call our onCreate method 
     //Removes table added by create table statement 
     //onCreate(db); 

     if (newVersion > oldVersion) { 
      db.execSQL("ALTER TABLE " + DATABASE_TABLE + 
      " ADD COLUMN " + KEY_DATE + " TEXT NOT NULL, "); 
     } 

    } 
} 

//Constructor below 
public MyDatabase(Context c) { 
    ourContext = c; 
} 
//open() allows us to open and write to database 

public MyDatabase open() { 
    ourHelper = new DbHelper(ourContext); // this is a new instance of that object passing in the context 
    ourDatabase = ourHelper.getWritableDatabase();//here we set up our database, 
    // getting the writeable database. This will open up our database through our helper 
    return this; 
} 

public void close() { 
    ourHelper.close(); //close our SQLiteOpenHelper 

} 

public long createEntry(int systolic, int diastolic) { 
    ContentValues cv = new ContentValues(); 
    Calendar c = Calendar.getInstance(); 
    String s = Integer.toString(c.get(Calendar.DAY_OF_MONTH)) + "/" + Integer.toString(c.get(Calendar.MONTH)) + 
      "/" + Integer.toString(c.get(Calendar.YEAR)); 

    cv.put(KEY_DATE, s); 
    cv.put(KEY_SYS, systolic); 
    cv.put(KEY_DIA, diastolic);//Put the string in KEY_NAME column? 
    return ourDatabase.insert(DATABASE_TABLE, null, cv); //we want this method to return this line 
} 
} 

有人可以告訴我的解決方案嗎?正如你所看到的,我嘗試了兩種不同的方法(其中一個在onUpgrade中被註釋掉了)。正如你可以看到我在版本10上已經玩過它了!

這裏是logcat的:

03-02 16:03:48.416 1811-1811/com.dissertation.michael.biolog E/SQLiteLog﹕ (1) Cannot add a NOT NULL column with default value NULL 
03-02 16:03:48.416 1811-1811/com.dissertation.michael.biolog D/AndroidRuntime﹕ Shutting down VM 
03-02 16:03:48.416 1811-1811/com.dissertation.michael.biolog W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41696bd8) 
03-02 16:03:48.416 1811-1811/com.dissertation.michael.biolog E/AndroidRuntime﹕ FATAL EXCEPTION: main 
Process: com.dissertation.michael.biolog, PID: 1811 
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1111, result=-1, data=Intent { (has extras) }} to activity {com.dissertation.michael.biolog/com.dissertation.michael.biolog.MainActivity}: android.database.sqlite.SQLiteException: Cannot add a NOT NULL column with default value NULL (code 1): , while compiling: ALTER TABLE bloodPressureTable ADD COLUMN DateTEXT NOT NULL 
     at android.app.ActivityThread.deliverResults(ActivityThread.java:3391) 
     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3434) 
     at android.app.ActivityThread.access$1300(ActivityThread.java:138) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:149) 
     at android.app.ActivityThread.main(ActivityThread.java:5045) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:515) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602) 
     at dalvik.system.NativeStart.main(Native Method) 
Caused by: android.database.sqlite.SQLiteException: Cannot add a NOT NULL column with default value NULL (code 1): , while compiling: ALTER TABLE bloodPressureTable ADD COLUMN DateTEXT NOT NULL 
     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 
     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889) 
     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500) 
     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) 
     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58) 
     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31) 
     at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1672) 
     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1603) 
     at com.dissertation.michael.biolog.MyDatabase$DbHelper.onUpgrade(MyDatabase.java:75) 
     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:257) 
     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164) 
     at com.dissertation.michael.biolog.MyDatabase.open(MyDatabase.java:90) 
     at com.dissertation.michael.biolog.MainActivity.onActivityResult(MainActivity.java:170) 
     at android.app.Activity.dispatchActivityResult(Activity.java:5430) 
     at android.app.ActivityThread.deliverResults(ActivityThread.java:3387) 
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3434) 
    at android.app.ActivityThread.access$1300(ActivityThread.java:138) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:149) 
at android.app.ActivityThread.main(ActivityThread.java:5045) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602) 
at dalvik.system.NativeStart.main(Native Method) 
03-02 16:03:48.424 1811-1811/com.dissertation.michael.biolog I/Process: Sending signal. PID: 1811 SIG: 9 

可選地(且優選地,如果它是簡單)刪除完全使得版本1被再次創建將是理想的我的表的方法。 (此時沒有有用的數據輸入到數據庫中)......乾杯!

+0

檢查logcat的'java.lang.NumberFormatException:無效INT:「300-400」'你想保存一個非整數在一個整數變量中。放置您的MainActivity代碼以查看錯誤的位置。 – 2015-03-02 13:07:48

+0

對不起,那個logcat碰巧在我的程序中發現了另一個錯誤...我再次運行程序並用新的logcat編輯了這個問題 – MichaelAndroidNewbie 2015-03-02 16:13:00

+0

只是想知道......你甚至讀過你的日誌嗎? '不能添加具有默認值NULL的NOT NULL列似乎對我很明顯。另外請發佈您的MainActivity代碼以查看錯誤的位置。 – 2015-03-02 16:23:27

回答

3

由於java.lang.NumberFormatException: Invalid int: "300-400",您的應用程序崩潰,這意味着您傳遞無效格式的INTEGER,並且300-400不是有效的int。

P.S.如果你想進入300-400到數據庫列中使用TEXTVARCHAR(15)爲你列的數據類型

+0

對不起,那個logcat碰巧在我的程序中發現了另一個bug ......我已經再次運行該程序並編輯了這個KEY_DATE +「TEXT NOT NULL」的logcat – MichaelAndroidNewbie 2015-03-02 16:13:11

+0

這個問題+不能爲空,因爲它已經有一些值在數據庫中,允許空值,以便可以更改。 – 2015-03-03 04:48:43

+0

完美,謝謝 – MichaelAndroidNewbie 2015-03-03 15:44:35