无法将选定的项目从微调器保存到数据库

时间:2018-07-29 12:50:30

标签: android sql database spinner

我的数据库有问题。我想将微调器中的选定项目保存到sql数据库中。问题是,我不知道为什么,但是我无法将选定的项目从微调器保存到数据库。我正在使用EditText试用该数据库,一切都很好。

MainActivity:

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
DatabaseHelper db;
Spinner spinner;
Button addData, showData;
TextView saveChosenItemFromSpinner;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    db = new DatabaseHelper(this);

    addData = (Button) findViewById(R.id.addDataBtn);
    showData = (Button) findViewById(R.id.showDataBtn);

    spinner = (Spinner) findViewById(R.id.spinner);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.spinnerDemo, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(this);

    saveChosenItemFromSpinner = (TextView) findViewById(R.id.text);

    AddData();
//        showData();
}




@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
    String text = adapterView.getItemAtPosition(i).toString();
    saveChosenItemFromSpinner.setText(text);
//        Toast.makeText(adapterView.getContext(), text, Toast.LENGTH_SHORT).show();
    AddData();
}

public void AddData() {
    addData.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    boolean isInserted = db.insertData(saveChosenItemFromSpinner.getText().toString() );
                    if (isInserted)
                        Toast.makeText(MainActivity.this, "Data Inserted", Toast.LENGTH_LONG).show();
                    else
                        Toast.makeText(MainActivity.this, "Data not Inserted", Toast.LENGTH_LONG).show();
                }
            }
    );
}

@Override
public void onNothingSelected(AdapterView<?> adapterView) {

}
}

DatabaseHelper类:

public static final String DATABASE_NAME = "TrainingPlan.db";
public static final String TABLE_NAME = "Chest";
public static final String COL_1 = "ID";
public static final String COL_2 = "Upper_section";

public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL("create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,UpperSectionExercises TEXT)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(db);
}

public boolean insertData(String upperSectionExercisesString) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_2, upperSectionExercisesString);
    long result = db.insert(TABLE_NAME, null, contentValues);

    if (result == -1) {
        return false;
    } else
        return true;
}
}

我试图独自寻找解决方案,但我什么也没找到。我可以给我一些提示吗?

2 个答案:

答案 0 :(得分:0)

您使用的键值与列名不同。

使用

contentValues.put("UpperSectionExercises", upperSectionExercisesString);

或更改您的create语句。

答案 1 :(得分:0)

您的表创建查询错误。就像

      db.execSQL("create table " + TABLE_NAME + " (ID INTEGER PRIMARY 
      KEY AUTOINCREMENT, "+ COL_2+" TEXT)");