我创建了数据库并运行它。然后我添加了几行。一切都按预期运行。然后我想跟踪最后一个日期进入表格的时间。所以我在同一个数据库名称中再添加一个表并尝试保存当前日期,但是当我运行它时,找不到表。 到目前为止我所得到的:在MYSQLiteHelper类中
//impotrs...
public class MYSQLiteHelper extends SQLiteOpenHelper {
// TABLE EMPLOYEES
public static final String TABLE_NAME = "employee";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_EMAIL = "email";
public static final String COLUMN_TIME = "saved_date";
//TABLE DATE
public static final String COLUMN_TIME_ID = "date_id";
public static final String TABLE_TIME_NAME = "date";
//DATABASE COMMENTS.DB
private static final String DATABASE_NAME = "commments.db";
private static final int DATABASE_VERSION = 1;
// Database creation sql statement for employee
private static final String DATABASE_CREATE = "create table "
+ TABLE_NAME + "(" + COLUMN_ID + " integer primary key autoincrement, " + COLUMN_NAME + " text not null, " + COLUMN_EMAIL + " text)";
// Database creation sql statement for date
private static final String DATABASE_CREATE_TIME = "create table "
+ TABLE_TIME_NAME + "(" + COLUMN_TIME_ID + " integer primary key autoincrement, " + COLUMN_TIME + " text)";
public MYSQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE_TIME);
database.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(MYSQLiteHelper.class.getName(),"Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_TIME_NAME);
Log.w(MYSQLiteHelper.class.getName(),"Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
名为employee的表正在运行。我可以添加新员工或选择任何查询。但是当我尝试将当前日期插入表名日期时,它有运行时错误" android.database.qslte.SQLiteException:没有这样的表(代码1);,同时编译SETECT date_id,seved_date从日期开始。 我在其他课程中有什么。大多数情况下,我做的就是我为就业做的事情: 在主Activity按钮中:
EmployeeDataSource dataSource = new EmployeeDataSource(this);
// other codes....
public void getCurrentDate(View v){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formatDate = sdf.format(new Date());
txtTop.setText(formatDate);
dataSource.addDate(formatDate);
}
EmployeeDataSource类中的:
private String[] timeColumn = {MYSQLiteHelper.COLUMN_TIME_ID ,MYSQLiteHelper.COLUMN_TIME};
public long addDate(String date){
ContentValues values = new ContentValues();
values.put(MYSQLiteHelper.COLUMN_TIME, date);
return database.insert(MYSQLiteHelper.TABLE_TIME_NAME, null, values);
}
public String getStoredDate(){
ArrayList<String> array = new ArrayList<String>();
Cursor crs = database.query(MYSQLiteHelper.TABLE_TIME_NAME, timeColumn, null, null, null, null, null);
crs.moveToFirst();
while (!crs.isAfterLast()){
array.add(crs.getString(1));
crs.moveToNext();
}
String strData = array.get(array.size() - 1);
return strData;
}
在我看来,我在MYSQLiteHelper中省略或出错了,因为我读到只有当数据库文件不存在但我的数据库文件已经存在时才调用onCreate()方法。那么请有人帮忙。谢谢。
答案 0 :(得分:1)
您需要增加DATABASE_VERSION常量,以便调用onUpgrade
并创建表。