android database 相關,androiddatabase

來源:互聯網
上載者:User

android database 相關,androiddatabase

以前用database從來都是直接執行,db.execute()。


今天看公司1.0的源碼中又學習了一個邏輯,

 SecureSQLiteDatabase db = null;

        try {

            db = mOpenHelper.openDatabase();


            db.beginTransaction();

            try {

                 todo 資料操作

                };

//db 所要執行的操作

                db.update(MstDataEntry.TABLE_NAME, values, whereClause, whereArgs);


                db.setTransactionSuccessful();

            } finally {

                db.endTransaction();

            }

        } finally {

            mOpenHelper.close();

        }


程式執行到endTransaction() 方法時會檢查事務的標誌是否為成功,

如果程式執行到endTransaction()之前調用了setTransactionSuccessful() 方法設定事務的標誌為成功則提交事務,

如果沒有調用setTransactionSuccessful() 方法則復原事務。

這樣就能使我們要執行的操作也就是事務被確定執行才返回,並關掉db。



對於android資料庫打包的問題,高分懸賞

我來賺分了 呵呵
其實很簡單的 我給你一個資料庫的基礎類吧 你繼承一下,然後就可以在sd卡上建立自己的資料庫了。 呵呵

你給的分好多哇
額 沒法上傳附件。。。直接代碼吧。。

package sean.data;

import java.io.File;

import sean.configs.Const;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.os.Environment;
import android.util.Log;

public abstract class SDSQLiteOpenHelper {
private static final String TAG = SDSQLiteOpenHelper.class.getSimpleName();

//@SuppressWarnings("unused")
//private final Context context;
private final String dbName;
private final CursorFactory factory;
private final int newVersion;
private String dbPath = Const.DBPath;

private SQLiteDatabase sdDatabase = null;
private boolean isInitializing = false;

/**
* Create a helper object to create, open, and/or manage a database. The
* database is not actually created or opened until one of
* {@link #getWritableDatabase} or {@link #getReadableDatabase} is called.
*
* @param context
* to use to open or create the database
* @param name
* of the database file, or null for an in-memory database
* @param factory
* to use for creating cursor objects, or null for the default
* @param version
* number of the database (starting at 1); if the database is
* older, {@link #onUpgrade} will be used to upgrade the database
*&#......餘下全文>>
 
android eclipse 資料庫使用

Android平台的資料庫大多是用ContentProvider進行封裝的。
建議你看一下SDK文檔中關於ContentProvider的內容
另外,建議你在SDK下的sample code當中找一個Provider的例子參考一下。

下面來簡單回答一下你提到的幾個問題:
1.這個資料庫是個.txt還是像mysql那樣的呀?
Android平台上的資料庫對SQL進行了一下封裝和精簡。叫SQLite。
基本操作(查詢,插入,更改刪除)和SQL一樣。
當你的程式運行過一次之後,就會在你應用的“安裝目錄”下產生一個*.db的檔案。
比如說你應用的包名是zhidao.baidu.com
那麼資料庫檔案就在下面的這個路徑下
/data/data/zhidao.baidu.com/databases/xxx.db
檔案名稱是你在ContentProvider裡指定的

2.在哪裡可以看到這個資料庫和表裡的內容呢?
同時給你推薦一個叫SQLite Browser的工具,你可以上網下一個。
windows版的和Linux版的都有,可以很直觀的看到db檔案裡的內容,
既然你對mysql比較熟的話,這個就更不成問題了。

3.假如我的資料庫類是db.java,然後別的有a.java、b.java、c.java什麼的,怎麼能關聯起來呢?
Android平台上,如果你繼承ContentProvider寫了一個類,用來操作資料庫,
那麼你需要在AndroidManifese.xml檔案中聲明這個資料庫
對這個資料庫的聲明有一個很重要的一點,就是它的authorities
這個authorities就是你給這個建立的資料庫所指定的一個虛擬路徑
它的命名方式和包名的命名類似
開發人員需要保證它的唯一性
其它的a.java,b.java,c.java可以通過你聲明的這個路徑來找到這個資料庫
哪怕a,b,c這幾個檔案是另外一個應用當中的檔案,一樣是可以訪問的
就像是你指定mysql資料的地址,不管你是在哪個php檔案當中,你都可以嘗試去訪問這個mysql資料庫

先說這麼多吧。
還是建議你先看一下SDK下的sample code裡的例子,裡面有很多基礎的例子。

200分夠多的啊,別忘了加分哈 ~O_O~
參考資料:www.lephone.net
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.