好的,所以,我想创建一个数据库来存储三个表,并在每次应用程序启动时存储数据(表、列和属性值)。我不需要添加 GUI 来添加或删除元组,但是在每次创建时对其进行硬编码是可以的(这是一个学生项目)。我尝试按照“4.教程:使用 SQLite”这个页面,这很棒,除了它确实添加了 GUI,并且只为单个表制作。
正如教程作者在文档前面提到的那样,我为每个表创建了一个类,并且我设置了 MySQLiteHelper 类,如下所示:
...
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);
}
现在,我已经明白我需要创建一个 DataSource 类,如教程中所述,但是我需要为每个表创建一个,还是需要使它适用于所有表,如果是,如何?
第一个stackoverflow问题,请让我知道它是否应该改写,或者它是否过于具体或不够具体。