2015-04-07 84 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()),完全可能的重复不是数值。