我有一个包含不同表的数据库,直到现在我更新数据库没有问题。但是现在我想在“TABLE_ALERTAS”中添加一个新行,即使我上传了数据库版本“DATABASE_VERSION = 12”总是给出错误说该行不存在。我不知道该尝试更多。
我确定当我打开应用程序时,所有其他表格都会更新。
问题是当我在 TABLE_ALERTAS 中添加行 KEY_CODIALERTA 时。
这是代码:
公共类 DBAdapter {
public static final String KEY_ROWID = "ID";
public static final String KEY_CODIACTIVITAT = "CODIACTIVITAT";
public static final String KEY_ACTIVITAT = "ACTIVITAT";
public static final String KEY_DATA = "DATA";
public static final String KEY_HORES = "HORES";
public static final String KEY_ROWID2 = "ID2";
public static final String KEY_nomprojecte = "NOMPROJECTE";
public static final String KEY_codictivitat = "CODIACTIVITAT";
public static final String KEY_nomctivitat = "NOMACTIVITAT";
public static final String KEY_codiprojecte = "CODIPROJECTE";
// Login table name
private static final String TABLE_LOGIN = "login";
// Login Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_EMAIL = "email";
private static final String KEY_UID = "uid";
private static final String KEY_DNI = "created_at";
// Alert table name
private static final String TABLE_ALERTAS = "alertas";
// Alert Table Columns names
private static final String KEY_TEXTALERTA = "TEXTALERTA";
private static final String KEY_DESCALERTA = "DESCALERTA";
private static final String KEY_NOMACTIVITAT = "NOMACTIVITAT";
private static final String KEY_TITOLPROJECTE = "TITOLPROJECTE";
private static final String KEY_CODIALERTA = "CODIALERTA";
public static final String DATABASE_NAME = "DB_Informacio";
public static final String DATABASE_TABLE = "Informacio";
public static final String DATABASE_TABLE2 = "dadesPerOmplir";
public static final int DATABASE_VERSION = 12;
public static final String TAG = "DBAdapter";
public static final String DATABASE_CREATE = "create table "+DATABASE_TABLE+"("+KEY_ROWID+" integer primary key autoincrement not null, "+
KEY_CODIACTIVITAT+" text not null, "+KEY_ACTIVITAT+" text not null, "+KEY_DATA+" text not null, "+KEY_HORES+" text not null);";
public static final String DATABASE_CREATE2 = "create table "+DATABASE_TABLE2+"("+KEY_ROWID2+" integer primary key autoincrement not null, "+
KEY_nomprojecte+" text not null, "+KEY_codictivitat+" text not null, "+KEY_nomctivitat+" text not null, "+KEY_codiprojecte+" text not null);";
public static final String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_LOGIN + "("
+ KEY_ID + " INTEGER PRIMARY KEY,"
+ KEY_NAME + " TEXT,"
+ KEY_EMAIL + " TEXT UNIQUE," //Es unique pq es el que utilizamos para el login "userName"
+ KEY_UID + " TEXT,"
+ KEY_DNI + " TEXT" + ")";
public static final String CREATE_ALERT_TABLE = "CREATE TABLE " + TABLE_ALERTAS + "("
+ KEY_ID + " INTEGER PRIMARY KEY,"
+ KEY_TEXTALERTA + " TEXT,"
+ KEY_DESCALERTA + " TEXT,"
+ KEY_NOMACTIVITAT + " TEXT,"
+ KEY_TITOLPROJECTE + " TEXT"
+ KEY_CODIALERTA + " TEXT" + ")";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
public static class DatabaseHelper extends SQLiteOpenHelper{
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
try{
db.execSQL(DATABASE_CREATE);
db.execSQL(DATABASE_CREATE2);
db.execSQL(CREATE_LOGIN_TABLE);
db.execSQL(CREATE_ALERT_TABLE);
}
catch(SQLException e)
{
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
Log.w(DBAdapter.TAG, "se ha actualizado la base de datos");
db.execSQL("DROP TABLE IF EXISTS '" + DBAdapter.DATABASE_TABLE + "'");
db.execSQL("DROP TABLE IF EXISTS '" + DBAdapter.DATABASE_TABLE2 + "'");
db.execSQL("DROP TABLE IF EXISTS '" + DBAdapter.TABLE_LOGIN + "'");
db.execSQL("DROP TABLE IF EXISTS '" + DBAdapter.TABLE_ALERTAS + "'");
onCreate(db);
}
}public long insertarAlerta(String txtalerta,String txtdescalerta, String txtnomactivitat, String txtnomprojecte, String intcodialerta)
{
ContentValues valores = new ContentValues();
valores.put(KEY_TEXTALERTA, txtalerta);
valores.put(KEY_DESCALERTA, txtdescalerta);
valores.put(KEY_NOMACTIVITAT, txtnomactivitat);
valores.put(KEY_TITOLPROJECTE, txtnomprojecte);
valores.put(KEY_CODIALERTA, intcodialerta);
return db.insert(TABLE_ALERTAS, null, valores);
}