有人可以帮我理解我做错了什么吗?我正在尝试向数据库添加一个事件,并在单击菜单项时将其打印出来。 我有一个SQLite Helper,使用以下方法:
public void addEvent(Event ev){
ContentValues contentValues = new ContentValues();
contentValues.put(DatabaseHelper.TITLE, ev.getTitle());
contentValues.put(DatabaseHelper.START_DATE, ev.getStartTime());
contentValues.put(DatabaseHelper.END_DATE, ev.getEndTime());
contentValues.put(DatabaseHelper.IMAGE_URL, ev.getImageURL());
contentValues.put(DatabaseHelper.URL, ev.getUrl());
contentValues.put(DatabaseHelper.SUBTITLE, ev.getSubtitle());
contentValues.put(DatabaseHelper.DESCRIPTION, ev.getDescription());
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_NAME, null, contentValues);
db.close();
}
这是我创建事件的地方,将其添加到数据库并打印出来:
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getTitle().equals("Sort by name")){
EventsListFragment.sort(-1);
}else if (item.getTitle().equals("Sort by date")) {
EventsListFragment.sort(1);
}else if (item.getTitle().equals("Stored events")){
Event event = new Event("AURORA", "Mon 23.25.2016 at 15:30:00", "Mon 23.25.2016 at 16:30:00", "g", "g", "g", "g");
eventsDB.addEvent(event);
printDatabase();
}
return true;
}
然后我得到了这个我无法理解的错误。
05-24 23:02:01.068 2453-2453/com.example.aurora.eventsapp E/SQLiteLog: (1) near "date": syntax error
05-24 23:02:01.068 2453-2453/com.example.aurora.eventsapp E/SQLiteDatabase: Error inserting Subtitle=g Image=g Description=g Title=AURORA End date=End: Mon 23.25.2016 at 16:30:00 URL=g Start date=Start: Mon 23.25.2016 at 15:30:00
android.database.sqlite.SQLiteException: near "date": syntax error (code 1): , while compiling: INSERT INTO stored_events_table(Subtitle,Image,Description,Title,End date,URL,Start date) VALUES (?,?,?,?,?,?,?)
#################################################################
Error Code : 1 (SQLITE_ERROR)
Caused By : SQL(query) error or missing database.
=================== UPDATE ===========================
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 2;
public static final String DATABASE_NAME = "storedevents.db";
public static final String TABLE_NAME = "stored_events_table";
public static final String ID = "_id";
public static final String TITLE = "Title";
public static final String START_DATE = "StartDate";
public static final String END_DATE = "EndDate";
public static final String IMAGE_URL = "Image";
public static final String URL = "URL";
public static final String SUBTITLE = "Subtitle";
public static final String DESCRIPTION = "Description";
public static final String[] ALL_COLUMNS = new String[] {ID, TITLE, START_DATE, END_DATE, IMAGE_URL, URL, SUBTITLE, DESCRIPTION};
public static final String[] LIST_COLUMNS = new String[] {TITLE, START_DATE, END_DATE, IMAGE_URL, URL, SUBTITLE, DESCRIPTION};
public static String[] stringArray(long id) {
return new String[] {Long.toString(id)};
}
private static final String CREATE_TABLE = "create table " + TABLE_NAME + "(" +
ID + " INTEGER PRIMARY KEY AUTOINCREMENT " +
TITLE + " text not null " +
START_DATE + " text not null " +
END_DATE + " text not null " +
IMAGE_URL + " text " +
URL + " text " +
SUBTITLE + " text " +
DESCRIPTION + " text);";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXIST " + TABLE_NAME);
onCreate(db);
}
//Add new row to the database
public void addEvent(Event ev){
ContentValues contentValues = new ContentValues();
contentValues.put(DatabaseHelper.TITLE, ev.getTitle());
contentValues.put(DatabaseHelper.START_DATE, ev.getStartTime());
contentValues.put(DatabaseHelper.END_DATE, ev.getEndTime());
contentValues.put(DatabaseHelper.IMAGE_URL, ev.getImageURL());
contentValues.put(DatabaseHelper.URL, ev.getUrl());
contentValues.put(DatabaseHelper.SUBTITLE, ev.getSubtitle());
contentValues.put(DatabaseHelper.DESCRIPTION, ev.getDescription());
SQLiteDatabase db = getWritableDatabase();
db.insert(TABLE_NAME, null, contentValues);
db.close();
}
//Delete event from database
public void deleteEvent(String eventTitle){
SQLiteDatabase db = getWritableDatabase();
db.execSQL("DELETE FROM " + TABLE_NAME + "WHERE " + TITLE + "=\"" + eventTitle + "\";" );
}
//Print the database as string
public String databaseToString(){
String dbString="";
SQLiteDatabase db = getWritableDatabase();
String query = "SELECT * FROM " + TABLE_NAME + " WHERE 1";
//points to a location in results
Cursor c = db.rawQuery(query, null);
c.moveToFirst();
while (!c.isAfterLast()){
if(c.getString(c.getColumnIndex("Title")) != null){
dbString += c.getString(c.getColumnIndex("Title"));
dbString += "\n";
}
}
db.close();
return dbString;
}
}
答案 0 :(得分:1)
正如我所看到的,语句中有一个SQL错误
INSERT INTO stored_events_table(Subtitle,Image,Description,Title,End date,URL,Start date)
VALUES ...
列名不能包含空格符号 - 请检查您定义\构造SQL状态的代码\常量。