Android數據庫的操作方法多樣化,掌握這些應用技巧對於我們的實際開發起着非常重要的作用。我們在這裏就爲大家介紹了一下相關操作技巧。
在Android 手機操作系統進行實際開發中,進場會應用到數據庫。而且在這一平臺中對數據庫的應用方法比較簡單靈活。我們在這裏就爲大家詳細介紹了相關方法,希望可以給大家帶來一些幫助。
昨天進行了GUI界面設計,感受了一下android初次設計的愉悅,今天接着學習其SQLite數據庫試用,將昨天的例子中數據存到數庫中,並讀取查看一下。 具體看代碼(原寫的有點問題,再改寫如下):
1) Android數據庫之庫操作類:
- package com.topsun;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.util.Log;
- public class DBHelper {
- private static final String TAG = "UserDB_DBHelper.java";
- private static final String DataBaseName = "UserDB";
- SQLiteDatabase db;
- Context context;
- public DBHelper(Context context) {
- this.open(context);
- }
- private void createTabel() {
- // TODO Auto-generated method stub
- String sql = "";
- try {
- sql = "CREATE TABLE IF NOT EXISTS TestUser (ID INTEGER
PRIMARY KEY autoincrement, NAME TEXT, SEX TEXT, AGES INTEGER)"; - this.db.execSQL(sql);
- Log.v(TAG, "Create Table TestUser ok");
- } catch (Exception e) {
- Log.v(TAG, "Create Table TestUser fail");
- } finally {
- //this.db.close();
- Log.v(TAG, "Create Table TestUser ");
- }
- }
- public boolean save(String name, String sex, Integer ages) {
- String sql = "insert into TestUser values
(null,'" + name + "','" + sex - + "'," + ages + ")";
- try {
- this.db.execSQL(sql);
- Log.v(TAG, "insert Table TestUser 1 record ok");
- return true;
- } catch (Exception e) {
- Log.v(TAG, "insert Table TestUser 1 record fail");
- return false;
- } finally {
- //this.db.close();
- Log.v(TAG, "insert Table TestUser ");
- }
- }
- public Cursor loadAll() {
- Cursor cur = db.query("TestUser", new String[]
{ "ID", "NAME","SEX","AGES"}, null, - null, null, null, null);
- return cur;
- }
- public void open(Context context){
- if (null == db || !this.db.isOpen()){
- this.context = context;
- this.db = context.openOrCreateDatabase(this.DataBaseName,
- context.MODE_PRIVATE, null);
- createTabel();
- Log.v(this.TAG, "create or Open DataBase。。。");
- }
- }
- public void close() {
- db.close();
- }
- }
- package com.topsun;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.util.Log;
- public class DBHelper {
- private static final String TAG = "UserDB_DBHelper.java";
- private static final String DataBaseName = "UserDB";
- SQLiteDatabase db;
- Context context;
- public DBHelper(Context context) {
- this.open(context);
- }
- private void createTabel() {
- // TODO Auto-generated method stub
- String sql = "";
- try {
- sql = "CREATE TABLE IF NOT EXISTS TestUser
(ID INTEGER PRIMARY KEY autoincrement,
NAME TEXT, SEX TEXT, AGES INTEGER)"; - this.db.execSQL(sql);
- Log.v(TAG, "Create Table TestUser ok");
- } catch (Exception e) {
- Log.v(TAG, "Create Table TestUser fail");
- } finally {
- //this.db.close();
- Log.v(TAG, "Create Table TestUser ");
- }
- }
- public boolean save(String name, String sex, Integer ages) {
- String sql = "insert into TestUser values
(null,'" + name + "','" + sex - + "'," + ages + ")";
- try {
- this.db.execSQL(sql);
- Log.v(TAG, "insert Table TestUser 1 record ok");
- return true;
- } catch (Exception e) {
- Log.v(TAG, "insert Table TestUser 1 record fail");
- return false;
- } finally {
- //this.db.close();
- Log.v(TAG, "insert Table TestUser ");
- }
- }
- public Cursor loadAll() {
- Cursor cur = db.query("TestUser", new String[]
{ "ID", "NAME","SEX","AGES"}, null, - null, null, null, null);
- return cur;
- }
- public void open(Context context){
- if (null == db || !this.db.isOpen()){
- this.context = context;
- this.db = context.openOrCreateDatabase(this.DataBaseName,
- context.MODE_PRIVATE, null);
- createTabel();
- Log.v(this.TAG, "create or Open DataBase。。。");
- }
- }
- public void close() {
- db.close();
- }
- }