0

我正在尝试放入 sqldatabase 2 列。当我插入评论时一切都很好,但是当我尝试阅读 listView 时出现错误。column '_id' does not exist 有logcat:

    01-25 12:30:55.787: E/AndroidRuntime(4256): FATAL EXCEPTION: main
01-25 12:30:55.787: E/AndroidRuntime(4256): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.spacebrowser/com.example.spacebrowser.books}: java.lang.IllegalArgumentException: column '_id' does not exist

有Sqliteadapter

    public class SQLiteAbdapter {
public static final String MYDATABASE_NAME = "MY_DATABASES";
public static final String MYDATABASE_TABLE = "MY_TABLE";
public static final int MYDATABASE_VERSION = 1;
public static final String KEY_ID = "_id";
public static final String KEY_CONTENT1 = "Content1";
public static final String KEY_CONTENT2 = "Content2";


private static final String SCRIPT_CREATE_DATABASE =
 "create table " + MYDATABASE_TABLE + " ("
 + KEY_ID + " integer primary key autoincrement, "
 + KEY_CONTENT1 + " text not null, "
 + KEY_CONTENT2 + " text not null);"; 

private SQLiteHelper sqLiteHelper;
private SQLiteDatabase sqLiteDatabase;
private Context context;
public SQLiteAbdapter(Context c){

 context = c;

} 

public SQLiteAbdapter openToRead() throws android.database.SQLException {
 sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
 sqLiteDatabase = sqLiteHelper.getReadableDatabase();
 return this; 

} 

public SQLiteAbdapter openToWrite() throws android.database.SQLException {
 sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
 sqLiteDatabase = sqLiteHelper.getWritableDatabase();
 return this; 

}

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

public long insert(String content1, String content2){

 ContentValues contentValues = new ContentValues();
 contentValues.put(KEY_CONTENT1, content1);
 contentValues.put(KEY_CONTENT2, content2);  


 return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);
} 

public int deleteAll(){
 return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null);
}
public Cursor queueAll(){
 String[] columns = new String[]{KEY_ID, KEY_CONTENT1, KEY_CONTENT2};
 Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns,
 null, null, null, null, null, null);
   return cursor;

} 

 public class SQLiteHelper extends SQLiteOpenHelper {
public SQLiteHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);

}

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(SCRIPT_CREATE_DATABASE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
 // TODO Auto-generated method stub   
    if (oldVersion >= newVersion)
        return;
    db.execSQL("DROP TABLE IF EXISTS " + MYDATABASE_TABLE);
    onCreate(db);
}
    }

  } 

还有books.java simplecursoradapter:

ListView listContent;   
  private SQLiteAbdapter mySQLiteAdapter;
  SimpleCursorAdapter cursorAdapter;
  Cursor cursor;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.books);

    listContent = (ListView)findViewById(R.id.list);
    mySQLiteAdapter = new SQLiteAbdapter(this);
    mySQLiteAdapter.openToRead();        

    cursor = mySQLiteAdapter.queueAll();
    String[] from = new String[]{SQLiteAdapter.KEY_ID, SQLiteAbdapter.KEY_CONTENT1, SQLiteAbdapter.KEY_CONTENT2};
    int[] to = new int[]{R.id.id, R.id.title, R.id.urlss};
    cursorAdapter =
     new SimpleCursorAdapter(this, R.layout.bookmark, cursor, from, to);
    listContent.setAdapter(cursorAdapter);    

我做的这么糟糕,以至于我有这个错误?

column '_id' does not exist
4

2 回答 2

0

您错过了在插入方法中插入 id。所以你会添加

public long insert(String id,String content1, String content2){

  ContentValues contentValues = new ContentValues();
  contentValues.put(KEY_ID, id);
  contentValues.put(KEY_CONTENT1, content1);
   contentValues.put(KEY_CONTENT2, content2);  


 return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);

 } 
于 2013-01-25T12:33:10.987 回答
0

我认为您不需要更新数据库版本或更新数据库版本,希望您的问题能够得到解决。

于 2013-01-25T11:02:52.260 回答