0

我正在尝试使用数据库助手类插入到我的数据库表中。
这是我创建要插入的对象的地方。

Item item1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    db = new MyDBAdapter(this);

    db.insertEntry(item1 = new Item("Bathtub", "Bathroom", "Typical", "Clean", "fill, wash", "Round, deep", "Bathroom", "Toilet, Bathroom", R.drawable.ic_launcher));
    Log.i("item", "item: " + item1.toString());

这是 Item 类 public class Item {

//private variables
int    _id;
String _item_name;
String _item_classification;
String _group;
String _use;
String _action;
String _properties;
String _location;
String _association;
int _img_id;

//Empty constructor
public Item(){

}

//constructor
public Item (int _id, String _item_name, String _group, String _item_classification, String _use, String _action, String _properties, String _location, String _association, int _img_id) {
    this._id = _id;
    this._item_name = _item_name;
    this._item_classification = _item_classification;
    this._group = _group;
    this._use = _use;
    this._action = _action;
    this._properties = _properties;
    this._location = _location;
    this._association = _association;
    this._img_id = _img_id;
}

public Item (String _item_name, String _group, String _item_classification, String _use, String _action, String _properties, String _location, String _association, int _img_id) {
    this._item_name = _item_name;
    this._item_classification = _item_classification;
    this._group = _group;
    this._use = _use;
    this._action = _action;
    this._properties = _properties;
    this._location = _location;
    this._association = _association;
    this._img_id = _img_id;

当然,使用 getter 和 setter。现在,当我为我的数据库助手使用 insert 方法时,我收到一个错误,提示“组”附近的语法错误。为什么我的插入失败?

public class MyDBAdapter {
private static final String DATABASE_NAME = "myDatabase.db";
private static final String DATABASE_TABLE = "mainTable";
private static final int DATABASE_VERSION = 1;

//main table columns
public static final String KEY_ITEM              = "item_name";
public static final String KEY_GROUP             = "group";
public static final String ITEM_CLASSIFICATION   = "classification";
public static final String KEY_USE               = "use";
public static final String KEY_ACTION            = "action";
public static final String KEY_PROPERTIES        = "properties";
public static final String KEY_ASSOCIATION       = "association";
public static final String KEY_IMG_ID            = "img_id";

// The index (key) column name for use in where clauses.
public static final String KEY_ID="_id";

// The name and column index of each column in your database.
public static final int NAME_COLUMN = 1;
public static final int GROUP_COLUMN = 2;
public static final int CLASSIFICATION_COLUMN = 3;
public static final int USE_COLUMN = 4;
public static final int ACTION_COLUMN = 5;
public static final int PROPERTIES_COLUMN = 6;
public static final int ASSOCIATION_COLUMN = 7;
public static final int IMG_ID_COLUMN = 8;
// TODO: Create public field for each column in your table.

// SQL Statement to create a new database.
private static final String DATABASE_CREATE = "create table " + 
        DATABASE_TABLE + " (" 
        + KEY_ID + " integer primary key autoincrement, " 
        + KEY_ITEM + " TEXT, " 
        + KEY_GROUP + " TEXT, " 
        + ITEM_CLASSIFICATION + " TEXT, " 
        + KEY_USE + " TEXT, " 
        + KEY_ACTION + " TEXT, " 
        + KEY_PROPERTIES + " TEXT, " 
        + KEY_ASSOCIATION + " TEXT, " 
        + KEY_IMG_ID + " INTEGER);";

// Variable to hold the database instance
private SQLiteDatabase db;
// Context of the application using the database.
private final Context context;
// Database open/upgrade helper
private myDbHelper dbHelper;

public MyDBAdapter(Context _context) {
    context = _context;
    dbHelper = new myDbHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
}

public MyDBAdapter open() throws SQLException {
    try {  
        db = dbHelper.getWritableDatabase();
    } catch (SQLiteException e) {
        db = dbHelper.getReadableDatabase();
    }
    return this;
}

public void close() {
    db.close();
}

public void insertEntry(Item item) {
    // TODO: Create a new ContentValues to represent my row
    // and insert it into the database.
    SQLiteDatabase db = dbHelper.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_ITEM, item.get_item_name());
    values.put(KEY_GROUP, item.get_group());
    values.put(ITEM_CLASSIFICATION, item.get_item_classification());
    values.put(KEY_USE, item.get_use());
    values.put(KEY_ACTION, item.get_action());
    values.put(KEY_PROPERTIES, item.get_properties());
    values.put(KEY_ASSOCIATION, item.get_association());
    values.put(KEY_IMG_ID, item.get_img_id());

    // insert row to table
    try{
        db.insertOrThrow(DATABASE_TABLE, null, values);
    }catch (Exception e){Log.w("insertFail", "insert failed: " + e.toString());}
    db.close();
}

我得到一个语法错误——>返回的sqlite:错误代码= 1,味精=靠近“组”:语法错误

4

3 回答 3

3

您忘记了列名后的空格。改变,

private static final String DATABASE_CREATE = "create table " + 
    DATABASE_TABLE + " (" + KEY_ID + 
    " integer primary key autoincrement, " +
    KEY_ITEM + " text not null, " + KEY_GROUP + "TEXT, " + 
    ITEM_CLASSIFICATION + "TEXT, " + KEY_USE + "TEXT, " + 
    KEY_ACTION + "TEXT, " + KEY_PROPERTIES + "TEXT" + 
    KEY_ASSOCIATION + "TEXT," + KEY_IMG_ID + "INTEGER" +  ");";

private static final String DATABASE_CREATE = "create table " + 
    DATABASE_TABLE + " (" + KEY_ID + 
    " integer primary key autoincrement, " +
    KEY_ITEM + " text not null, " + KEY_GROUP + " TEXT, " + 
    ITEM_CLASSIFICATION + " TEXT, " + KEY_USE + " TEXT, " + 
    KEY_ACTION + " TEXT, " + KEY_PROPERTIES + " TEXT, " + 
    KEY_ASSOCIATION + " TEXT," + KEY_IMG_ID + " INTEGER" +  ");";

TEXT(注意和之前添加的空格INTEGER)。

于 2012-06-18T00:47:40.633 回答
1

您正在创建一个字符串,如:

...integer primary key autoincrement, item_name text not null, groupTEXT, classificationTEXT, useTEXT, actionTEXT, propertiesTEXT...(原文如此)。

看到问题了吗?

于 2012-06-18T00:52:10.477 回答
0

我在表中的列名中使用了“组”。显然这有一个问题。我猜“组”是 SQLite for android 的保留字。因此,如果其他人遇到此问题,请尝试更改列名。希望这会有所帮助。

于 2012-06-20T14:50:00.770 回答