SQLite 编译时出错:CREATE TABLE IF NOT EXISTS entry (TEXT PRIMARY KEY, Test TEXT, )
SQLite Error while compiling: CREATE TABLE IF NOT EXISTS entry (TEXT PRIMARY KEY, Test TEXT, )
这是我第一次尝试使用 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 的方法。首先,我的 table 只有两列。
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 class。
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;
}
}
我有一个数据库管理器class
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 class
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。
答案就在你的标题中。
问题出在您的 CREATE 命令末尾的小“,”:
CREATE TABLE IF NOT EXISTS entry (TEXT PRIMARY KEY, Test TEXT**,** )
SQLite 明确不喜欢右括号前的逗号。
我想,您可以在完成命令后解决此问题 - 例如在 createTable 中删除字段中的任何尾随逗号。
当然在这里去掉会更简单
.append(EntryTable.Cols.COLUMN_TEST).append(" TEXT, ");
只是,当您也想对其他表使用 createTable 时,考虑一个通用的解决方案可能是值得的,调用者不需要考虑逗号...
( 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, )
这是我第一次尝试使用 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 的方法。首先,我的 table 只有两列。
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 class。
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;
}
}
我有一个数据库管理器class
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 class
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。
答案就在你的标题中。
问题出在您的 CREATE 命令末尾的小“,”:
CREATE TABLE IF NOT EXISTS entry (TEXT PRIMARY KEY, Test TEXT**,** )
SQLite 明确不喜欢右括号前的逗号。
我想,您可以在完成命令后解决此问题 - 例如在 createTable 中删除字段中的任何尾随逗号。
当然在这里去掉会更简单
.append(EntryTable.Cols.COLUMN_TEST).append(" TEXT, ");
只是,当您也想对其他表使用 createTable 时,考虑一个通用的解决方案可能是值得的,调用者不需要考虑逗号...
( 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, )