这是我的代码,我看过其他有同样问题的人,他们通常会错过一个空格或者什么东西,但是我花了一段时间来搜索SQL并找不到它会是什么。在此先感谢,这是我的代码:
public class TimingsDb {
public static final String KEY_ID = "_id";
public static final String KEY_SUBJECT = "subject_name";
public static final String KEY_WEEK = "1";
public static final String KEY_DAY = "day_name";
public static final String KEY_LESSON = "1";
private static final String DATABASE_NAME = "TimingsDB";
private static final String DATABASE_TABLE = "TimingsTable";
private static final int DATABASE_VERSION = 1;
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private static class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}// end Constructor
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_SUBJECT
+ " TEXT, " + KEY_WEEK + " INTEGER, " + KEY_DAY
+ " TEXT, " + KEY_LESSON + " INTEGER);");
}// end onCreate
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS "+DATABASE_TABLE);
onCreate(db);
}//end onUpgrade
}// end class
public TimingsDb(Context c){
ourContext = c;
}
public TimingsDb open() throws SQLException{
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}//end open
public void close(){
ourHelper.close();
}//end close
public long createEntry(String subjectToAdd, int week, String day, int lesson) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_SUBJECT, subjectToAdd);
cv.put(KEY_WEEK, week);
cv.put(KEY_DAY, day);
cv.put(KEY_LESSON, lesson);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}//end createEntry
public String getData() {
// TODO Auto-generated method stub
String[] columns = new String[]{KEY_ID, KEY_SUBJECT, KEY_WEEK, KEY_DAY, KEY_LESSON};
Cursor c = ourDatabase.query(DATABASE_TABLE,columns,null,null,null,null,null);
String result = "";
int iId = c.getColumnIndex(KEY_ID);
int iSubject = c.getColumnIndex(KEY_SUBJECT);
int iWeek = c.getColumnIndex(KEY_WEEK);
int iDay = c.getColumnIndex(KEY_DAY);
int iLesson = c.getColumnIndex(KEY_LESSON);
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
result = result + c.getString(iSubject) + c.getString(iWeek) +
c.getString(iDay) + c.getString(iLesson) + "\n";
}//end for
return result;
}
}
答案 0 :(得分:3)
1
不是有效的列名:
public static final String KEY_WEEK = "1";
public static final String KEY_DAY = "day_name";
public static final String KEY_LESSON = "1";
...
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_SUBJECT
+ " TEXT, " + KEY_WEEK + " INTEGER, " + KEY_DAY
+ " TEXT, " + KEY_LESSON + " INTEGER);");
使用以字母开头的列名称,例如week
和lesson
。