这是我第一次尝试使用SQLite,但是我收到了一个常见的冲突,但是,我自己无法解决这个问题。以下是问题的原因。
03-10 17:43:21.747: E/AndroidRuntime(11225): Caused by: android.database.sqlite.SQLiteException: near ")": syntax error (code 1): , while compiling: CREATE TABLE IF NOT EXISTS entry ( TEXT PRIMARY KEY, Test TEXT, )
我知道这种方法在某种程度上构造不正确,但我需要帮助弄清楚如何。
private void createEntryTable(SQLiteDatabase db) {
StringBuilder entryTableFields = new StringBuilder();
entryTableFields.append(EntryTable.Cols.COLUMN_ENTRY)
.append(" TEXT PRIMARY KEY, ")
.append(EntryTable.Cols.COLUMN_TEST).append(" TEXT, ");
createTable(db, EntryTable.TABLE_NAME, entryTableFields.toString());
}
以下是我设置SQLite的方法。首先,我的表只有两列。
public class EntryTable {
public static final String TABLE_NAME = "entry";
public static final String PATH = "entry";
public static final int PATH_TOKEN = 2015;
public static final Uri CONTENT_URI = ContentDescriptor.BASE_URI.buildUpon().appendPath(PATH).build();
public static class Cols {
// Table column names
public static final String COLUMN_ENTRY = "Entry";
public static final String COLUMN_TEST = "Test";
}
}
我创建了一个ContentDescriptor类。
public class ContentDescriptor {
public static final String AUTHORITY = "com.package";
public static final Uri BASE_URI = Uri.parse("content://" + AUTHORITY);
public static final UriMatcher URI_MATCHER = buildUriMatcher();
private static UriMatcher buildUriMatcher() {
final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
// add as many tables as you want below
matcher.addURI(AUTHORITY, EntryTable.PATH, EntryTable.PATH_TOKEN);
return matcher;
}
}
我有一个数据库管理器类
public class DatabaseManager {
public static void saveEntry(Context context, String entry) {
try {
ContentValues values = getContentValuesEntryTable(entry);
ContentResolver resolver = context.getContentResolver();
Cursor cursor = resolver.query(EntryTable.CONTENT_URI, null, null, null, null);
if (cursor != null && cursor.getCount() > 0) {
resolver.update(EntryTable.CONTENT_URI, values, null, null);
} else {
resolver.insert(EntryTable.CONTENT_URI, values);
}
cursor.close();
resolver.insert(EntryTable.CONTENT_URI, values);
} catch (Exception e) {
Log.e("TEST", "error: " + e.toString());
e.printStackTrace();
}
}
public static Cursor getEntry(Context context, String entry) {
Cursor cursor;
String sorting = null;
if (TextUtils.isEmpty(entry)) {
cursor = context.getContentResolver().query(EntryTable.CONTENT_URI, null, null, null, sorting);
} else {
cursor = context.getContentResolver().query(EntryTable.CONTENT_URI, null, EntryTable.Cols.COLUMN_ENTRY + " = '" + entry + "'", null, sorting);
}
if (cursor != null) {
cursor.moveToFirst();
}
return cursor;
}
private static ContentValues getContentValuesEntryTable(String entry) {
ContentValues values = new ContentValues();
values.put(EntryTable.Cols.COLUMN_ENTRY, entry);
return values;
}
}
最后我有一个DBHelper类
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String KEY_CREATE_TABLE = "CREATE TABLE IF NOT EXISTS {0} ({1})";
public static final String KEY_DROP_TABLE = "DROP TABLE IF EXISTS {0}";
private static final int CURRENT_DB_VERSION = 1;
private static final String DB_NAME = "qmun.db";
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, CURRENT_DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.d("TEST", "DB Creation :: Going to create db ");
createEntryTable(db);
}
private void createEntryTable(SQLiteDatabase db) {
StringBuilder entryTableFields = new StringBuilder();
entryTableFields.append(EntryTable.Cols.COLUMN_ENTRY)
.append(" TEXT PRIMARY KEY, ")
.append(EntryTable.Cols.COLUMN_TEST).append(" TEXT, ");
createTable(db, EntryTable.TABLE_NAME, entryTableFields.toString());
}
public void dropTable(SQLiteDatabase db, String name) {
String query = MessageFormat.format(DatabaseHelper.KEY_DROP_TABLE, name);
db.execSQL(query);
}
public void createTable(SQLiteDatabase db, String name, String fields) {
String query = MessageFormat.format(DatabaseHelper.KEY_CREATE_TABLE, name, fields);
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
dropTable(db, EntryTable.TABLE_NAME);
onCreate(db);
}
}
从我的MainActivity我试图用
插入数据DatabaseManager.saveEntry(MainActivity.this, "text goes into my db");
先谢谢你的帮助。我正在慢慢了解SQLite。
答案 0 :(得分:2)
答案在你的标题中。
问题是小问题,""在CREATE命令结束时:
CREATE TABLE IF NOT NOT EXISTS条目(TEXT PRIMARY KEY,Test TEXT **,**)
SQLite在结束括号之前明确地不喜欢逗号。
我想,当你完成命令时,你可以解决这个问题 - 例如在任何来自字段的尾随逗号的createTable strip中。
当然在这里删除它会更简单
.append(EntryTable.Cols.COLUMN_TEST).append(" TEXT, ");
只有当你想将createTable用于其他表时,考虑一个通用的解决方案是值得的,调用者不需要考虑逗号......
答案 1 :(得分:2)
( TEXT PRIMARY KEY, Test TEXT, )
看起来你这里有一个额外的逗号?
(从下面的日志中拉出)
Caused by: android.database.sqlite.SQLiteException: near ")": syntax error (code 1): , while compiling: CREATE TABLE IF NOT EXISTS entry ( TEXT PRIMARY KEY, Test TEXT, )