Android Can';t将整数类型插入数据库
我在DB中使用整数时遇到问题。我在这里创建了一个SQLiteOpenHelper 数据库适配器:Android Can';t将整数类型插入数据库,android,sqlite,Android,Sqlite,我在DB中使用整数时遇到问题。我在这里创建了一个SQLiteOpenHelper 数据库适配器: 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
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)
问题发生在最后一个变量上。我们可以把钥匙放在EXR上;KEY_WGHT而不是KEY_REP。如果我将KEY_REP列更改为VARCHAR only,那么我可以向该行添加数据。我可能在创建表的过程中出错。请在insertRecord()方法中注释行: //value.clear()
因为它会从值列表中清除值,并且不会更新表中的新行 对于非空字段,也指定默认值。
此外,根据事故报告,存在数字格式异常。选中Integer.parseInt(repeation.toString()),很可能重复不是数值。repeation Integer not null要插入哪个整数?是否有错误?结果是什么?如果使用“重复整数非空”,则必须声明默认值,如
默认值0
。我想知道你是如何毫无例外地以这种方式创建DB的。另外,在插入之前(在values.clear();
)清除所有插入值,这样就不会插入任何内容,包括KEY\u EXR和KEY\u WGHT。还要尝试使用values.put(DBAdapter.KEY_REP,0xDEAD);为了看到它添加了一个整数(我的意思是没有整数解析异常发生),我现在正在最后清除值。还是有问题。更改了如下方法:private void 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(repeation.toString());long id=mDbHelper.getWritableDatabase().insert(DBAdapter.DATABASE_NAME,null,values);values.clear();}