コードをきれいにするクラスを作成しました。これは単純なものです。完全な ORM を使用したくありません。
package com.xxx.xxx.db;
public class DatabaseTable {
private String TABLE_NAME;
private String[] COLUMN_NAMES;
private String[] COLUMN_TYPES;
private String[] COLUMN_SPECS;
public DatabaseTable(String TABLE_NAME) {
this.TABLE_NAME = TABLE_NAME;
}
public void insertColumn(String COLUMN_NAME, String COLUMN_TYPE, String COLUMN_SPECS) {
if (COLUMN_NAME != null && COLUMN_TYPE != null) {
int actualIndex = this.COLUMN_NAMES.length+1;
this.COLUMN_NAMES[actualIndex] = COLUMN_NAME;
this.COLUMN_TYPES[actualIndex] = COLUMN_TYPE;
if (COLUMN_SPECS != null) {
this.COLUMN_SPECS[actualIndex] = COLUMN_SPECS;
}
}
}
public String getCreateString() {
String textoRetorno = "CREATE TABLE IF NOT EXIST " + this.TABLE_NAME + " (";
for(int i=0;i<=this.COLUMN_NAMES.length;i++) {
textoRetorno = textoRetorno + this.COLUMN_NAMES[i] + " " + this.COLUMN_TYPES[i];
if (this.COLUMN_SPECS[i] != null) {
textoRetorno = textoRetorno + " " + this.COLUMN_SPECS[i];
}
}
textoRetorno = textoRetorno + ");";
return textoRetorno;
}
}
そして、これが私のSQLiteOpenHelperクラスでの使用方法です...
package com.xxx.xxx.db;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.twomadheads.dietcontroller.db.DatabaseTable;
public class DatabaseHelper extends SQLiteOpenHelper {
static final String DB_NAME = "EDC.db";
static final int DB_VERSION = 1;
//TABLES
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
DatabaseTable produtos = new DatabaseTable("Produtos");
produtos.insertColumn("id", "INTEGER", "NOT NULL AUTO_INCREMENT");
produtos.insertColumn("titulo", "TEXT", "NOT NULL");
produtos.insertColumn("barcode", "TEXT", "NULL");
produtos.insertColumn("calorias", "REAL", "NULL");
produtos.insertColumn("carboidratos", "REAL", "NULL");
produtos.insertColumn("proteinas", "REAL", "NULL");
produtos.insertColumn("gorduras totais", "REAL", "NULL");
produtos.insertColumn("gorduras saturadas", "REAL", "NULL");
produtos.insertColumn("fibras", "REAL", "NULL");
produtos.insertColumn("sincronizado", "TEXT", "NULL");
db.execSQL(produtos.getCreateString());
}
}
LogCatにエラーは見られません...だから.. :(