将错误插入android sqlite数据库

时间:2012-06-18 00:45:13

标签: java android sqlite debugging

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

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类     公共类项目{

//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方法为我的数据库助手时,我得到一个错误,即在“group”附近说语法错误。为什么我的插入失败?

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,msg =靠近“group”:语法错误

3 个答案:

答案 0 :(得分: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" +  ");";

(请注意TEXTINTEGER之前添加的空格。

答案 1 :(得分:1)

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

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

看到问题?

答案 2 :(得分:0)

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