[英]SQLite - no such column
我想預填充數據庫,然后簡單地檢索數據。 這是一個時間表(目前)將數據放入三列中。 活動名稱,日期和位置。
我可以成功檢索ID號,即PK和活動名稱,但是當遇到列日期時,應用程序將關閉。
這是我收到的logcat錯誤:
Caused by: android.database.sqlite.SQLiteException: no such column: day (code 1): , while compiling: SELECT _id, activity, day, location FROM TrainingTable
我將包含我的代碼,以查看是否遺漏了一個明顯的錯誤:
public class Training_table {
//setting up rows
public static final String KEY_ROWID = "_id";//creates a row ID variable
public static final String KEY_ACTIVITY = "activity";
public static final String KEY_DAY = "day";
public static final String KEY_LOCATION = "location";
//setting up database as private so only this class can ac
private static final String DATABASE_TABLE = "TrainingTable";
private static final int DATABASE_VERSION = 2;//db version
//setup db and setup subclass and use dbHelper to setup db
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);//passing info into super class
}
//exe SQL code to create a table & three columns
//only time this onCreate method is called is first time we create db
/*@Override
public void onCreate(SQLiteDatabase db) {
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_ACTIVITY + " TEXT NOT NULL, " + KEY_DAY + " TEXT NOT NULL, " +
KEY_LOCATION + " TEXT NOT NULL);"
);//Execute Database and create table
}//onCreate method
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " +DATABASE_TABLE);
onCreate(db);
}
}
public Training_table(Context c){
ourContext = c;
}//constructor (initialising context)
//openMethod & write to it
public Training_table open(){
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
//close SQLiteOpenHelper
public void close(){
ourHelper.close();
}
public void deleteTable(){
ourDatabase.delete(DATABASE_TABLE, null, null);
}
public long newEntry(String theActivity, String theDay, String theLocation) {
// TODO Auto-generated method stub
ContentValues values = new ContentValues();
values.put(KEY_ACTIVITY, theActivity);
values.put(KEY_DAY, theDay);
values.put(KEY_LOCATION, theLocation);
return ourDatabase.insert(DATABASE_TABLE, null, values);
}
public String getData() { //CREATING getData() method
String[] columns = new String[] {KEY_ROWID, KEY_ACTIVITY, KEY_DAY, KEY_LOCATION}; /*Setting up column array,
holding all fields*/
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null); //initialising the cursor
String result = "";
/*Setting up a few int's below, to hold the column location of the activity,
day and location within the database */
int indexActivity = c.getColumnIndex(KEY_ACTIVITY);
int indexDay = c.getColumnIndex(KEY_DAY);
int indexLocation = c.getColumnIndex(KEY_LOCATION);
for (c.moveToLast(); !c.isAfterLast(); c.moveToNext()){
//Getting all values below
result = c.getString(indexActivity);
result = result + " " + c.getString(indexDay) + " " + c.getString(indexLocation);
}
c.close(); //Closing the cursor
return result;
}
}//RaceRating class (DB Class)
這是調用方法的java類:
public class Training_table_view extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.training_table_view);
TextView tv = (TextView)findViewById(R.id.tvTTinfo);//setting up reference
//Button bv = (Button)findViewById(R.id.bViewDetails);
String act = "Swimming";
String day = "Mon";
String loc = "bfast";
Training_table info = new Training_table(this);//create training table
info.open();//open Training table class
info.deleteTable();
//info.insertRecord1();
info.newEntry(act,day,loc);
String data = info.getData();//return string from get data method
info.close();
tv.setText(data);//set text view to the string we got data from
}//onCreate
}//Training_table_view class
Caused by: android.database.sqlite.SQLiteException: no such column: day (code 1): , while compiling: SELECT _id, activity, day, location FROM TrainingTable
您可能在以后的階段添加了列day
(即以前運行的應用程序)
您需要清除應用程序的數據,重新安裝並再次檢查。
打開設備設置>應用程序管理器/管理應用程序> YourAppName。
在這里您將找到清除數據的按鈕,然后將其卸載。
希望這可以幫助。
首先請解釋一下
info.open();//open Training table class
info.deleteTable();
創建表后要刪除表嗎? 為什么呢?
其次,嘗試將KEY_DAY的字段重命名為_day或其他名稱
public static final String KEY_DAY = "_day";
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_ACTIVITY + " TEXT NOT NULL, " +
KEY_DAY + " TEXT NOT NULL, " +
KEY_LOCATION + " TEXT NOT NULL " + ");";
);//Execute Database and create table
還要從oncreate和onupgrade方法中刪除注釋。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.