gpt4 book ai didi

java - 将错误插入android sqlite数据库

转载 作者:行者123 更新时间:2023-12-01 19:03:50 25 4
gpt4 key购买 nike

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

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

//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;

当然还有 getters 和 setters。现在,当我对数据库助手使用 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”:语法错误

最佳答案

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

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 之前添加的空格)。

关于java - 将错误插入android sqlite数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11075911/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com