Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

android - SQLiteLog: (1) near "Produse": syntax error

I Want to add data from another class into my database and I get this errors:

"SQLiteLog: (1) near "Produse": syntax error" and "SQLiteDatabase: Error inserting Pret=102.0 Ingrediente=Ingredient 1; Ingredient 2; Ingredient 3; Ingredient 4; Ingredient 5; Nume Produse=Produsul 15 Poza=poza15.jpg Gramaj=195.0
android.database.sqlite.SQLiteException: near "Produse": syntax error (code 1): , while compiling: INSERT INTO Produse(Pret,Ingrediente,Nume Produse,Poza,Gramaj) VALUES (?,?,?,?,?)"

Here is my code for MyDBHandler class

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();
    }

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should not use whitespaces in your column names.

change

public static String COLUMN_PRODUS = "Nume Produse";

into

public static String COLUMN_PRODUS = "NumeProduse";

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...