使用引用创建SQLite Native时出错

时间:2014-02-21 11:25:31

标签: android database sqlite

我正在尝试在SQLite中使用引用来创建数据库,但是我获得了一些错误。我发布了最后一次。

02-21 12:08:13.630  10102-10102/com.testapp E/Testapp_DBHelper﹕ SELECT * FROM productcategories
02-21 12:08:13.630  10102-10688/com.testapp D//RequestRunner.java:274﹕ 12:08:13.639 Thread-943 It tooks 898 ms to process request CachedSpiceRequest [requestCacheKey=productreq, cacheDuration=-1, spiceRequest=com.testapp.requesters.product.ProductRequester@43aed718].
02-21 12:08:13.735  10102-10102/com.testapp E/SQLiteLog﹕ (1) near "CREATE": syntax error
02-21 12:08:13.740  10102-10102/com.testapp D/AndroidRuntime﹕ Shutting down VM
02-21 12:08:13.740  10102-10102/com.testapp W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41cc0700)
02-21 12:08:16.780  10102-10102/com.testapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
    android.database.sqlite.SQLiteException: near "CREATE": syntax error (code 1): , while compiling: CREATE TABLE parameters(id INTEGER PRIMARY KEY,name TEXT,fk_id_product_category INTEGER, FOREIGN KEY (fk_id_product_category) REFERENCES CREATE TABLE productcategories(id INTEGER,name TEXT,latest_update_date TEXT) (id))
            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1118)
            at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:691)
            at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
            at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
            at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
            at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1794)
            at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1725)
            at com.testapp.helper.DatabaseHelper.onCreate(DatabaseHelper.java:120)
            at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
            at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
            at com.testapp.helper.DatabaseHelper.getAllProductCategories(DatabaseHelper.java:203)
            at com.testapp.ui.HomeActivity.productLoaded(HomeActivity.java:547)

...

这是我的DatabaseHelper类。

public class DatabaseHelper extends SQLiteOpenHelper {

    private Context _context;

    private int version_old = 0; //DB

    // Logcat tag
    private static final String LOG = "Testapp_DBHelper";

    // Database Version
    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "testapp_cache";

    // Table Names
    private static final String TABLE_PRODUCTCATEGORY = "productcategories";
    private static final String TABLE_PARAMETER = "parameters";
    private static final String TABLE_BRAND = "brands";
    private static final String TABLE_MODEL = "models";

    // Trigger Names
    private static final String TRIGGER_PCAT_PARAMS = "fk_pcatid_parameterid";
    private static final String TRIGGER_PCAT_BRANDS = "fk_pcatid_brandid";
    private static final String TRIGGER_BRAND_MODELS = "fk_brandid_modelid";


    // ProductCategory table column names
    private static final String KEY_ID_PCAT = "id";
    private static final String KEY_NAME_PCAT = "name";
    private static final String KEY_LATEST_UPDATE_DATE_PCAT = "latest_update_date";

    // Parameters table common column names
    private static final String KEY_ID_PARAM = "id";
    private static final String KEY_NAME_PARAM = "name";
    private static final String KEY_FK_ID_PRODUCT_CATEGORY_PARAM = "fk_id_product_category";

    // Brand table common column names
    private static final String KEY_ID_BRAND = "id";
    private static final String KEY_NAME_BRAND = "name";
    private static final String KEY_FK_ID_PRODUCT_CATEGORY_BRAND = "fk_id_product_category";

    // Model table common column names
    private static final String KEY_ID_MODEL = "id";
    private static final String KEY_NAME_MODEL = "name";
    private static final String KEY_FK_ID_BRAND_MODEL = "fk_id_brand";

