Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将错误插入android sqlite数据库_Java_Android_Sqlite_Debugging - Fatal编程技术网

Java 将错误插入android sqlite数据库

Java 将错误插入android sqlite数据库,java,android,sqlite,debugging,Java,Android,Sqlite,Debugging,我正试图使用database helper类插入到我的数据库表中。 这是我创建要插入的对象的地方 Item item1; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

我正试图使用database helper类插入到我的数据库表中。
这是我创建要插入的对象的地方

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”附近显示语法错误。为什么我的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,msg=near“group”:语法错误

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

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" +  ");";


(请注意在
文本
整数
之前添加的空格)。

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

…整型主键自动递增,项目名称文本不为空,groupTEXT,classificationTEXT,useTEXT,actionTEXT,propertiesTEXT…
(原文如此)


看到问题了吗?

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

…整型主键自动递增,项目名称文本不为空,groupTEXT,classificationTEXT,useTEXT,actionTEXT,propertiesTEXT…
(原文如此)


看到问题了吗?

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

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

哇,非常感谢。有时确实需要另一个人的眼睛才能看到这些东西。我感谢帮助。现在插入的内容似乎很好。哇,非常感谢。有时确实需要另一个人的眼睛我很想抓住那些东西。我很感激你的帮助。现在看起来很好。