MyDatabaseHelper.java
public class MyDatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME="dbname";
public static final String KEY_ID = "_id";
public static final String KEY_NAME = "name";
public static final String KEY_GENDER = "gender";
public static final String KEY_CATEGORY = "category";
public static final String KEY_DAY = "day";
public static final String KEY_MONTH = "month";
public static final String KEY_YEAR = "year";
public DatabaseDomeny(Context context) {
super(context, DATABASE_NAME2, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE mytable (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, " +
"gender TEXT, category TEXT, day TEXT, month TEXT, year TEXT);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
android.util.Log.w("mytable", "Upgrading database, which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS mytable");
onCreate(db);
}
}
MyActivity.java
public class MyActivity extends ListActivity {
private SQLiteDatabase db=null;
private Cursor constantsCursor=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
db=(new MyDatabaseHelper(this)).getWritableDatabase();
list_it();
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
}
private void list_it() {
constantsCursor=db.rawQuery("SELECT * FROM mytable WHERE _id > 0 ORDER BY day, month ",
null);
ListAdapter adapter=new SimpleAdapter(this,
R.layout.mylistxml, constantsCursor,
new String[] {"name", "gender", "category", "day", "month", "year"},
new int[] {R.id.name, R.id.gender, R.id.category, R.id.day, R.id.month, R.id.year});
setListAdapter(adapter);
}
@Override
public void onDestroy() {
super.onDestroy();
constantsCursor.close();
db.close();
}
}
所以它與Android類,查找_id兼容,你應該命名爲id字段_id。 – danny117 2014-11-04 18:43:52
@ danny17這不是必要的。您始終可以使用別名。 – 2014-11-04 19:00:36
'SELECT * FROM myTable ORDER BY day,month'(我會換一個月一天) – 2014-11-04 19:01:41