【问题标题】:SQLiteException: near "" :syntax error (code 1) while compilingSQLiteException:在“”附近:编译时出现语法错误(代码 1)
【发布时间】:2013-03-13 11:54:32
【问题描述】:

我在编译时遇到这个错误,我不知道为什么,谁能帮助我?

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

发生了什么?

【问题讨论】:

  • String 参数必须用引号括起来。 "SELECT _id, type, alcohol FROM " + TABLE_BEERS + " WHERE company = '" + compania + "'";

标签: android sqlite exception


【解决方案1】:

试试下面的代码:-

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

你错过了单引号..

【讨论】:

  • 迟到的评论,但我的问题是为什么 SQLite 无法检测到缺少单引号并将该事实放入错误消息中?我们收到的错误消息似乎是在灯光变暗后创建的。这么多人犯了同样的bug,到时候应该是错误信息的创造者让其沉入其中重新思考的时候了,不是吗?
【解决方案2】:

文本列值必须用单引号传递。尝试关注

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

【讨论】:

    猜你喜欢
    • 2020-05-19
    • 1970-01-01
    • 2015-10-03
    • 1970-01-01
    • 1970-01-01
    • 2015-01-11
    • 1970-01-01
    • 2018-08-02
    • 1970-01-01
    相关资源
    最近更新 更多