0
例如,我應該把SQL字段名的類本身你把數據庫字段名稱放在哪裏?
public class Book {
public static final String TABLE_NAME = "book";
public static final String ID = "id";
public static final String NUM_PAGES = "num_pages";
private long id;
private int numPages;
public Book(long id, int numPages) {
this.id = id;
this.numPages = numPages;
}
//...
}
其中
public class DatabaseHelper extends SQLiteOpenHelper {
//...
public static final query = "select * from " + Book.TABLE_NAME; //for example
//...
}
還是應該等領域走在DatabaseHelper內:
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String BOOK_TABLE_NAME = "book";
public static final String BOOK_ID = "id";
public static final String BOOK_NUM_PAGES = "num_pages";
//...
public static final query = "select * from " + BOOK_TABLE_NAME; //for example
//...
}
大多隻是試圖找出這些名字爲了CRUD功能需要實際去哪裏。
我不會讓常量組成SQL字符串。它幾乎是一個自己動手的JPA或ORM,總是浪費時間,因爲它增加了代碼庫的毛病。也許可以用單獨的XML或其他語言收集SQL片段,但使用常量時,忘記逗號的風險仍然存在問題,即使不是更多。 _完全個人觀點_ –