      // Table Create Statements
// ProductCategory table create statement
private static final String CREATE_TABLE_PRODUCTCATEGORY = "CREATE TABLE "
        + TABLE_PRODUCTCATEGORY + " (" + KEY_ID_PCAT + " INTEGER," + KEY_NAME_PCAT
        + " TEXT," + KEY_LATEST_UPDATE_DATE_PCAT + " TEXT" + ")";

// Parameter table create statement
private static final String CREATE_TABLE_PARAMETER = "CREATE TABLE " + TABLE_PARAMETER
        + " (" + KEY_ID_PARAM + " INTEGER PRIMARY KEY," + KEY_NAME_PARAM + " TEXT,"
        + KEY_FK_ID_PRODUCT_CATEGORY_PARAM + " INTEGER, FOREIGN KEY (" + KEY_FK_ID_PRODUCT_CATEGORY_PARAM + ") REFERENCES " + TABLE_PRODUCTCATEGORY + " (" + KEY_ID_PCAT + ")" + ")";

// Brand table create statement
private static final String CREATE_TABLE_BRAND = "CREATE TABLE "
        + TABLE_BRAND + " (" + KEY_ID_BRAND + " INTEGER PRIMARY KEY,"
        + KEY_NAME_BRAND + " TEXT," + KEY_FK_ID_PRODUCT_CATEGORY_BRAND + " INTEGER, FOREIGN KEY (" + KEY_FK_ID_PRODUCT_CATEGORY_BRAND + ") REFERENCES " + TABLE_PRODUCTCATEGORY + " (" + KEY_ID_PCAT + ")" + ")";

// Model table create statement
private static final String CREATE_TABLE_MODEL = "CREATE TABLE "
        + TABLE_MODEL + " (" + KEY_ID_MODEL + " INTEGER PRIMARY KEY,"
        + KEY_NAME_MODEL + " TEXT," + KEY_FK_ID_BRAND_MODEL + " INTEGER, FOREIGN KEY (" + KEY_FK_ID_BRAND_MODEL + ") REFERENCES " + TABLE_BRAND + " (" + KEY_ID_BRAND + ")" + ")";

// Trigger to manage null IDs on ProductCategory on Parameters Table
private static final String CREATE_TRIGGER_PCAT_PARAMS = "CREATE TRIGGER " + TRIGGER_PCAT_PARAMS + " ON " + TABLE_PARAMETER + " FOR EACH ROW BEGIN " +
        "SELECT CASE WHEN ((SELECT " + KEY_ID_PCAT +" FROM "+ TABLE_PRODUCTCATEGORY +" WHERE "+ KEY_ID_PCAT +"=new."+ KEY_FK_ID_PRODUCT_CATEGORY_PARAM +" ) IS NULL)"+
        " THEN RAISE (ABORT,'Foreign Key Violation') END;"+
        "  END;";

// Trigger to manage null IDs on ProductCategory on Brands Table
private static final String CREATE_TRIGGER_PCAT_BRAND = "CREATE TRIGGER " + TRIGGER_PCAT_BRANDS + " ON " + TABLE_BRAND + " FOR EACH ROW BEGIN " +
        "SELECT CASE WHEN ((SELECT " + KEY_ID_PCAT +" FROM "+ TABLE_PRODUCTCATEGORY +" WHERE "+ KEY_ID_PCAT +"=new."+ KEY_FK_ID_PRODUCT_CATEGORY_BRAND +" ) IS NULL)"+
        " THEN RAISE (ABORT,'Foreign Key Violation') END;"+
        "  END;";

// Trigger to manage null IDs on Brands on Model Table
private static final String CREATE_TRIGGER_BRAND_MODEL = "CREATE TRIGGER " + TRIGGER_BRAND_MODELS + " ON " + TABLE_BRAND + " FOR EACH ROW BEGIN " +
        "SELECT CASE WHEN ((SELECT " + KEY_ID_PCAT +" FROM "+ TABLE_PRODUCTCATEGORY +" WHERE "+ KEY_ID_PCAT +"=new."+ KEY_FK_ID_PRODUCT_CATEGORY_BRAND +" ) IS NULL)"+
        " THEN RAISE (ABORT,'Foreign Key Violation') END;"+
        "  END;";

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

