updated result
main.java:
public class SQLiteActivity extends Activity {
/** Called when the activity is first created. */
MySQLiteHelper myHelper; //資料庫輔助類別物件的引用
TextView tv;
private static final String TABLE_NAME = "dictionary_info";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.textView01);
myHelper = new MySQLiteHelper(this, "my.db", null, 1); //新建資料庫輔助類別物件
insertAndUpdateData(myHelper); //向資料庫中插入和更新資料
String result = queryData(myHelper); //向資料庫中查詢資料
tv.setText("字彙\t 中文\t 難度\n" + result);
}
//方法:向資料庫中的表中插入和更新資料
public void insertAndUpdateData (MySQLiteHelper myHelper) {
SQLiteDatabase db = myHelper.getWritableDatabase();//獲取資料庫物件,設定可以寫入的資料庫
//使用execSQL方法向表中插入第一筆資料
db.execSQL("insert into " + TABLE_NAME + "(voc, chimeaning, level) values('kiwi', '奇異果', 4)");
//使用insert方法向表中插入第二筆資料
ContentValues values = new ContentValues(); //新建ContentValues物件儲存“欄名-欄值”映射
values.put("voc", "cantaloupe");
values.put("chimeaning", "哈密瓜");
values.put("level", 2);
db.insert(TABLE_NAME, "id", values); //呼叫方法插入資料
//使用insert方法向表中插入第三筆資料
values.clear();
values.put("voc", "pomello");
values.put("chimeaning", "柚子");
values.put("level", 5);
db.insert(TABLE_NAME, "id", values); //呼叫方法插入資料
//使用insert方法向表中插入第四筆資料
values.clear();
values.put("voc", "sugar cane");
values.put("chimeaning", "甘蔗");
values.put("level", 5);
db.insert(TABLE_NAME, "id", values); //呼叫方法插入資料
//使用update方法更新表中的資料
values.clear(); //清空ContentValues物件
values.put("voc", "sugar apple");
values.put("chimeaning", "釋迦");
values.put("level", 3);
db.update(TABLE_NAME, values, "level = 5", null); //更新表中level為2的那列資料
db.close(); //關閉SQLiteDatabase物件
} //end of insertAndUpdateData()
//方法:從資料庫中查詢資料
public String queryData(MySQLiteHelper myHelper) {
String result = "";
SQLiteDatabase db = myHelper.getReadableDatabase(); //設定可以讀出
Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null);
int nameIndex = cursor.getColumnIndex("voc"); //獲取voc欄的索引
int chiIndex = cursor.getColumnIndex("chimeaning"); //獲取voc欄的索引
int levelIndex = cursor.getColumnIndex("level");//獲取level欄的索引
for (cursor.moveToFirst(); !(cursor.isAfterLast()); cursor.moveToNext()) { //提取資料
result = result + cursor.getString(nameIndex) + " ";
result = result + cursor.getString(chiIndex) + " ";
result = result + cursor.getInt(levelIndex) + " \n";
}
cursor.close();
db.close();
return result;
}// end of queryData()
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
SQLiteDatabase db = myHelper.getWritableDatabase();
db.delete(TABLE_NAME, "1", null); //刪除hero_info表中的所有資料
super.onDestroy();
}
}
*********************************************
public class MySQLiteHelper extends SQLiteOpenHelper{
public MySQLiteHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("create table if not exists dictionary_info("
+ "id integer primary key,"
+ "voc varchar,"
+ "chimeaning varchar,"
+ "level integer)"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}