Morton
Morton

Reputation: 5770

android SQL IllegalStateException

I try to use EditText to save database in SQLiteOpenHelper.

When i add the values on the function , it throws IllegalStateException on my execSQL function.

I don't have any idea about it, any help will be grateful.

it's my create database code:

@Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        getWritableDatabase().execSQL("CREATE TABLE blood " +
                "(_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
                "bloodvalue VARCHAR , " +
                "eattime VARCHAR, " +
                "blooddate DATETIME NOT NULL)");
    }

it's my add button function:

sendBlood = (Button) findViewById(R.id.sendBlood);
        sendBlood.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String bloodSugar = editBloodSugar.getText().toString();
                String bloodEatTime = editBloodEatTime.getText().toString();
                int bloodDate = Integer.parseInt(editBloodDate.getText().toString());

                ContentValues values = new ContentValues();
                values.put("bloodvalue", bloodSugar);
                values.put("eattime", bloodEatTime);
                values.put("blooddate", bloodDate);
                long id = sqlForBloodSugar.getWritableDatabase().insert("blood", null, values);
                Log.d("ADD", id + "");
            }
        });

Upvotes: 0

Views: 73

Answers (1)

Vüsal
Vüsal

Reputation: 2706

Use sqLiteDatabase.execSQL() instead of getWritableDatabase().execSQL()

Upvotes: 1

Related Questions