Ok, so, I want to create a database for storing three tables and keeping the data (tables, columns and attribute values) stored every time the app launches. I do not need to add GUI for adding or deleting tuples, but hard coding it on each creation is ok (it's a student project). I've tried to follow the "4. Tutorial: Using SQLite" this page, which is great, except it does add GUI, and is only made for a single table.
I've created a class for each table, as the tutorial author mentions earlier in the document, and I've set up the MySQLiteHelper class like this:
...
public class BFAppOpenHelper extends SQLiteOpenHelper {
// The 'crags'-table name and columns
public static final String CRAGS_TABLE = "crags";
public static final String CRAG_NAME = "cname";
public static final String CRAG_MINGRADE = "minGrade";
public static final String CRAG_MAXGRADE = "maxGrade";
public static final String CRAG_STEEP = "steep";
public static final String CRAG_AREA = "area";
public static final String CRAG_DESCRIPTION = "description";
public static final String CRAG_APPROACH = "appr_time";
// The 'routes'-table name and columns
public static final String ROUTES_TABLE = "routes";
public static final String ROUTE_NAME = "rname";
public static final String ROUTE_GRADE = "grade";
public static final String ROUTE_HEIGHT = "height";
public static final String ROUTE_STARS = "stars";
public static final String ROUTE_BOLTS = "bolts";
public static final String ROUTE_FIRST = "first";
public static final String ROUTE_NO = "number";
public static final String ROUTE_REBOLTED = "rebolted";
// The 'crag_images'-table name and columns
public static final String CRAG_IMAGES_TABLE = "crag_images";
public static final String CRAG_IMAGE_CRAG_NAME = "cname";
public static final String CRAG_IMAGE_IMAGE = "image";
// Database information
private static final String DATABASE_NAME = "BFApp.db";
private static final int DATABASE_VERSION = 1;
// The SQL-script for creating the 'crags'-table
private static final String CRAGS_TABLE_CREATE = "CREATE TABLE " +
CRAGS_TABLE + " ("
+ CRAG_NAME + " TEXT NOT NULL, "
+ CRAG_MINGRADE + " TEXT NOT NULL,"
+ CRAG_MAXGRADE + " TEXT NOT NULL,"
+ CRAG_STEEP + " NUMBER NOT NULL,"
+ CRAG_AREA + " TEXT NOT NULL,"
+ CRAG_DESCRIPTION + " TEXT,"
+ CRAG_APPROACH + " NUMBER,"
+ " PRIMARY KEY (" + CRAG_NAME + "));";
// The SQL-script for creating the 'routes'-table
private static final String ROUTES_TABLE_CREATE = "CREATE TABLE " +
ROUTES_TABLE + " ("
+ ROUTE_NAME + " TEXT NOT NULL, "
+ ROUTE_GRADE + " TEXT NOT NULL,"
+ ROUTE_HEIGHT + " NUMBER NOT NULL,"
+ ROUTE_STARS + " NUMBER NOT NULL,"
+ ROUTE_BOLTS + " NUMBER NOT NULL,"
+ ROUTE_FIRST + " TEXT,"
+ ROUTE_NO + " NUMBER,"
+ ROUTE_REBOLTED + "TEXT,"
+ " PRIMARY KEY (" + ROUTE_NAME + "));";
// The SQL-script for creating the 'crag_images'-table
private static final String CRAG_IMAGES_TABLE_CREATE = "CREATE TABLE " +
ROUTES_TABLE + " ("
+ CRAG_IMAGE_CRAG_NAME + " TEXT PRIMARY KEY, "
+ CRAG_IMAGE_IMAGE + "TEXT,"
+ " FOREIGN KEY(" + CRAG_IMAGE_CRAG_NAME + ") REFERENCES " + CRAGS_TABLE + "(" + CRAG_NAME + "),"
+ " PRIMARY KEY(" + CRAG_IMAGE_CRAG_NAME + ", " + CRAG_IMAGE_IMAGE + "));";
public BFAppOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(CRAGS_TABLE_CREATE);
database.execSQL(ROUTES_TABLE_CREATE);
database.execSQL(CRAG_IMAGES_TABLE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(BFAppOpenHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + CRAGS_TABLE);
onCreate(db);
}
Now, I've understood that I need to create a DataSource class, as explained in the tutorial, but do I need to create one for each table, or do I need to make it work for all the tabels, and if so, how?
First stackoverflow question, please let me know if it should be rephrased or if it's either too specific or not specific enough.