为什么SQLite不允许我将数据插入数据库

时间:2014-04-11 16:43:49

标签: android sqlite

基本上,当我运行应用程序时,它说,它无法添加,num1input和num2input到数据库,因为

有错误
entry.createEntry(number_one, number_two, answer);

这是我的MainActivity代码:我也拿出了答案,因为它出现了一个错误,我认为这是因为进行计算的按钮没有给出相同的输出,而且#&# 39;基本上是从XML重命名TextView。

public class MainActivity extends Activity implements OnClickListener {

Button sqlUpdate, sqlView;
EditText sqlNumber1, sqlNumber2, sqlAnswer;

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    sqlUpdate = (Button) findViewById(R.id.bSQLUpdate);
    sqlNumber1 = (EditText)findViewById(R.id.num1input);
    sqlNumber2 = (EditText)findViewById(R.id.num2input);
    sqlAnswer = (EditText)findViewById(R.id.ans1);

    sqlView = (Button) findViewById(R.id.bSQLopenView);
    sqlView.setOnClickListener(this);
    sqlUpdate.setOnClickListener(this);

}

public void onClick(View arg0) {

    switch (arg0.getId()){
    case R.id.bSQLUpdate:
        boolean didItWork = true;
        try{
        String number_one = sqlNumber1.getText().toString();
        String number_two = sqlNumber2.getText().toString();
        String answer = sqlAnswer.getText().toString();

        DBA entry = new DBA(MainActivity.this);

        entry.open();

        entry.createEntry(number_one, number_two);

        entry.close();
        } catch (Exception e) {
            didItWork = false;
            String error = e.toString();
            Dialog d = new Dialog(this);
            d.setTitle("Damn!");
            TextView tv = new TextView(this);
            tv.setText(error);
            d.setContentView(tv);
            d.show();
        }finally{
            if (didItWork){
                Dialog d = new Dialog(this);
                d.setTitle("It Worked");
                TextView tv = new TextView(this);
                tv.setText("Success!");
                d.setContentView(tv);
                d.show();
            }
        }
        break;
    case R.id.bSQLopenView:
        Intent i = new Intent("com.cmpcione.calculator.SQLVIEW");
        startActivity(i);
        break;
    }
}

public void sum(View v) {
    EditText number1input = (EditText)findViewById(R.id.num1input);
    EditText number2input = (EditText)findViewById(R.id.num2input);

    Integer num1=Integer.parseInt(number1input.getText().toString()),num2=Integer.parseInt(number2input.getText().toString());

    Integer ans1=num1+num2;

    TextView answer=(TextView)findViewById(R.id.ans1);

    answer.setText(ans1.toString());

}

DBA类的代码:

public class DBA {

public static final String KEY_ROWID = "_id";
public static final String KEY_NUMBER1 = "num1input";
public static final String KEY_NUMBER2 = "num2input";
public static final String KEY_ANSWER = "ans1";


private static final String DATABASE_NAME = "CalculationsDB";
private static final String DATABASE_TABLE = "Calculation_table";
private static final int DATABASE_VERSION = 2;

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

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) {
        // TODO Auto-generated method stub
        //creates table and rows
        db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +

                KEY_ROWID + " INTEGER PRIMARY KEY, " + 
                KEY_NUMBER1 + " TEXT NOT NULL, " + 
                KEY_NUMBER2 + " TEXT NOT NULL, " +
                KEY_ANSWER + " TEXT NOT NULL);" 
                );
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
        onCreate(db);
    }

}

public DBA(Context c){
    ourContext = c;
}

public DBA open() {
    ourHelper = new DbHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;
}

public void close() {
    ourHelper.close();

}

public long createEntry(String number_one, String number_two) {
    // TODO Auto-generated method stub
    ContentValues cv = new ContentValues();
    cv.put(KEY_NUMBER1, number_one);
    cv.put(KEY_NUMBER2, number_two);
    cv.put(KEY_ANSWER, answer);
    return ourDatabase.insert(DATABASE_TABLE, null, cv);

}

public String getData() {
    // TODO Auto-generated method stub
    String[] columns = new String[]{KEY_ROWID, KEY_NUMBER1, KEY_NUMBER2};
    //using the cursor to read from database
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
    String result = " ";

    int iRow = c.getColumnIndex(KEY_ROWID);
    int iNumber1 = c.getColumnIndex(KEY_NUMBER1);
    int iNumber2 = c.getColumnIndex(KEY_NUMBER2);
    int iAnswer = c.getColumnIndex(KEY_ANSWER);

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
        //it gets the string of our index then it gets the first number and then the last
        result = result + c.getString(iRow) + " " + c.getString(iNumber1) + " " + c.getString(iNumber2) + "\n";
    }
    return null;


}

} logcat的完整列表:它从无法将数字插入数据库开始:

04-11 17:57:48.177: E/AndroidRuntime(278): FATAL EXCEPTION: main
04-11 17:57:48.177: E/AndroidRuntime(278): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cmpcione.calculator/com.cmpcione.calculator.MainActivity}: java.lang.ClassCastException: android.widget.TextView
04-11 17:57:48.177: E/AndroidRuntime(278):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-11 17:57:48.177: E/AndroidRuntime(278):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-11 17:57:48.177: E/AndroidRuntime(278):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-11 17:57:48.177: E/AndroidRuntime(278):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-11 17:57:48.177: E/AndroidRuntime(278):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-11 17:57:48.177: E/AndroidRuntime(278):  at android.os.Looper.loop(Looper.java:123)
04-11 17:57:48.177: E/AndroidRuntime(278):  at android.app.ActivityThread.main(ActivityThread.java:4627)
04-11 17:57:48.177: E/AndroidRuntime(278):  at java.lang.reflect.Method.invokeNative(Native Method)
04-11 17:57:48.177: E/AndroidRuntime(278):  at java.lang.reflect.Method.invoke(Method.java:521)
04-11 17:57:48.177: E/AndroidRuntime(278):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-11 17:57:48.177: E/AndroidRuntime(278):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-11 17:57:48.177: E/AndroidRuntime(278):  at dalvik.system.NativeStart.main(Native Method)
04-11 17:57:48.177: E/AndroidRuntime(278): Caused by: java.lang.ClassCastException: android.widget.TextView
04-11 17:57:48.177: E/AndroidRuntime(278):  at com.cmpcione.calculator.MainActivity.onCreate(MainActivity.java:27)
04-11 17:57:48.177: E/AndroidRuntime(278):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-11 17:57:48.177: E/AndroidRuntime(278):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

1 个答案:

答案 0 :(得分:6)

是因为您的表名为Calculations而不是Calculation_Table

如果您编辑了表定义,请确保增加数据库版本,使其进入onUpgrade方法并重新创建数据库。

您看到的下一个问题constraint failed是由answer列未插入任何值引起的。定义说它是not null

以下是您需要做的事情:

public long createEntry(String number_one, String number_two) {
    // TODO Auto-generated method stub
    ContentValues cv = new ContentValues();
    cv.put(KEY_NUMBER1, number_one);
    cv.put(KEY_NUMBER2, number_two);

    // the answer needs to be added to cv, uncomment the line and figure out the answer.
    int answer = Integer.parseInt(number_one) + Integer.parseInt(number_two);
    cv.put(KEY_ANSWER, answer);
    return ourDatabase.insert(DATABASE_TABLE, null, cv);
}