Dialog(Date) & Spinner(Dropdown) 可以被视为字符串吗?如何将其存储到数据库中?我试图解释更多我所面临的情况



>我在数据库中为(对话框(日期创建了一个字符串,并为微调器(下拉列表(创建了一个字符串。 我正在尝试将这两个存储在数据库中。 但它似乎不起作用,因为这些不是字符串。但是在Sqlite中,没有"日期时间"来存储。

从数据库助手

public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Person.db";
public static final String TABLE_NAME = "Person_Table";
public static final String COL_1 = "OID";
public static final String COL_2 = "Date";
public static final String COL_3 = "Description";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME + " (OID INTEGER PRIMARY KEY AUTOINCREMENT," +
Description TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean insertData(String Date, String Description) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2, Date);
contentValues.put(COL_3, Description);
long result = db.insert(TABLE_NAME, null, contentValues);
if (result == -1)
return false;
else
return true;
}
}

点击后 保存 按钮。它应该存储到数据库中。但是我一直点击,它显示"数据未插入"。(对话框(日期和(微调器(下拉列表不能被视为字符串吗?所以它不会存储到数据库中吗?

来自JavaPage

Save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isInserted = myDb.insertData(
Display_date.getText().toString().trim(),
e_Description.getText().toString());
if (isInserted == true)
Toast.makeText(Donation.this, "Data Inserted", Toast.LENGTH_LONG).show();
else
Toast.makeText(Donation.this, "Data not Inserted", Toast.LENGTH_LONG).show();
}
});

这是(对话框(日期代码

Display_date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar cal = Calendar.getInstance();
int day = cal.get(Calendar.DAY_OF_MONTH);
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
DatePickerDialog dialog = new DatePickerDialog(Donation.this,
android.R.style.Theme_Holo_Light_Dialog_MinWidth, DataListener,
year, month, day);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
}
});
DataListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
month = month + 1;
Log.d(TAG, "onDateSet: mm/dd/yyyy: " + month + "/" + day + "/" + year);
String date = month + "/" + day + "/" + year;
Display_date.setText(date);
}
};

这是(微调器(下拉代码

public void dropdown() {
Spinner dropdown = findViewById(R.id.s_Description);
String[] items = new String[]{"Beverage", "food"};
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, items);
dropdown.setAdapter(adapter);
}

您的表格只有 2 列,1 列用于 id,第 2 列用于描述。您正在尝试在 id 列中插入说明,在描述列中插入日期。因此,它将给予例外。

若要更正它,请在数据库中为字符串类型的日期时间再创建 1 列,然后再次尝试插入数据。

请记住在重试之前卸载您当前的应用程序,因为一旦创建的表无法更新,除非您onUpgrade()方法中使用该代码,并且要调用 onUpgrade((,您必须保留版本代码。

你混淆了列名。在你的insertData(String Date, String Description).在该代码中,您的数据库中只有一个 2 列。OIB和COL_2。但是您正在尝试将数据插入COL_3位置。所以这将是一个错误。再添加一个列COL_3。

相关内容

最新更新