简体   繁体   English

错误 SQLiteOpenHelper: no such column: year in “SELECT event, time, date, month, year FROM eventstable WHERE month=? 和年=年”

[英]Error SQLiteOpenHelper : no such column: year in “SELECT event, time, date, month, year FROM eventstable WHERE month=? and year=year”

I'm new to android and SQLite and I have a problem with my code that makes the error "no such column: year in "SELECT event, time, date, month, year FROM eventstable WHERE month=?我是 android 和 SQLite 的新手,我的代码有问题,导致错误“没有这样的列:“SELECT event, time, date, month, year FROM eventstable WHERE month=? and year=?" when I'm trying to launch my activity.和年=?”当我试图启动我的活动时。

I'm also new to StackOverflow so don't hesitate to tell me if I did something wrong:)我也是 StackOverflow 的新手,所以如果我做错了什么,请随时告诉我:)

Thanks everyone感谢大家

There is my code of my class and my DBopenHelper有我的 class 和我的 DBopenHelper 的代码

public class CustomCalendarView extends LinearLayout {
TextView previousMonth, currentMonth, followMonth, newEvent;
GridView gridView;
private static final int MAX_CALENDAR_DAYS = 35;
Calendar calendar = Calendar.getInstance(Locale.FRENCH);
Context context;

SimpleDateFormat dateFormat = new SimpleDateFormat("MMM",Locale.FRENCH);
SimpleDateFormat monthFormat = new SimpleDateFormat("MMMM", Locale.FRENCH);
SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy",Locale.FRENCH);
SimpleDateFormat eventDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.FRENCH);

CalendarAdapter myGridAdapter;
AlertDialog alertDialog;
List<Date> dates = new ArrayList<>();
List<Events> eventsList = new ArrayList<>();

DBOpenHelper dbOpenHelper;


public CustomCalendarView (Context context){super(context);}

public  CustomCalendarView (final Context context, @Nullable AttributeSet attrs){
    super(context, attrs);
    this.context = context;
    InitializeLayout();
    SetUpCalendar();

    previousMonth.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            calendar.add(Calendar.MONTH, -1);
            SetUpCalendar();
        }
    });

    followMonth.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            calendar.add(Calendar.MONTH, 1);
            SetUpCalendar();
        }
    });


    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setCancelable(true);
            final View addView = LayoutInflater.from(parent.getContext()).inflate(R.layout.add_newevent_layout, null);
            final EditText EventName = addView.findViewById(R.id.eventname);
            final TextView EventTime = addView.findViewById(R.id.eventtime);
            ImageButton SetTime = addView.findViewById(R.id.seteventtime);
            Button AddEvent = addView.findViewById(R.id.addevent);
            SetTime.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Calendar calendar = Calendar.getInstance();
                    int hours = calendar.get(Calendar.HOUR_OF_DAY);
                    int minutes = calendar.get(Calendar.MINUTE);
                    TimePickerDialog timePickerDialog = new TimePickerDialog(addView.getContext(), R.style.Theme_AppCompat_Dialog
                            , new TimePickerDialog.OnTimeSetListener() {
                        @Override
                        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                            Calendar c = Calendar.getInstance();
                            c.set(Calendar.HOUR_OF_DAY, hourOfDay);
                            c.set(Calendar.MINUTE, minute);
                            c.setTimeZone(TimeZone.getDefault());
                            SimpleDateFormat format = new SimpleDateFormat("K:mm a", Locale.FRENCH);
                            String event_Time = format.format(c.getTime());
                            EventTime.setText(event_Time);
                        }
                    }, hours, minutes, false);
                    timePickerDialog.show();
                }
            });
            final String date = eventDateFormat.format(dates.get(position));
            final String month = monthFormat.format(dates.get(position));
            final String year = yearFormat.format(dates.get(position));

            AddEvent.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    SaveEvent(EventName.getText().toString(), EventTime.getText().toString(), date, month, year);
                    SetUpCalendar();
                    alertDialog.dismiss();
                }
            });
            builder.setView(addView);
            alertDialog = builder.create();
            alertDialog.show();
        }
    });

}

public CustomCalendarView (Context context, @Nullable AttributeSet attrs, int defStyleAttr){
    super(context, attrs, defStyleAttr);
}

private  void SaveEvent(String event, String time, String date, String month, String year){
    dbOpenHelper = new DBOpenHelper(context);
    SQLiteDatabase database = dbOpenHelper.getWritableDatabase();
    dbOpenHelper.SaveEvent(event, time, date, month, year, database);
    dbOpenHelper.close();
    Toast.makeText(context, "Event Saved", Toast.LENGTH_SHORT).show();
}


private void InitializeLayout (){
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.calendar_layout, this);
    followMonth = view.findViewById(R.id.next_month);
    previousMonth = view.findViewById(R.id.previous_month);
    currentMonth = view.findViewById(R.id.current_month);
    gridView = view.findViewById(R.id.gridView);
    newEvent = view.findViewById(R.id.newevent);
}


