在sqlite版本控制中使用常量

时间:2017-01-31 09:17:53

标签: android sqlite

我们发现很多关于sqlite的教程使用常量来管理sqlite文件的方法中的请求......但这是一个好方法吗?我们想象一下,我们将数据库的三列重命名,我们将使用不同名称的三倍相同的常量。

那么最佳做法是什么?对于我自己,我写了这样的版本:

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    if (oldVersion < 4) {
        db.execSQL("ALTER TABLE " + "demandes" + " RENAME TO demandes2");
        db.execSQL("CREATE TABLE " + "demandes" + "(" + "id" + " INTEGER PRIMARY KEY," + "xmlSendLead" + " TEXT," + "statutEnvoieSendLead" + " INTEGER," + "dateEnvoieSendLead" + " DATETIME," + "contactWebId" + " INTEGER," + "xmlSimulation" + " TEXT," + "statutEnvoieSimulation" + " INTEGER," + "dateEnvoieSimulation" + " DATETIME" + ")");
        db.execSQL("INSERT INTO " + "demandes" + " (" + "id" + "," + "xmlSendLead" + "," + "statutEnvoieSendLead" + "," + "dateEnvoieSendLead" + ")" + " SELECT id, xml, statutEnvoie, dateEnvoie" + " FROM demandes2;");
        db.execSQL("DROP TABLE demandes2");
        System.out.println("v4 parsed");
    }
    if (oldVersion < 5) {
        db.execSQL("CREATE TABLE " + "leads" + "(" + "id" + " INTEGER PRIMARY KEY," + "xmlSendLead" + " TEXT," + "statutSendLead" + " INTEGER," + "dateSendLead" + " DATETIME," + "contactWebId" + " INTEGER," + "xmlSimulation" + " TEXT," + "statutSendSimulation" + " INTEGER," + "dateSendSimulation" + " DATETIME" + ")");
        db.execSQL("INSERT INTO " + "leads" + " (" + "id" + "," + "xmlSendLead" + "," + "statutSendLead" + "," + "dateSendLead" + "contactWebId" + "," + "xmlSimulation" + "," + "statutSendSimulation" + "," + "dateSendSimulation" + ")" + " SELECT id, xml, statutEnvoie, dateEnvoie" + " FROM demandes2;");

        db.execSQL("CREATE TABLE " + "leads" + "(" + "id" + " INTEGER PRIMARY KEY," + "prepaymentPenaltyPercentage" + " REAL," + "notaryFeeGrid" + " INTEGER," + "immoRatePercentage" + " REAL," + "immoMinMonthly" + " INTEGER," + "immoMaxMonthly" + " INTEGER," + "fileFeePercentage" + " REAL," + "date" + " DATETIME," + "consoRatePercentage" + " REAL," + "consoMinMonthly" + " INTEGER," + "consoMaxMonthly" + " INTEGER," + "bankFeePercentage" + " REAL)");
    }
}

所以在这里我永远不会在名称之间做错...对于其他方法我使用常量。你怎么看?

其他方法:

private static final int DATABASE_VERSION = 5;
private static final String DATABASE_NAME = "solutis.db";
private static final String KEY_ID = "id";

private static final String TABLE_LEADS = "leads";
private static final String KEY_XML_SENDLEAD = "xmlSendLead";
private static final String KEY_STATUTENVOIE_SENDLEAD = "statutEnvoieSendLead";
private static final String KEY_DATEENVOIE_SENDLEAD = "dateEnvoieSendLead";
private static final String KEY_CONTACTWEBID = "contactWebId";
private static final String KEY_XML_SIMULATION = "xmlSimulation";
private static final String KEY_STATUTENVOIE_SIMULATION = "statutEnvoieSimulation";
private static final String KEY_DATEENVOIE_SIMULATION = "dateEnvoieSimulation";

private static final String TABLE_SIMULATIONPARAMS = "simulationParams";
private static final String KEY_BANKFEEPERCENTAGE = "bankFeePercentage";
private static final String KEY_CONSOMAXMONTHLY = "consoMaxMonthly";
private static final String KEY_CONSOMINMONTHLY = "consoMinMonthly";
private static final String KEY_CONSORATEPERCENTAGE = "consoRatePercentage";
private static final String KEY_FILEFEEPERCENTAGE = "fileFeePercentage";
private static final String KEY_IMMOMAXMONTHLY = "immoMaxMonthly";
private static final String KEY_IMMOMINMONTHLY = "immoMinMonthly";
private static final String KEY_IMMORATEPERCENTAGE = "immoRatePercentage";
private static final String KEY_PREPAYMENTPENALTYPERCENTAGE = "prepaymentPenaltyPercentage";
private static final String KEY_NOTARYFEEGRID = "notaryFeeGrid";
private static final String KEY_DATE = "date";


@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_DEMANDES_TABLE = "CREATE TABLE " + TABLE_LEADS + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_XML_SENDLEAD + " TEXT," + KEY_STATUTENVOIE_SENDLEAD + " INTEGER," + KEY_DATEENVOIE_SENDLEAD + " DATETIME," + KEY_CONTACTWEBID + " INTEGER," + KEY_XML_SIMULATION + " TEXT," + KEY_STATUTENVOIE_SIMULATION + " INTEGER," + KEY_DATEENVOIE_SIMULATION + " DATETIME" + ")";

    db.execSQL(CREATE_DEMANDES_TABLE);
    /*
    String CREATE_DEMANDES_TABLE = "CREATE TABLE " + TABLE_LEADS + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_XML_SENDLEAD + " TEXT," + KEY_STATUTENVOIE_SENDLEAD + " INTEGER," + KEY_DATEENVOIE_SENDLEAD + " DATETIME" + ")";
    db.execSQL(CREATE_DEMANDES_TABLE);
    */
}


public Demande addDemande(Demande demande) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(KEY_XML_SENDLEAD, demande.getXmlSendLead());
    values.put(KEY_STATUTENVOIE_SENDLEAD, demande.getStatutEnvoieSendLead());
    values.put(KEY_DATEENVOIE_SENDLEAD, demande.getDateEnvoieSendLead());
    values.put(KEY_CONTACTWEBID, demande.getContactWebId());
    values.put(KEY_XML_SIMULATION, demande.getXmlSimulation());
    values.put(KEY_STATUTENVOIE_SIMULATION, demande.getStatutEnvoieSimulation());
    values.put(KEY_DATEENVOIE_SIMULATION, demande.getDateEnvoieSimulation());
    //long test = db.insert(TABLE_LEADS, null, values);
    //int test2 = (int) test;
    //demande.setID(test2);
    long insert = db.insert(TABLE_LEADS, null, values);
    int insertDemande = (int) insert;
    demande.setId(insertDemande);
    db.close();
    return demande;
}

1 个答案:

答案 0 :(得分:0)

当您将表/列名直接写入SQL语句时,会增加将其拼错到某处的风险,并且应用程序将在以后爆炸。 如果你使用常量,编译器会抱怨任何拼写错误。

所以一般来说,你应该总是使用常量。

但是,即使使用常量也不能防止意外使用错误的常量。 所以你仍然需要测试你的所有代码。

如果您认为实际的表/列名称比TABLE_THIS和KEY_THAT更具可读性,并且如果您确信您的测试将捕获所有错误,那么您可以决定不使用常量。而这个决定是否是一个好主意是你必须自己找到的。