public class MyHelper extends SQLiteOpenHelper {
public static final String TB_NAME = "countrycode";
public static final String ID = "_id";
public static final String COUNTRY = "country";
public static final String CODE = "code";
public MyHelper(Context context, String name,
CursorFactory factory,int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
// 创建表countrycode
db.execSQL("CREATE TABLE IF NOT EXISTS "
+ TB_NAME + " ("
+ ID + " INTEGER PRIMARY KEY,"
+ COUNTRY + " VARCHAR,"
+ CODE + " INTEGER)");
}
@Override
public void onUpgrade(SQLiteDatabase db,
int oldVersion, int newVersion) {
//删除以前的旧表,创建一张新的空表,极端 可以使用alert table来修改表
db.execSQL("DROP TABLE IF EXISTS "+TB_NAME);
onCreate(db);
}
}
public class SQLite2 extends Activity {
public static final String DB_NAME = "code.db";
public static final int VERSION = 1;
MyHelper helper;
SQLiteDatabase db;
Cursor c;
TextView display;
Spinner s;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//初始化数据库辅助对象
helper = new MyHelper(this, DB_NAME, null, VERSION);
//获得可读写的SQLiteDatabase对象
db = helper.getWritableDatabase();
//用insert方法像数据库中插入"中国 86"
ContentValues values = new ContentValues();
values.put(MyHelper.COUNTRY, "中国");
values.put(MyHelper.CODE, 86);
db.insert(MyHelper.TB_NAME,MyHelper.ID, values);
//在SQLite数据库中将所有声明为INTEGER PRIMARY KEY的列自动识别为自增列
db.insert(MyHelper.TB_NAME,MyHelper.ID,null);
values.clear();
values.put(MyHelper.COUNTRY, "意大利");
values.put(MyHelper.CODE, 39);
db.update(MyHelper.TB_NAME, values,MyHelper.ID + " = 2",null);
//使用execSQL方法插入数据"洪都拉斯 504"
db.execSQL("INSERT INTO "
+ MyHelper.TB_NAME + "("
+ MyHelper.COUNTRY + ","
+ MyHelper.CODE + ") VALUES "
+ "('洪都拉斯',504)");
//=====================================
c = db.query(MyHelper.TB_NAME,null,null,null,null,null,
MyHelper.CODE+" DESC");
final int countryIndex = c.getColumnIndexOrThrow(MyHelper.COUNTRY);
final int codeIndex = c.getColumnIndexOrThrow(MyHelper.CODE);
/*for (c.moveToFirst();!(c.isAfterLast());c.moveToNext()) {
String country = c.getString(countryIndex);
int code = c.getInt(codeIndex);
Toast.makeText(this, country+code,
Toast.LENGTH_LONG).show();
}*/
//======================================
s = (Spinner)findViewById(R.id.spinner);
display = (TextView)findViewById(R.id.display);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_spinner_item,
c,
new String[] {MyHelper.COUNTRY},
new int[] {android.R.id.text1});
//设置子控件的布局方式
adapter.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(adapter);
s.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> adapter,View v,
int pos, long id) {
c.moveToPosition(pos);
display.setText(c.getString(codeIndex));
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
@Override
public void onDestroy() {
//程序退出时删除所有行。
db.delete(MyHelper.TB_NAME,null,null);
super.onDestroy();
}
}
Cursor 是每行的集合。使用 moveToFirst() 定位第一行。你必须知道每一列的名称。你必须知道每一列的数据类型。Cursor 是一个随机的数据源。所有的数据都是通过下标取得。
数据库query的结果是返回一个cursor对象,cursor是位于结果集之上的一个游标,可以对结果集进行向前,向后,或随机的访问,而cursor本身是一个接口,提供了对结果集访问的一些方法。根据功能的不同在其子类有着不同的实现。要控制查询时返回的Cursor类型,可以自定义一个继承自CursorFactory类通过实现其newCursor()方法来返回需要的Cursor子类对象,但在CursorFactory传入null的默认情况下,查询操作会返回一个指向第一行数据之前的SQLiteCursor的对象。
在实际的应用编写过程中,更多是通过适配器(Adapter)来将Cursor与适配器控件联系起来。Android为Cursor提供了一个抽象类CursorAdapter,可以方便实现Cursor与适配器的连接。只需要创建一个继承自CursorAdapter的类,实现其bindView()和newView()两个抽象方法,或根据需要重新实现其他方法就可以用此类来构造一个可适配Cursor的适配器
An Adapter object acts as a bridge between an AdapterView and the underlying data for that view. The Adapter provides access to the data items. The Adapter is also responsible for making a View for each item in the data set.