2013-01-25 106 views
0

我試圖把sqldatabase 2列。 當我插入coment都很好,但是當我嘗試讀取listView時我有錯誤。 column '_id' does not exist 有logcat的:android sqlite錯誤

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 

回答

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

} 
0

我想你dnt更新數據庫版本或更新數據庫版本希望你的問題將得到解決。