SQL select
SQL 查詢資料
public class MainActivity extends AppCompatActivity {
// 資料庫名稱
private final static String db_name = "data.db";
// 資料庫 版本
private final static int version = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SQLhelper helper = new SQLhelper(this, db_name, null, version);
SQLiteDatabase db = helper.getWritableDatabase();
// Cursor 像是一個指標 它是每個Row的集合
// 從表格選取所有的欄位
Cursor cursor = db.rawQuery("select * from user", null);
// 將指標移動到第一行
cursor.moveToFirst();
do {
// 將資料列印出來
Log.e("tag ", "name = " + cursor.getString(cursor.getColumnIndex("name")));
Log.e("tag ", "value = " + cursor.getInt(cursor.getColumnIndex("value")));
} while (cursor.moveToNext());//判斷是否有下一筆資料
// 關閉資料庫
db.close();
setContentView(R.layout.activity_main);
}
}