Reputation: 77
I've created a class to make my code cleaner, here it is, its simples, i just don't want to use a full 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;
}
}
And here is how i'm using it at my SQLiteOpenHelper class...
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());
}
}
I can see no error on the LogCat... so.. :(
Upvotes: 0
Views: 174
Reputation: 716
Problem 1:
You are missing letter in your create table statement. It is:
CREATE TABLE IF NOT EXIST
And should be:
CREATE TABLE IF NOT EXISTS
Letter S?
Problem 2: arrays not initialized. Refer to problem 3.
Problem 3: Targeting array element that is beyond array limit. Try code below. It may not work out of box but should give you a clue:
import java.util.ArrayList;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseTable {
private String TABLE_NAME;
private ArrayList<String> COLUMN_NAMES;
private ArrayList<String> COLUMN_TYPES;
private ArrayList<String> COLUMN_SPECS;
public DatabaseTable(String TABLE_NAME) {
this.TABLE_NAME = TABLE_NAME;
COLUMN_NAMES = new ArrayList<String>();
COLUMN_TYPES = new ArrayList<String>();
COLUMN_SPECS = new ArrayList<String>();
}
public void insertColumn(String COLUMN_NAME, String COLUMN_TYPE, String COLUMN_SPECS) {
if (COLUMN_NAME != null && COLUMN_TYPE != null) {
this.COLUMN_NAMES.add(COLUMN_NAME);
this.COLUMN_TYPES.add(COLUMN_TYPE);
this.COLUMN_SPECS.add(COLUMN_SPECS);
}
}
public String getCreateString() {
String textoRetorno = "CREATE TABLE IF NOT EXIST " + this.TABLE_NAME + " (";
for(int i=0;i<=this.COLUMN_NAMES.size();i++) {
textoRetorno = textoRetorno + this.COLUMN_NAMES.get(i) + " " + this.COLUMN_TYPES.get(i);
if (this.COLUMN_SPECS.get(i) != null) {
textoRetorno = textoRetorno + " " + this.COLUMN_SPECS.get(i);
}
}
textoRetorno = textoRetorno + ");";
return textoRetorno;
}
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());
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
}
}
Upvotes: 1