android-app/src/fr/gaulupeau/apps/Poche/ArticlesSQLiteOpenHelper.java
2013-11-15 14:32:25 +01:00

64 lines
2.2 KiB
Java

package fr.gaulupeau.apps.Poche;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import static fr.gaulupeau.apps.Poche.Helpers.PREFS_NAME;
import static fr.gaulupeau.apps.Poche.Helpers.zeroUpdate;
public class ArticlesSQLiteOpenHelper extends SQLiteOpenHelper {
public static final int VERSION = 1;
public static final String DB_NAME = "article_db.sqlite";
public static String MY_ID = "my_id";
public static String ARTICLE_TABLE = "article";
public static String ARTICLE_ID = "article_id";
public static String ARTICLE_AUTHOR = "author";
public static String ARTICLE_CONTENT = "content";
public static String ARTICLE_TITLE = "title";
public static String ARTICLE_URL = "url";
public static String ARCHIVE = "archive";
Context c;
public ArticlesSQLiteOpenHelper(Context context) {
super(context, DB_NAME, null, VERSION);
c = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
createTables(db);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.delete(ARTICLE_TABLE, null, null);
SharedPreferences preferences = c.getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("previous_update", zeroUpdate);
editor.commit();
}
protected void createTables(SQLiteDatabase db) {
db.execSQL(
"create table " + ARTICLE_TABLE + " (" +
MY_ID + " integer primary key autoincrement not null, " +
ARTICLE_AUTHOR + " text, " +
ARTICLE_CONTENT + " text, " +
ARTICLE_TITLE + " text, " +
ARTICLE_URL + " text, " +
ARTICLE_ID + " integer, " +
ARCHIVE + " integer," +
"UNIQUE (" + ARTICLE_ID + ")" +
");"
);
}
}