我无法更新SQLite数据库Android

时间:2013-05-10 22:29:16

标签: android sqlite android-sqlite

我有一个包含不同表的数据库,到现在为止我正在更新数据库而没有任何问题。但是现在我想在“TABLE_ALERTAS”中添加一个新行,即使我上传数据库版本“DATABASE_VERSION = 12”也总是给出错误,说明该行不存在。我不知道该怎么做。

我确信当我打开应用程序时,所有其他表都会更新。

问题是我在TABLE_ALERTAS中添加行KEY_CODIALERTA。

以下是代码:

public class 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);
}

1 个答案:

答案 0 :(得分:3)

简单。你错过了一个逗号。

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," // a comma was missing here
        + KEY_CODIALERTA + " TEXT" + ")";

我建议不要在onCreate()中捕获并忽略该异常,因为您会错过应该抛出异常的错误。

catch(SQLException e)
{
    e.printStackTrace();
    throw e;
}