2015-04-07 85 views
0

我在使用數據庫中的整數時遇到問題。我有一個SQLiteOpenHelper這裏我創建無法將整數類型插入到數據庫中

將對DBAdapter:

public static final String KEY_ROWID="id"; 
public static final String KEY_EXR="name"; 
public static final String KEY_WGHT="weight"; 
public static final String KEY_REP="repetition"; 
public static final String KEY_DATE="duedate"; 
final static String[] columns = {KEY_ROWID, KEY_EXR, KEY_WGHT, KEY_REP}; 



    final static String DATABASE_NAME = "ExerciseDB"; 
final static String DATABASE_TABLE = "exercisetb"; 
final static int DATABASE_VERSION = 2; 


//DBAdapter için Loglar 

private static final String TAG = "DBAdapter"; 



private static final String DATABASE_CREATE = 
     "create table if not exists exercisetb (id integer primary key autoincrement, " 
       + "name VARCHAR not null, weight VARCHAR not null, repetition integer not null);"; 

插入:

public long insertRecord() { 


      ContentValues values = new ContentValues(); 

      values.put(DBAdapter.KEY_EXR, name.toString()); 

      values.put(DBAdapter.KEY_WGHT, weight.toString(); 

      values.put(DBAdapter.KEY_REP, Integer.parseInt(repetition.toString())); 
      values.clear(); 

      return mDbHelper.getWritableDatabase().insert(DBAdapter.DATABASE_NAME, null, values); 

     } 

編輯: 我加insertRecord()來顯示哪個步驟出現問題:

public long insertRecord() { 


      ContentValues values = new ContentValues(); 

      values.put(DBAdapter.KEY_EXR, name.toString()); 
      Log.i(TAG,"Name field assign."); 

      values.put(DBAdapter.KEY_WGHT, Integer.parseInt(weight.toString())); 
      Log.i(TAG,"weight assisgn."); 
      values.put(DBAdapter.KEY_REP, Integer.parseInt(repetition.toString())); 
      Log.i(TAG,"repetition field assign."); 
      values.clear(); 

      return mDbHelper.getWritableDatabase().insert(DBAdapter.DATABASE_NAME, null, values); 

     } 

嘗試插入後的日誌輸出:

04-07 15:23:25.217 3442-3442/com.example.alperen.exercisem I/addnew﹕ OnClick'e girildi 
04-07 15:23:25.217 3442-3442/com.example.alperen.exercisem I/addnew﹕ Name field assign. 
04-07 15:23:25.217 3442-3442/com.example.alperen.exercisem I/addnew﹕ weight assisgn. 
04-07 15:23:25.217 3442-3442/com.example.alperen.exercisem D/AndroidRuntime﹕ Shutting down VM 
04-07 15:23:25.217 3442-3442/com.example.alperen.exercisem W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x430ef140) 
04-07 15:23:25.227 3442-3442/com.example.alperen.exercisem E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: com.example.alperen.exercisem, PID: 3442 
    java.lang.NumberFormatException: Invalid int: "android.widget.EditText{21d1d000 VFED..CL .F...... 0,166-720,249 #7f080043 app:id/repetition}" 
      at java.lang.Integer.invalidInt(Integer.java:137) 
      at java.lang.Integer.parse(Integer.java:374) 
      at java.lang.Integer.parseInt(Integer.java:365) 
      at java.lang.Integer.parseInt(Integer.java:331) 
      at com.example.alperen.exercisem.Addnew$1.insertRecord(Addnew.java:69) 

問題發生在最後一個變量上。我們可以把KEY_EXR; KEY_WGHT,但不是KEY_REP。如果我只將KEY_REP列更改爲VARCHAR,那麼我可以將數據添加到該行。我在創建表格時可能會犯錯。

+0

repetition INTEGER not null –

+0

您試圖插入哪個**整數**? –

+0

你有錯誤嗎?結果是什麼? – niyou

回答

-1

請評論線在你insertRecord()梅索德:

//values.clear();

因爲它清除了值列表中的值,並且它不會更新表中的新行。

0

對於非空字段,也指定默認值。 此外,根據崩潰報告,有數字格式異常。檢查Integer.parseInt(repetition.toString()),完全可能的重複不是數值。