我得到下面的日志猫错误:
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;
}