我目前正在使用Android工作室并尝试在本地DataBase中插入日期(使用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类中,我将此功能插入到DB
中 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);
}
}
感谢您的帮助!
答案 0 :(得分:2)
错误表示该字段" date"表格"考试"中不存在,但我看到你在sql语句中创建它。可能是因为你改变了" CREATE"声明,但在使用旧方案创建表之后从未执行过。将VERSION整数增加到2,然后运行应用程序。它将使用适当的结构重新创建表格。