0

コードをきれいにするクラスを作成しました。これは単純なものです。完全な 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にエラーは見られません...だから.. :(

4

1 に答える 1

1

問題1:

createtableステートメントに文字がありません。それは:

CREATE TABLE IF NOT EXIST

そして、次のようになります。

CREATE TABLE IF NOT EXISTS

文字S?

問題2:アレイが初期化されていません。問題3を参照してください。

問題3:配列の制限を超えている配列要素をターゲットにしています。以下のコードを試してください。そのままでは機能しない場合がありますが、手がかりが得られるはずです。

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

        }


    }
}
于 2013-02-16T01:21:06.900 に答える