SQLiteException:接近“”:编译时出现语法错误(代码1)

时间:2013-03-23 18:22:35

标签: android sqlite exception

我在编译时遇到此错误,我不知道为什么,任何人都可以帮助我吗?

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

这是为了创建表

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

发生了什么事?

2 个答案:

答案 0 :(得分:10)

尝试以下代码: -

String selectQuery = "SELECT _id, type, alcohol FROM " + TABLE_BEERS + " WHERE company= ' " + compania+" ' ";
你错过了单引号..

答案 1 :(得分:1)

文本列值必须以单引号传递。试试以下

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