Android SQLiteDatabase 插入语法错误
Android SQLiteDatabase Insert Syntax Error
我四处搜索,发现有人有类似的问题,但又不完全相同。我收到 SQLite 语法错误,但我找不到错误的语法。请帮忙!
我在 DatabaseAdapter.java 中创建了一个 SQLite 数据库,见此处:
static class DatabaseHelper extends SQLiteOpenHelper{
private static final String DATABASE_NAME = "mealdatabase";
private static final String TABLE_INGREDIENT = "INGREDIENTS";
private static final int DATABASE_VERSION = 1;
private static final String UID = "_id";
private static final String INGREDIENT_NAME = "Ingredient Name";
private static final String SERVING_UNITS = "Serving Units";
private static final String SPC = "Servings Per Container";
private static final String CREATE_TABLE_INGREDIENT = "CREATE TABLE "+TABLE_INGREDIENT+
" ("+UID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+INGREDIENT_NAME+" VARCHAR(255), "
+SERVING_UNITS+" VARCHAR(255), "+SPC+" VARCHAR(255));";
private static final String DROP_TABLE_INGREDIENT = "DROP TABLE IF EXISTS "+TABLE_INGREDIENT;
private Context context;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context=context;
Message.message(context, "Constructor Called");
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(CREATE_TABLE_INGREDIENT);
Message.message(context, "onCreate Called");
} catch (SQLException e) {
Message.message(context, ""+e);
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
try {
Message.message(context, "onUpgrade Called");
db.execSQL(DROP_TABLE_INGREDIENT);
//context.deleteDatabase(DATABASE_NAME);
onCreate(db);
} catch (SQLException e) {
Message.message(context, ""+e);
}
}
}
我正在尝试将新数据插入现有的 SQLite 数据库。数据库创建时没有错误。但是,当调用我的插入函数时,出现以下语法错误:
03-06 20:44:27.559: E/SQLiteLog(968): (1) near "Per": syntax error
03-06 20:44:27.619: E/SQLiteDatabase(968): Error inserting Servings Per Container=8.0 Serving Units=oz Ingredient Name=Spaghetti Noodle
这里是我调用 NewIngredient.java:
中的插入函数的地方
public void saveIngredientData() {
try {
String ingredient = ingredient_editText.getText().toString();
String servingUnits = spinner.getSelectedItem().toString();
double servPerContainer = Double.parseDouble(SPC_editText.getText().toString());
long id = databaseHelper.insertIngredientData(ingredient, servingUnits, servPerContainer);
if (id < 0)
{
Message.message(this, "Insert was Unsuccessful");
}
else
Message.message(this, "Successfully entered a row");
} catch (NumberFormatException e) {
Message.message(getApplicationContext(), ""+e);
}
这里是包含语法错误的 insertIngredientData 函数,位于 DatabaseAdapter.java:
public long insertIngredientData(String ingredientName, String units, double spc){
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(DatabaseHelper.INGREDIENT_NAME, ingredientName);
contentValues.put(DatabaseHelper.SERVING_UNITS, units);
contentValues.put(DatabaseHelper.SPC, spc);
long id = db.insert(DatabaseHelper.TABLE_INGREDIENT, null, contentValues);
db.close();
return id;
}
您在字段名称中使用了空格。
您最好使用下划线 (_)。即:private static final String SPC = "Servings_Per_Container";
.
或者(如果您真的想坚持使用空格),您必须将字段名称包含在方括号中([ 和 ])。即:private static final String SPC = "[Servings Per Container]";
.
更正 ALL 您的字段名称,而不仅仅是示例中的那个。
我四处搜索,发现有人有类似的问题,但又不完全相同。我收到 SQLite 语法错误,但我找不到错误的语法。请帮忙!
我在 DatabaseAdapter.java 中创建了一个 SQLite 数据库,见此处:
static class DatabaseHelper extends SQLiteOpenHelper{
private static final String DATABASE_NAME = "mealdatabase";
private static final String TABLE_INGREDIENT = "INGREDIENTS";
private static final int DATABASE_VERSION = 1;
private static final String UID = "_id";
private static final String INGREDIENT_NAME = "Ingredient Name";
private static final String SERVING_UNITS = "Serving Units";
private static final String SPC = "Servings Per Container";
private static final String CREATE_TABLE_INGREDIENT = "CREATE TABLE "+TABLE_INGREDIENT+
" ("+UID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+INGREDIENT_NAME+" VARCHAR(255), "
+SERVING_UNITS+" VARCHAR(255), "+SPC+" VARCHAR(255));";
private static final String DROP_TABLE_INGREDIENT = "DROP TABLE IF EXISTS "+TABLE_INGREDIENT;
private Context context;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context=context;
Message.message(context, "Constructor Called");
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(CREATE_TABLE_INGREDIENT);
Message.message(context, "onCreate Called");
} catch (SQLException e) {
Message.message(context, ""+e);
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
try {
Message.message(context, "onUpgrade Called");
db.execSQL(DROP_TABLE_INGREDIENT);
//context.deleteDatabase(DATABASE_NAME);
onCreate(db);
} catch (SQLException e) {
Message.message(context, ""+e);
}
}
}
我正在尝试将新数据插入现有的 SQLite 数据库。数据库创建时没有错误。但是,当调用我的插入函数时,出现以下语法错误:
03-06 20:44:27.559: E/SQLiteLog(968): (1) near "Per": syntax error
03-06 20:44:27.619: E/SQLiteDatabase(968): Error inserting Servings Per Container=8.0 Serving Units=oz Ingredient Name=Spaghetti Noodle
这里是我调用 NewIngredient.java:
中的插入函数的地方public void saveIngredientData() {
try {
String ingredient = ingredient_editText.getText().toString();
String servingUnits = spinner.getSelectedItem().toString();
double servPerContainer = Double.parseDouble(SPC_editText.getText().toString());
long id = databaseHelper.insertIngredientData(ingredient, servingUnits, servPerContainer);
if (id < 0)
{
Message.message(this, "Insert was Unsuccessful");
}
else
Message.message(this, "Successfully entered a row");
} catch (NumberFormatException e) {
Message.message(getApplicationContext(), ""+e);
}
这里是包含语法错误的 insertIngredientData 函数,位于 DatabaseAdapter.java:
public long insertIngredientData(String ingredientName, String units, double spc){
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(DatabaseHelper.INGREDIENT_NAME, ingredientName);
contentValues.put(DatabaseHelper.SERVING_UNITS, units);
contentValues.put(DatabaseHelper.SPC, spc);
long id = db.insert(DatabaseHelper.TABLE_INGREDIENT, null, contentValues);
db.close();
return id;
}
您在字段名称中使用了空格。
您最好使用下划线 (_)。即:private static final String SPC = "Servings_Per_Container";
.
或者(如果您真的想坚持使用空格),您必须将字段名称包含在方括号中([ 和 ])。即:private static final String SPC = "[Servings Per Container]";
.
更正 ALL 您的字段名称,而不仅仅是示例中的那个。