【问题标题】:Android insert date to database datePicker errorAndroid将日期插入数据库datePicker错误
【发布时间】:2023-04-01 11:38:01
【问题描述】:

我目前正在使用 Android Studio 并尝试在本地数据库中插入日期(使用 DatePickerFragment),但我不断收到该列不存在的错误。我有点菜鸟,所以请温柔:)

android.database.sqlite.SQLiteException: no such column: date (code 1): , while compiling: SELECT nome, descricao, date FROM exames

我有 DatePickerFragment 类:

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

private Calendar dateTime=Calendar.getInstance();

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the current date as the default date in the picker
    final Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);

    // Create a new instance of DatePickerDialog and return it
    return new DatePickerDialog(getActivity(), this, year, month, day);
}

public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
    // Do something with the date chosen by the user
    TextView et1= (TextView) getActivity().findViewById(R.id.theDateET);
    et1.setText("Year: " + view.getYear() + " Month: " + view.getMonth() + " Day: " + view.getDayOfMonth());




    dateTime.set(year,monthOfYear,dayOfMonth);

    int mYear = year;   // Here you can get day,month and year.
    int mMonth = monthOfYear +1;
    int mDay = dayOfMonth;

    ContentValues values = new ContentValues();

    values.put("date",mDay + mMonth + mYear);
   /* values.put("Month",mMonth);
    values.put("Year", mYear);*/

     }
}

在我的 InsertActivity 中我调用了这个方法:

 public void showDatePickerDialog(View v) {
    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getSupportFragmentManager(), "datePicker");
}

在我的 DataBaseAdapter 类中,我也有这个函数可以插入到数据库中

 public long insertExame(String oNome, String aDescricao, String date) {
    ContentValues values = new ContentValues();
    values.put("nome", oNome);
    values.put("descricao", aDescricao);
    values.put("date", date);
   /* values.put("month", month);
    values.put("day", day);*/

    return database.insert("exames", null, values);
}

编辑: 这是我创建数据库的地方:

public class HelpDataBase01 extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "base-dados.db";
private static final int VERSION = 1;
public HelpDataBase01(Context context){
    super(context, DATABASE_NAME, null, VERSION);
}
@Override
public void onCreate(SQLiteDatabase db){
    db.execSQL("CREATE TABLE exames(_id integer primary key autoincrement, nome varchar(40), descricao varchar(40), date varchar(100))");

}
@Override
public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion){
    db.execSQL("DROP TABLE IF EXISTS Exames");
    onCreate(db);

}

}

感谢您的帮助!

【问题讨论】:

  • 分享您在 DB 中创建考试表的代码
  • 错误表明“exams”表中不存在“date”字段,但我看到您在 sql 语句中创建了它。可能是因为您更改了“CREATE”语句,但在使用旧方案创建表后从未执行过它。将 VERSION 整数增加到 2,然后运行应用程序。它将以正确的结构重新创建表。
  • 感谢@RobertoArtilesAstelarra,它成功了!顺便说一句,我是否使用了正确的日期类型?对我的菜鸟代码有什么建议吗?再次感谢我的朋友!
  • 没问题。我将其发布为答案。关于日期类型结帐这个答案stackoverflow.com/a/7363557/889636 或这个stackoverflow.com/a/13694823/889636 并自己决定在你的情况下最合适的方法是什么

标签: java android sqlite datepicker


【解决方案1】:

错误表明“考试”表中不存在“日期”字段,但我看到您在 sql 语句中创建了它。可能是因为您更改了“CREATE”语句,但在使用旧方案创建表后从未执行过它。将 VERSION 整数增加到 2,然后运行应用程序。它将以正确的结构重新创建表。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-13
    • 2011-04-06
    • 1970-01-01
    相关资源
    最近更新 更多