SQLiteLog:(1)near" Produse":语法错误

时间:2016-03-08 11:52:36

标签: android mysql sqlite

我想将另一个类的数据添加到我的数据库中,我得到了这个错误:

  

" SQLiteLog:(1)near" Produse":语法错误"和#34; SQLiteDatabase:   插入错误Pret = 102.0 Ingrediente =成分1;成分2;   成分3;成分4;成分5; Nume Produse = Produsul 15   Poza = poza15.jpg Gramaj = 195.0
  android.database.sqlite.SQLiteException:near" Produse":语法错误   (代码1):,编译时:INSERT INTO Produse(Pret,Ingrediente,Nume   Produse,Poza,Gramaj)VALUES(?,?,?,?,?)"

这是我的MyDBHandler类的代码

public class MyDBHander extends SQLiteOpenHelper{

    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "produseDB.db";
    public static final String TABLE_PRODUCTS = "Produse";

    public static int COLUMN_ID = 0;
    public static String COLUMN_PRODUS = "Nume Produse";
    public static String COLUMN_INGREDIENTE = "Ingrediente";
    public static String COLUMN_GRAMAJ = "Gramaj";
    public static String COLUMN_PRET = "Pret";
    public static String COLUMN_POZA = "Poza";
    public static SQLiteDatabase db;

    public MyDBHander(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, DATABASE_NAME, factory, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db){
        String CREATE_PRODUCTS_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_PRODUCTS + " ( "
                + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT " + COLUMN_PRODUS + " TEXT, " + COLUMN_INGREDIENTE
                + " TEXT, " + COLUMN_GRAMAJ + " TEXT, " + COLUMN_PRET + " TEXT, " + COLUMN_POZA +" TEXT" + " ) ";
        db.execSQL(CREATE_PRODUCTS_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTS);
        onCreate(db);
    }

    public void addProdus(Produs produs){
        ContentValues values = new ContentValues();

        ArrayList<String> ingredienteprodus;
        ingredienteprodus=produs.getIngrediente();
        String listaingrediente="";
        for (String s : ingredienteprodus)
        {
            listaingrediente += s + "; ";
        }

        values.put(COLUMN_PRODUS,produs.getNume_produs());
        values.put(COLUMN_INGREDIENTE, listaingrediente);
        values.put(COLUMN_GRAMAJ, String.valueOf(produs.getGramaj()));
        values.put(COLUMN_PRET, String.valueOf(produs.getPret()));
        values.put(COLUMN_POZA, produs.getPoza());

        db = getWritableDatabase();

        db.insert(TABLE_PRODUCTS, null, values);
        db.close();
    }

}

2 个答案:

答案 0 :(得分:5)

您不应在列名中使用空格。

变化

public static String COLUMN_PRODUS = "Nume Produse";

public static String COLUMN_PRODUS = "NumeProduse";

答案 1 :(得分:1)

如果你想在sqlite中使用带空格的列,你应该使用``。 而不是:

INSERT INTO table (column, column two) values...

你应该使用:

INSERT INTO table (column, 'column two') values...

创建表时也一样。