2012-06-18 4 views
0

데이터베이스 도우미 클래스를 사용하여 데이터베이스 테이블에 삽입하려고합니다.
여기에 삽입 할 개체가 생성됩니다.안드로이드 sqlite 데이터베이스에 오류를 삽입하십시오

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()); 

그리고이 항목 클래스 공용 클래스 항목은 {

course.Now의 getter 및 setter와
//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; 

나는 오류가 발생 내 데이터베이스 도우미의 삽입 방법을 사용하는 말을한다 "그룹"근처에 구문 오류가 있습니다. 삽입 기능이 왜 실패합니까?

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 = 근처 "그룹": 구문 오류

답변

0

"group"을 사용하여 내 테이블의 내 열 이름을 사용하고있었습니다. 분명히이 문제가 있습니다. 나는 "그룹"이 안드로이드 용 SQLite의 예약어라고 추측하고있다. 그래서 누군가가이 문제에 부딪쳤다면 열 이름을 변경해보십시오. 희망이 도움이됩니다.

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

당신은 같은 문자열 작성 :

...integer primary key autoincrement, item_name text not null, groupTEXT, classificationTEXT, useTEXT, actionTEXT, propertiesTEXT... (원문 참조).

문제점을 확인하십시오.

+0

와우, 고마워. 때로는 다른 사람의 눈을 사로 잡는 경우가 있습니다. 도움에 감사드립니다. 이제는 괜찮을 것 같습니다. – foltzy7

관련 문제