    @Override
    public void onCreate(SQLiteDatabase db) {

        // creating required tables
        db.execSQL(CREATE_TABLE_PRODUCTCATEGORY);
        db.execSQL(CREATE_TABLE_PARAMETER);
        db.execSQL(CREATE_TABLE_BRAND);
        db.execSQL(CREATE_TABLE_MODEL);
        db.execSQL(CREATE_TRIGGER_PCAT_PARAMS);
        db.execSQL(CREATE_TRIGGER_BRAND_MODEL);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // on upgrade drop older tables
        db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE_MODEL);
        db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE_BRAND);
        db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE_PARAMETER);
        db.execSQL("DROP TABLE IF EXISTS " + CREATE_TABLE_PRODUCTCATEGORY);

        // create new tables
        onCreate(db);
    }

    @Override
    public void onOpen(SQLiteDatabase db) {
        super.onOpen(db);
        if (!db.isReadOnly()) {
            // Enable foreign key constraints
            db.execSQL("PRAGMA foreign_keys=ON;");
        }
    }

    // ------------------------ "PRODUCTCATEGORY" table methods ----------------//

    /*
     * Creating a productcategory
     */
    public long createProductCategory(Category cat){
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_ID_PCAT, cat.getId());
        values.put(KEY_NAME_PCAT, cat.getName());
        values.put(KEY_LATEST_UPDATE_DATE_PCAT, cat.getCategoryDate().getDate());

        long returned_id = 0;
        if (db != null) {
            returned_id = db.insert(TABLE_PRODUCTCATEGORY, null, values);
        }
        return returned_id;
    }

    /*
     * get single productcategory
     */

    public ProductCategory getProductCategory(long pcat_id) {
        SQLiteDatabase db = this.getReadableDatabase();

        String selectQuery = "SELECT * FROM " + TABLE_PRODUCTCATEGORY + " WHERE " + KEY_ID_PCAT + " = " + pcat_id;

        Log.e(LOG, selectQuery);

        Cursor c = null;
        if (db != null) {
            c = db.rawQuery(selectQuery, null);
        }

        ProductCategory pcat = new ProductCategory();
        if (c != null) {
            c.moveToFirst();
            pcat.setId(c.getInt(c.getColumnIndex(KEY_ID_PCAT)));
            pcat.setName(c.getString(c.getColumnIndex(KEY_NAME_PCAT)));
            pcat.setLatest_update_date(c.getString(c.getColumnIndex(KEY_LATEST_UPDATE_DATE_PCAT)));
            c.close();
        }
        return pcat;
    }

    /**
     * getting all productcategories
     * */
    public List<ProductCategory> getAllProductCategories() {
        List<ProductCategory> pcat_list = new ArrayList<ProductCategory>();
        String selectQuery = "SELECT * FROM " + TABLE_PRODUCTCATEGORY;

        Log.e(LOG, selectQuery);

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor c = null;
        if (db != null) {
            c = db.rawQuery(selectQuery, null);
        }

        // looping through all rows and adding to list
        if (c != null) {
            if (c.moveToFirst()) {
                do {
                    ProductCategory pcat = new ProductCategory();
                    pcat.setId(c.getInt(c.getColumnIndex(KEY_ID_PCAT)));
                    pcat.setName(c.getString(c.getColumnIndex(KEY_NAME_PCAT)));
                    pcat.setLatest_update_date(c.getString(c.getColumnIndex(KEY_LATEST_UPDATE_DATE_PCAT)));

                    // adding to productcategory list
                    pcat_list.add(pcat);
                } while (c.moveToNext());
            }
            c.close();
        }

        return pcat_list;
    }

    /*
     * getting productcategory count
     */
    public int getProductCategoryCount() {
        String countQuery = "SELECT * FROM " + TABLE_PRODUCTCATEGORY;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = null;
        if (db != null) {
            cursor = db.rawQuery(countQuery, null);
        }

        int count = 0;
        if (cursor != null) {
            count = cursor.getCount();
            cursor.close();
        }

        // return count
        return count;
    }

    /*
     * Updating a productcategory
     */
    public int updateProductCategory(ProductCategory pcat) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_ID_PCAT, pcat.getId());
        values.put(KEY_NAME_PCAT, pcat.getName());
        values.put(KEY_LATEST_UPDATE_DATE_PCAT, pcat.getLatest_update_date());

        // updating row
        if (db != null) {
            return db.update(TABLE_PRODUCTCATEGORY, values, KEY_ID_PCAT + " = ?", new String[] { String.valueOf(pcat.getId()) });
        }
        return 0;
    }

    /*
     * Deleting a productcategory
     */
    public void deleteProductCategory(long pcat_id) {
        SQLiteDatabase db = this.getWritableDatabase();
        if (db != null) {
            db.delete(TABLE_PRODUCTCATEGORY, KEY_ID_PCAT + " = ?", new String[] { String.valueOf(pcat_id) });
        }
    }

    // ------------------------ "parameters" table methods ----------------//

    /*
     * Creating parameters
     */
    public long createParameters(Parameters params) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_ID_PARAM, params.getId());
        values.put(KEY_NAME_PARAM, params.getName());
        values.put(KEY_FK_ID_PRODUCT_CATEGORY_PARAM, params.getFk_id_product_category());

        // insert row
        long returned_id = 0;
        if (db != null) {
            returned_id = db.insert(TABLE_PARAMETER, null, values);
        }
        return returned_id;
    }

    /**
     * getting all parameters
     * */
    public List<Parameters> getAllParameters() {
        List<Parameters> params_list = new ArrayList<Parameters>();
        String selectQuery = "SELECT * FROM " + TABLE_PARAMETER;

        Log.e(LOG, selectQuery);

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor c = null;
        if (db != null) {
            c = db.rawQuery(selectQuery, null);
        }

        // looping through all rows and adding to list
        if (c != null) {
            if (c.moveToFirst()) {
                do {
                    Parameters params = new Parameters();
                    params.setId(c.getInt(c.getColumnIndex(KEY_ID_PARAM)));
                    params.setName(c.getString(c.getColumnIndex(KEY_NAME_PARAM)));
                    params.setFk_id_product_category(c.getInt(c.getColumnIndex(KEY_FK_ID_PRODUCT_CATEGORY_PARAM)));

                    // adding to tags list
                    params_list.add(params);
                } while (c.moveToNext());
            }
            c.close();
        }
        return params_list;
    }

    /*
     * Updating a parameters
     */
    public int updateParameters(Parameters params) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_ID_PARAM, params.getId());
        values.put(KEY_NAME_PARAM, params.getName());
        values.put(KEY_FK_ID_PRODUCT_CATEGORY_PARAM, params.getFk_id_product_category());


        // updating row
        if (db != null) {
            return db.update(TABLE_PARAMETER, values, KEY_ID_PARAM + " = ?", new String[] { String.valueOf(params.getId()) });
        }
        return 0;
    }

    /*
     * Deleting a parameter
     */
    public void deleteParameters(long params_id) {
        SQLiteDatabase db = this.getWritableDatabase();
        if (db != null) {
            db.delete(TABLE_PARAMETER, KEY_ID_PARAM + " = ?", new String[] { String.valueOf(params_id) });
        }
    }

    // ------------------------ "BRAND" table methods ----------------//

    /*
     * Creating a brand
     */
    public long createBrand(Brand brand){
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_ID_BRAND, brand.getId());
        values.put(KEY_NAME_BRAND, brand.getName());
        values.put(KEY_FK_ID_PRODUCT_CATEGORY_BRAND, brand.getFk_id_product_category());

        long returned_id = 0;
        if (db != null) {
            returned_id = db.insert(TABLE_BRAND, null, values);
        }
        return returned_id;
    }

    /*
     * get single brand
     */

    public Brand getBrand(long brand_id) {
        SQLiteDatabase db = this.getReadableDatabase();

        String selectQuery = "SELECT * FROM " + TABLE_BRAND + " WHERE " + KEY_ID_BRAND + " = " + brand_id;

        Log.e(LOG, selectQuery);

        Cursor c = null;
        if (db != null) {
            c = db.rawQuery(selectQuery, null);
        }

        Brand brand = new Brand();
        if (c != null) {
            c.moveToFirst();
            brand.setId(c.getInt(c.getColumnIndex(KEY_ID_BRAND)));
            brand.setName(c.getString(c.getColumnIndex(KEY_NAME_BRAND)));
            brand.setFk_id_product_category(c.getInt(c.getColumnIndex(KEY_FK_ID_PRODUCT_CATEGORY_BRAND)));
            c.close();
        }

        return brand;
    }

    /**
     * getting all brand
     * */
    public List<Brand> getAllBrand() {
        List<Brand> brand_list = new ArrayList<Brand>();
        String selectQuery = "SELECT * FROM " + TABLE_BRAND;

        Log.e(LOG, selectQuery);

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor c = null;
        if (db != null) {
            c = db.rawQuery(selectQuery, null);
        }

        // looping through all rows and adding to list
        if (c != null) {
            if (c.moveToFirst()) {
                do {
                    Brand brand = new Brand();
                    brand.setId(c.getInt(c.getColumnIndex(KEY_ID_BRAND)));
                    brand.setName(c.getString(c.getColumnIndex(KEY_NAME_BRAND)));
                    brand.setFk_id_product_category(c.getInt(c.getColumnIndex(KEY_FK_ID_PRODUCT_CATEGORY_BRAND)));

                    // adding to productcategory list
                    brand_list.add(brand);
                } while (c.moveToNext());

            }
            c.close();
        }

        return brand_list;
    }

    /*
     * getting brand count
     */
    public int getBrandCount() {
        String countQuery = "SELECT * FROM " + TABLE_BRAND;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = null;
        if (db != null) {
            cursor = db.rawQuery(countQuery, null);
        }

        int count = 0;
        if (cursor != null) {
            count = cursor.getCount();
            cursor.close();
        }

        // return count
        return count;
    }

    /*
     * Updating a brand
     */
    public int updateBrand(Brand brand) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_ID_BRAND, brand.getId());
        values.put(KEY_NAME_BRAND, brand.getName());
        values.put(KEY_FK_ID_PRODUCT_CATEGORY_BRAND, brand.getFk_id_product_category());

        // updating row
        if (db != null) {
            return db.update(TABLE_BRAND, values, KEY_ID_BRAND + " = ?", new String[] { String.valueOf(brand.getId()) });
        }
        return 0;
    }

    /*
     * Deleting a brand
     */
    public void deleteBrand(long brand_id) {
        SQLiteDatabase db = this.getWritableDatabase();
        if (db != null) {
            db.delete(TABLE_BRAND, KEY_ID_BRAND + " = ?", new String[] { String.valueOf(brand_id) });
        }
    }

    // ------------------------ "MODEL" table methods ----------------//

    /*
     * Creating a model
     */
    public long createModel(Model model){
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_ID_MODEL, model.getId());
        values.put(KEY_NAME_MODEL, model.getName());
        values.put(KEY_FK_ID_BRAND_MODEL, model.getFk_id_brand());

        long returned_id = 0;
        if (db != null) {
            returned_id = db.insert(TABLE_MODEL, null, values);
        }
        return returned_id;
    }

    /*
     * get single model
     */

    public Model getModel(long model_id) {
        SQLiteDatabase db = this.getReadableDatabase();

        String selectQuery = "SELECT * FROM " + TABLE_MODEL + " WHERE " + KEY_ID_MODEL + " = " + model_id;

        Log.e(LOG, selectQuery);

        Cursor c = null;
        if (db != null) {
            c = db.rawQuery(selectQuery, null);
        }

        if (c != null)
            c.moveToFirst();

        Model model = new Model();
        if (c != null) {
            model.setId(c.getInt(c.getColumnIndex(KEY_ID_MODEL)));
            model.setName(c.getString(c.getColumnIndex(KEY_NAME_MODEL)));
            model.setFk_id_brand(c.getInt(c.getColumnIndex(KEY_FK_ID_BRAND_MODEL)));
            c.close();
        }
        return model;
    }

    /**
     * getting all model
     * */
    public List<Model> getAllModel() {
        List<Model> model_list = new ArrayList<Model>();
        String selectQuery = "SELECT * FROM " + TABLE_MODEL;

        Log.e(LOG, selectQuery);

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor c = null;
        if (db != null) {
            c = db.rawQuery(selectQuery, null);
        }

        // looping through all rows and adding to list
        if (c != null) {
            if (c.moveToFirst()) {
                do {
                    Model model = new Model();
                    model.setId(c.getInt(c.getColumnIndex(KEY_ID_MODEL)));
                    model.setName(c.getString(c.getColumnIndex(KEY_NAME_MODEL)));
                    model.setFk_id_brand(c.getInt(c.getColumnIndex(KEY_FK_ID_BRAND_MODEL)));

                    // adding to model list
                    model_list.add(model);
                } while (c.moveToNext());
            }
            c.close();
        }

        return model_list;
    }

    /*
     * getting model count
     */
    public int getModelCount() {
        String countQuery = "SELECT * FROM " + TABLE_MODEL;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = null;
        if (db != null) {
            cursor = db.rawQuery(countQuery, null);
        }

        int count = 0;
        if (cursor != null) {
            count = cursor.getCount();
            cursor.close();
        }

        // return count
        return count;
    }

    /*
     * Updating a model
     */
    public int updateModel(Model model) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_ID_MODEL, model.getId());
        values.put(KEY_NAME_MODEL, model.getName());
        values.put(KEY_FK_ID_BRAND_MODEL, model.getFk_id_brand());

        // updating row
        if (db != null) {
            return db.update(TABLE_MODEL, values, KEY_ID_MODEL + " = ?", new String[] { String.valueOf(model.getId()) });
        }
        return 0;
    }

    /*
     * Deleting a model
     */
    public void deleteModel(long model_id) {
        SQLiteDatabase db = this.getWritableDatabase();
        if (db != null) {
            db.delete(TABLE_MODEL, KEY_ID_MODEL + " = ?", new String[] { String.valueOf(model_id) });
        }
    }

