1

我得到下面的日志猫错误:

 11-18 21:37:49.700: E/AndroidRuntime(19122): Caused by: android.database.sqlite.SQLiteException: no such column: Home1: , while compiling: SELECT _id FROM projects WHERE _name like Home1
11-18 21:37:49.700: E/AndroidRuntime(19122):    at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
11-18 21:37:49.700: E/AndroidRuntime(19122):    at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:68)
11-18 21:37:49.700: E/AndroidRuntime(19122):    at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:143)
11-18 21:37:49.700: E/AndroidRuntime(19122):    at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
11-18 21:37:49.700: E/AndroidRuntime(19122):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:127)
11-18 21:37:49.700: E/AndroidRuntime(19122):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:94)
11-18 21:37:49.700: E/AndroidRuntime(19122):    at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:53)
11-18 21:37:49.700: E/AndroidRuntime(19122):    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
11-18 21:37:49.700: E/AndroidRuntime(19122):    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1697)
11-18 21:37:49.700: E/AndroidRuntime(19122):    at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1582)
11-18 21:37:49.700: E/AndroidRuntime(19122):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1538)
11-18 21:37:49.700: E/AndroidRuntime(19122):    at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1618)
11-18 21:37:49.700: E/AndroidRuntime(19122):    at com.example.home_painter.DatabaseHandler.checkName(DatabaseHandler.java:334)
11-18 21:37:49.700: E/AndroidRuntime(19122):    at com.example.home_painter.MainActivity.addProject(MainActivity.java:37)
11-18 21:37:49.700: E/AndroidRuntime(19122):    ... 14 more

这是因为“SELECT _id FROM projects WHERE _name like Home1”的语法不正确吗?我似乎无法弄清楚这个错误的原因。

这是我的 sqldatabaseHelper 文件中的一些代码:变量:

  // table name
private static final String TABLE_PROJECTS = "projects";
private static final String TABLE_IMAGES = "images";
private static final String TABLE_COLOR_MAP = "colorMap";
private static final String TABLE_COLORS_COLLECTION = "collection";

// Project Table columns names
private static final String KEY_ID = "_id"; // Primary, integer
private static final String KEY_NAME = "_name"; // Unique, text

OnCreate 方法:

  public void onCreate(SQLiteDatabase db) {

    // FOREIGN KEYS
    String FOREIGN_KEYS = "PRAGMA foreign_keys = ON;";

    // PROJECTS TABLE
    String CREATE_PROJECTS_TABLE = "CREATE TABLE " + TABLE_PROJECTS 
            + "(" + KEY_ID + " INTEGER NOT NULL, " + KEY_NAME + " TEXT NOT NULL, " +" PRIMARY KEY("
            + KEY_ID + "), UNIQUE( "+ KEY_NAME + "))";

    // IMAGES TABLE
    String CREATE_IMAGES_TABLE = "CREATE TABLE " + TABLE_IMAGES + "(" + KEY_IM_ID + " INTEGER NOT NULL, "
            + IM_URI + " TEXT, PRIMARY KEY(" + KEY_IM_ID+ "))";

    // COLOR MAP TABLE
    String CREATE_COLOR_MAP_TABLE = "CREATE TABLE " + TABLE_COLOR_MAP + "(" + PROJ_COLOR_ID + " INTEGER NOT NULL, "
            + COLLECTION_COLOR_ID + " INTEGER NOT NULL, FOREIGN KEY(" + PROJ_COLOR_ID + ") REFERENCES " 
            + TABLE_PROJECTS + "(" + KEY_ID + "), FOREIGN KEY(" + COLLECTION_COLOR_ID + ") REFERENCES " + TABLE_COLORS_COLLECTION
            + "(" + KEY_ID_COLLECT + "))";

    // COLLECTION COLOR TABLE
    String CREATE_COLORS_COLLECTION = "CREATE TABLE " + TABLE_COLORS_COLLECTION + "("
            + KEY_ID_COLLECT + " INTEGER NOT NULL, " + COLLECTION_HEX + " TEXT NOT NULL, "
            + COLLECTION_NAME + " TEXT NOT NULL, PRIMARY KEY(" + KEY_ID_COLLECT + "), UNIQUE("
            + COLLECTION_NAME + "))";
    /*System.out.println(CREATE_PROJECTS_TABLE);
    System.out.println(CREATE_IMAGES_TABLE);
    System.out.println(CREATE_COLORS_TABLE);*/

    db.execSQL(FOREIGN_KEYS);
    db.execSQL(CREATE_PROJECTS_TABLE);
    db.execSQL(CREATE_IMAGES_TABLE);
    db.execSQL(CREATE_COLORS_COLLECTION);
    db.execSQL(CREATE_COLOR_MAP_TABLE);


}

onUpgrade 方法:

   public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    //Drop older tables if existing
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_PROJECTS);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_IMAGES);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_COLORS_COLLECTION);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_COLOR_MAP);
    // Create tables again
    onCreate(db);
}

我认为的代码与我从 project.java 获取信息以放入数据库的方法相关:

values_PT.put(KEY_NAME, project.getName()); // project name
long project_id = db.insert(TABLE_PROJECTS, null, values_PT); // insert into database and save id for use

checkName 方法的代码:

  public boolean checkName(String name) {

    boolean resultName = false;
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor c = db.query(TABLE_PROJECTS, new String[]{KEY_ID}, KEY_NAME + " like " + name, null, null, null, null);

    if (c != null) {

        resultName = true;
    }

    return resultName;
}
4

3 回答 3

3

您发送到 SQLite 的 SQL 如下所示:

SELECT _id FROM projects WHERE _name like Home1

SQLite 将您解释Home1为列名,因为,一个不带引号的字符串值是 SQL 中的标识符。这就是您的“未知列”错误的来源。你想要这样的东西进入 SQLite:

SELECT _id FROM projects WHERE _name like 'Home1'

或(可能)更好:

SELECT _id FROM projects WHERE _name = 'Home1'

如果您不使用通配符_%通配符,那么 LIKE 通常只是一个过于复杂且昂贵的=.

我们如何解决这个问题?您可以尝试手动引用name,但这在 2012 年很愚蠢,我们有占位符将引用和转义问题推送到它们所属的数据库中。我想你想使用这样的db.query调用:

db.query(TABLE_PROJECTS, new String[]{KEY_ID}, KEY_NAME + "=?", new String[] {name}, null, null, null);

占位符将?替换为name(正确引用和转义)的值。使用占位符还有助于保护您免受 SQL 注入问题的影响,因此这是一个好习惯。

于 2012-11-19T05:58:50.813 回答
0

您使用查询:

"SELECT _id FROM projects WHERE _name like Home1"

但是,在您的表 PROJECTS 中没有 _id 和 _name 列。你有 key_id 和 key_name。因此将查询更改为:

"SELECT key_id FROM projects WHERE key_name like Home1"
于 2012-11-19T03:02:50.120 回答
0

您的查询必须是:

Cursor c = db.query(TABLE_PROJECTS, new String[]{KEY_ID}, KEY_NAME + " like ?",new String[]{"'"+name+"%'"}, null, null, null);
于 2012-11-19T03:31:02.923 回答