private void SetUpCalendar(){
    String currentDate = dateFormat.format(calendar.getTime());
    currentMonth.setText(currentDate);
    dates.clear();
    Calendar monthCalendar = (Calendar) calendar.clone();
    monthCalendar.set(Calendar.DAY_OF_MONTH,1);
    int FirstDayofMonth = monthCalendar.get(Calendar.DAY_OF_WEEK) - 1;
    monthCalendar.add(Calendar.DAY_OF_MONTH, -FirstDayofMonth);
    CollectEventsPerMonth(monthFormat.format(calendar.getTime()),yearFormat.format(calendar.getTime()));
    while (dates.size() < MAX_CALENDAR_DAYS){
        dates.add(monthCalendar.getTime());
        monthCalendar.add(Calendar.DAY_OF_MONTH, 1);
    }
    myGridAdapter = new CalendarAdapter(context, dates, calendar, eventsList);
    gridView.setAdapter(myGridAdapter);
}

private void CollectEventsPerMonth (String Month, String Year) {
    eventsList.clear();
    dbOpenHelper = new DBOpenHelper(context);
    SQLiteDatabase database = dbOpenHelper.getReadableDatabase();
    Cursor cursor = dbOpenHelper.ReadEventsperMonth(Month, Year, database);
    while (cursor.moveToNext()){
        String event = cursor.getString(cursor.getColumnIndex(DBStructure.EVENT));
        String time = cursor.getString(cursor.getColumnIndex(DBStructure.TIME));
        String date = cursor.getString(cursor.getColumnIndex(DBStructure.DATE));
        String month = cursor.getString(cursor.getColumnIndex(DBStructure.MONTH));
        String year = cursor.getString(cursor.getColumnIndex(DBStructure.YEAR));
        Events events = new Events(event, time, date, month, year);
        eventsList.add(events);
    }
    cursor.close();
    dbOpenHelper.close();
}

} }

public class DBOpenHelper extends SQLiteOpenHelper {公共 class DBOpenHelper 扩展 SQLiteOpenHelper {

private static final String CREATE_EVENTS_TABLE = "create table " + DBStructure.EVENT_TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, "
        + DBStructure.EVENT + " TEXT, " + DBStructure.TIME + " TEXT, " + DBStructure.DATE + " TEXT, " + DBStructure.MONTH + " TEXT, "
        + DBStructure.YEAR + "TEXT)";
private static final String DROP_EVENTS_TABLE = "DROP TABLE IF EXISTS " + DBStructure.EVENT_TABLE_NAME;



public DBOpenHelper(@Nullable Context context) {
    super(context, DBStructure.DB_NAME, null, DBStructure.DB_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_EVENTS_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL(DROP_EVENTS_TABLE);
    onCreate(db);
}

public void SaveEvent (String event, String time, String date, String month, String year, SQLiteDatabase database){
    ContentValues contentValues = new ContentValues();
    contentValues.put (DBStructure.EVENT, event);
    contentValues.put(DBStructure.TIME,time);
    contentValues.put(DBStructure.MONTH,month);
    contentValues.put(DBStructure.YEAR,year);
    database.insert(DBStructure.EVENT_TABLE_NAME, null, contentValues);
}

public Cursor ReadEvents (String date, SQLiteDatabase database){
    String [] Projections = {DBStructure.EVENT, DBStructure.TIME, DBStructure.DATE, DBStructure.MONTH, DBStructure.YEAR};
    String Selection = DBStructure.DATE +"=?";
    String [] SelectionArgs = {date};
    return database.query(DBStructure.EVENT_TABLE_NAME, Projections, Selection, SelectionArgs, null,null,null);
}

public Cursor ReadEventsperMonth (String month, String year, SQLiteDatabase database){
    String [] Projections = {DBStructure.EVENT, DBStructure.TIME, DBStructure.DATE, DBStructure.MONTH, DBStructure.YEAR};
    String Selection = DBStructure.MONTH +"=? and " + DBStructure.YEAR + "=?";
    String [] SelectionArgs = {month, year};
    return database.query(DBStructure.EVENT_TABLE_NAME, Projections, Selection, SelectionArgs, null,null,null);
}

} }

I guess you need to give a space on the "TEXT" string near the year constant on query that is creating the table, check it out:我想您需要在创建表的查询中的年份常量附近的“TEXT”字符串上留一个空格,检查一下:

private static final String CREATE_EVENTS_TABLE = "create table " + DBStructure.EVENT_TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, "
    + DBStructure.EVENT + " TEXT, " + DBStructure.TIME + " TEXT, " + DBStructure.DATE + " TEXT, " + DBStructure.MONTH + " TEXT, "
    + DBStructure.YEAR + " TEXT)";

Also you can use sqlitebrowser to double check the data base and tables, check it out:您也可以使用 sqlitebrowser 仔细检查数据库和表,检查一下:

View contents of database file in Android Studio 在Android Studio中查看数据库文件内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM