編輯:高級開發
在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();
- }
- }
2) android數據庫交互代碼
- package com.topsun;
- import android.app.Activity;
- import android.database.Cursor;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.VIEw;
- import android.view.VIEw.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- public class guiWindows extends Activity
implements OnClickListener {- EditText TEditname;
- EditText TEditsex;
- EditText TEditages;
- EditText TEditmerge;
- Button TSavebutton;
- Button TVIEwbutton;
- DBHelper db;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentVIEw(R.layout.main);
- this.TEditname = (EditText) this.findVIEwById
(R.id.widgeteditname);- this.TEditsex = (EditText) this.findVIEwById
(R.id.widgeteditsex);- this.TEditages = (EditText) this.findVIEwById
(R.id.widgeteditages);- TEditmerge = (EditText) this.findVIEwById
(R.id.widgeteditmerge);- this.TSavebutton = (Button) this.findVIEwById
(R.id.widgetSavebutton);- TVIEwbutton = (Button) this.findVIEwById
(R.id.widgetVIEwbutton);- this.db = new DBHelper(this);
- this.TSavebutton.setOnClickListener(this);
- this.TVIEwbutton.setOnClickListener(this);
- }
- @Override
- public void onClick(VIEw v) {
- // TODO Auto-generated method stub
- // this.TEditages.setText(this.TEditname.getText().
toString()+this.TEditsex.getText().toString());- if (v.getId() == R.id.widgetSavebutton) {
- try {
- this.db.open(this);
- this.db.save(this.TEditname.getText().toString(), this.TEditsex
- .getText().toString(), Integer.valueOf(this.TEditages
- .getText().toString()));
- } catch (Exception e) {
- Log.v("save data", "save data fail");
- } finally {
- this.db.close();
- }
- } else if (v.getId() == R.id.widgetVIEwbutton && null != db) {
- this.db.open(this);
- // 浏覽所有數據
- Cursor cur = db.loadAll();
- StringBuffer sf = new StringBuffer();
- cur.moveToFirst();
- while (!cur.isAfterLast()) {
- sf.append(cur.getInt(0)).append(" : ").append(cur.getString(1))
- .append(" : ").append(cur.getString(2)).append(" : ")
- .append(cur.getInt(3)).append("\n");
- cur.moveToNext();
- }
- db.close();
- this.TEditmerge.setText(sf.toString());
- }
- }
- }
最近,Google面向大學生推出android開發挑戰賽,android開發成為時下開發者的熱點開發項目。像《在NetBeans上搭建android SDK環境》這樣的
目標:利用NDK 生成 SO 庫,使用 SO 庫進行 JNI 調用,在 android sdcard 創建文件並寫入數據。 工具:NDK1.5 R1, android
本軟件除了擁有傳統的日歷功能外,還具有查詢天氣預報、添加提醒時間,顯示農歷日期、天干地支、宜忌、公歷、農歷節日等信息。要注意的是,查詢天氣預報需要訪問internet.
國外一家名為Phandroid的網站近日披露了android 3.0(Gingerbread)的一些細節。雖然新的系統仍在開發之中,不過我們仍然可以從曝光的一些細節中看