我希望你能给我一些启示。谢谢你的帮助,对不起我的英语。

另一个错误,现在在触发器上:S:

02-21 12:34:25.113    9343-9343/com.testapp E/Testapp_DBHelper﹕ SELECT * FROM productcategories
02-21 12:34:25.143    9343-9343/com.testapp E/SQLiteLog﹕ (1) near "ON": syntax error
02-21 12:34:25.233    9343-9343/com.testapp D/AndroidRuntime﹕ Shutting down VM
02-21 12:34:25.233    9343-9343/com.testapp W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41db8700)
02-21 12:34:28.303    9343-9343/com.testapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
    android.database.sqlite.SQLiteException: near "ON": syntax error (code 1): , while compiling: CREATE TRIGGER fk_pcatid_parameterid ON parameters FOR EACH ROW BEGIN SELECT CASE WHEN ((SELECT id FROM productcategories WHERE id=new.fk_id_product_category ) IS NULL) THEN RAISE (ABORT,'Foreign Key Violation') END;  END;
            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1118)
            at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:691)
            at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
            at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
            at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
            at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1794)
            at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1725)
            at com.testapp.helper.DatabaseHelper.onCreate(DatabaseHelper.java:123)
            at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
            at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
            at com.testapp.helper.DatabaseHelper.getAllProductCategories(DatabaseHelper.java:203)

...

1 个答案:

答案 0 :(得分:1)

数据类型和“KEY_NAME_BRAND ..”之后需要空格

+ TABLE_BRAND + " (" + KEY_ID_BRAND + " INTEGER PRIMARY KEY, "
        + KEY_NAME_BRAND

而不是:

+ TABLE_BRAND + "(" + KEY_ID_BRAND + " INTEGER PRIMARY KEY,"
            + KEY_NAME_BRAND  

对于ref: http://hmkcode.com/android-simple-sqlite-database-tutorial/
Android::SQLite Error: no such table