我正在使用SQLiteOpenHelper在Android中设置一个SQLite数据库... 当我执行程序时,LogCat在调用onCreate(SQLiteDatabase db)方法时记录错误:
靠近“表格”:语法错误
这是我的SQL语句:
db.execSQL(
"CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_DESCRIPTION + " TEXT NOT NULL, " +
KEY_REPS + " TEXT NOT NULL, " +
KEY_WEIGHT + " TEXT NOT NULL);");
当我宣布所有变量如DATABASE_TABLE时,这应该可行。 有没有办法看到实际上出了什么问题,或者我的SQL只是错误的?!
感谢任何帮助!
public static final String KEY_ID = "_id"; -> this could be buggy
public static final String KEY_DESCRIPTION = "Description";
public static final String KEY_REPS = "Reps";
public static final String KEY_WEIGHT = "Weight";
private static final String DATABASE_NAME = "Workout";
private static final String DATABASE_TABLE = "Table";
答案 0 :(得分:0)
最快的方法是将SQL创建为字符串。然后,您可以通过Log.i显示SQL(...
我真的需要查看变量的值,首先猜测它不喜欢你的表名。 但是从我所看到的内容来看,它看起来是正确的。
我的样本private static final String DATABASE_CREATE = "create table "
+ MOVES_TABLE + " (" + KEY_ID
+ " integer primary key autoincrement, "
+ KEY_GAME_ID + " INTEGER, "
+ KEY_LANGUAGE_PREF + " INTEGER, "
+ KEY_BOARD + " TEXT, "
+ KEY_POSITION + " TEXT);"
答案 1 :(得分:0)
猜猜:尝试以下方法:
db.execSQL(
"CREATE TABLE '" + DATABASE_TABLE + "' ('" +
KEY_ID + "' INTEGER PRIMARY KEY AUTOINCREMENT, '" +
KEY_DESCRIPTION + "' TEXT NOT NULL, '" +
KEY_REPS + "' TEXT NOT NULL, '" +
KEY_WEIGHT + "' TEXT NOT NULL);");
这用单引号括住所有标识符。如果这些标签中的任何一个包含特殊字符或保留字,那应该可以解决问题。
答案 2 :(得分:0)
我使用辅助方法来执行日志记录
static void logSql (SQLiteDatabase db, String sql, Object... bindArgs) {
if (Sanity.verboseLog) {
Log.i (TAG, sql);
}
db.execSQL (sql, bindArgs);
}
在我访问数据库的任何地方,我都会查看这个Sanity(一个Stroustrup)变量
public static class Sanity {
public static final boolean checked = false;
public static final boolean waitForDebugger = checked;
public static final boolean forceStartupSync = checked;
public static final boolean writeDbToSd = checked;
public static final boolean showDebugActions = checked;
public static final boolean debugLog = checked;
public static final boolean verboseLog = checked;
public static final boolean debugContentProvider = debugLog;