我在下面得到了log cat错误:
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;
}
答案 0 :(得分:3)
您发送到SQLite的SQL如下所示:
SELECT _id FROM projects WHERE _name like Home1
SQLite将您的Home1
解释为列名,因为,不带引号的string-ish值是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注入问题的影响,因此获取它是一种好习惯。
答案 1 :(得分: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"
答案 2 :(得分:0)
您的查询必须是:
Cursor c = db.query(TABLE_PROJECTS, new String[]{KEY_ID}, KEY_NAME + " like ?",new String[]{"'"+name+"%'"}, null, null, null);