11

I have this error while compiling and I don´t know why, can anyone help me?

public static final String TABLE_BEERS = "cervezas";

// Contacts Table Columns names
public static final String KEY_NAME = "_id";
public static final String KEY_COMPANY = "company";
public static final String KEY_TYPE = "type";
public static final String KEY_ALCOHOL = "alcohol";


public DatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    // TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {

    String query = String.format("CREATE TABLE %s (%s STRING PRIMARY KEY,%s TEXT, %s TEXT, %s TEXT);", 
            TABLE_BEERS, KEY_NAME, KEY_COMPANY,
            KEY_TYPE, KEY_ALCOHOL);

    /*
    String CREATE_BEER_TABLE = "create table " + TABLE_BEERS + "("
            + KEY_NAME + " STRING PRIMARY KEY," 
            + KEY_COMPANY + " TEXT,"
            + KEY_TYPE + " TEXT," 
            + KEY_ALCOHOL + " TEXT )";*/
    db.execSQL(query);

This is for create the table

public List<Cervezas> getCompanyCervezas(String compania){

            List<Cervezas> cervezasList = new ArrayList<Cervezas>();
            // Select All Query
            String selectQuery = "SELECT _id, type, alcohol FROM " + TABLE_BEERS + " WHERE company=" + compania;

            SQLiteDatabase db = this.getReadableDatabase();
            Cursor cursor = db.rawQuery(selectQuery, null);

LogCat

android.database.sqlite.SQLiteException: no such column : Alean (code 1): , while compiling: SELECT _id, type, alcohol FROM cervezas WHERE company=Alean

What is happening?

1
  • 1
    String parameters must be surrounded by quotes. "SELECT _id, type, alcohol FROM " + TABLE_BEERS + " WHERE company = '" + compania + "'"; Commented Mar 23, 2013 at 18:26

2 Answers 2

13

try below code:-

String selectQuery = "SELECT _id, type, alcohol FROM " + TABLE_BEERS + " WHERE company= ' " + compania+" ' ";

you miss single quotes ..

Sign up to request clarification or add additional context in comments.

1 Comment

Late comment, but the question I have is why SQLite is so unable to detect that a single quote is missing and put THAT fact into the error message? The error message we get seems to have been created after the lights have gone down real low. So many people make the same bug, and then it should be time for the creator of the error message to let it sink in and think anew, not?
2

Text column values must pass in single quotes. Try following

String selectQuery = "SELECT _id, type, alcohol FROM " + TABLE_BEERS + " WHERE company= '" + compania +"'";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.