1

我有一个模型类,我们称之为“历史”:

public class History {

    int _id;
    String _file_name;
    String _full_path;
    String _file_type;

    public History(){
    }

    public History(int id, String _name, String _full_path, String _file_type){
        this._id = id;
        this._file_name = _name;
        this._full_path = _full_path;

 this._file_type = _file_type;

}
public int get_id() {
    return _id;
}
public void set_id(int _id) {
    this._id = _id;
}
public String get_file_name() {
    return _file_name;
}
public void set_file_name(String _file_name) {
    this._file_name = _file_name;
}
public String get_full_path() {
    return _full_path;
}
public void set_full_path(String _full_path) {
    this._full_path = _full_path;
}
public String get_file_type() {
    return _file_type;
}
public void set_file_type(String _file_type) {
    this._file_type = _file_type;
}

}

我想为我的所有模型自动生成一个类似于此的数据库处理程序:

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHandler extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;

    private static final String DATABASE_NAME = "file_explorer";

    private static final String TABLE_HISTORY = "history";

    private static final String KEY_ID = "id";
    private static final String KEY_NAME = "name";
    private static final String KEY_PH_NO = "phone_number";

    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_HISTORY + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
                + KEY_PH_NO + " TEXT" + ")";
        db.execSQL(CREATE_CONTACTS_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_HISTORY);
        onCreate(db);
    }
}

(这不是我模型的处理程序,但你明白了)

这是可能的还是我必须为每个模型手动创建一个数据库处理程序?

我检查了 Eclipse 的自动生成选项,但没有找到我需要的。

4

1 回答 1

2

看看这个 SO 问题:Any good ORM tools for Android development?

虽然我可能会选择ORMLite for Android。使用示例在网站上。

于 2013-01-08T00:46:42.890 回答