導航:首頁 > 操作系統 > android打包sqlite

android打包sqlite

發布時間:2023-02-22 15:09:51

1. 如何將android的資料庫打包到APK里

sdk不能直接打開res\raw目錄中的資料庫文件,不過我們可以通過將這個文件復制到手機內存或sd卡上來訪問。復制的方法也很簡單,就是在程序第一次啟動時判斷要復制的目標路徑是否存在該資料庫文件,如果不存在該資料庫文件,那先復制這個文件,然後再打開資料庫文件。
復制資料庫文件可以通過getresources().openrawresource方法獲得res\raw目錄中資源的inputstream對象,然後將該inputstream對象中的數據寫入其他的目錄中相應文件中。打開資料庫可以使用sqlitedatabase.openorcreatedatabase方法來打開sqlite資料庫文件。openorcreatedatabase方法可以打開任意目錄下的資料庫文件。

2. Android 如何打包sqlite資料庫問題。。!!

第一個問題簡單,你直接寫一個文件下載界面,讓用戶自己選擇自己所在城市,然後去下載相應的資料庫。把資料庫保存到你說的那個位置就好了。

3. 如何sqllite資料庫復制到android手機上

你是說直接打包到apk包中么?

1.把你的.sqlite文件打包成zip文件放在Assert目錄下。

2.在程序第一次運行時,Activity.getAssert()函數讀助assert目錄下的sqlite文件,復制到你想要的目錄下(一般是sd卡目錄下,不過要注意的就是 要檢查sd卡是否存在)

3.用SqliteDataBase.openDataBase()函數打開資料庫,進行各種操作。

--------------------------
但你的需求,如果只是配置信息,用SharedPreferences 來做不是更方便么?

4. android怎樣把SQLite打包到APk中

你在eclipse中編譯工程的時候,db資料庫文件會自動打包到apk文件中,也就是說你安裝這個apk的時候,資料庫就已經存在了,無需在手工導入

5. android上如何使用sqlite資料庫

SQLite 一個非常流行的嵌入式資料庫,它支持 SQL 語言,並且只利用很少的內存就有很好的性能。此外它還是開源的,任何人都可以使用它。許多開源項目((Mozilla, PHP, Python)都使用了 SQLite.

Android 開發中使用 SQLite 資料庫
Activites 可以通過 Content Provider 或者 Service 訪問一個資料庫。下面會詳細講解如果創建資料庫,添加數據和查詢資料庫。
創建資料庫
Android 不自動提供資料庫。在 Android 應用程序中使用 SQLite,必須自己創建資料庫,然後創建表、索引,填充數據。Android 提供了 SQLiteOpenHelper 幫助你創建一個資料庫,你只要繼承 SQLiteOpenHelper 類,就可以輕松的創建資料庫。SQLiteOpenHelper 類根據開發應用程序的需要,封裝了創建和更新資料庫使用的邏輯。SQLiteOpenHelper 的子類,至少需要實現三個方法:
構造函數,調用父類 SQLiteOpenHelper 的構造函數。這個方法需要四個參數:上下文環境(例如,一個 Activity),資料庫名字,一個可選的游標工廠(通常是 Null),一個代表你正在使用的資料庫模型版本的整數。
onCreate()方法,它需要一個 SQLiteDatabase 對象作為參數,根據需要對這個對象填充表和初始化數據。
onUpgrage() 方法,它需要三個參數,一個 SQLiteDatabase 對象,一個舊的版本號和一個新的版本號,這樣你就可以清楚如何把一個資料庫從舊的模型轉變到新的模型。

6. 關於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
*/
//Context context0,
public SDSQLiteOpenHelper(String name0,
CursorFactory factory0, int version0) {
if (version0 < 1)
throw new IllegalArgumentException("Version must be >= 1, was "
+ version0);

// context = context0;
if (name0.indexOf('/') != -1) {// 如果name0包含路徑的話,更改dbPath為name0中的指定路徑
while (name0.charAt(0) == '/') {// 將name0中最前面的'/'去除
name0 = name0.substring(1);
}
dbPath += name0.substring(0, name0.lastIndexOf('/'));
name0 = name0.substring(name0.lastIndexOf('/') + 1, name0.length());
}
dbName = name0;
factory = factory0;
newVersion = version0;
}

/**
* Create and/or open a database that will be used for reading and writing.
* Once opened successfully, the database is cached, so you can call this
* method every time you need to write to the database. Make sure to call
* {@link #close} when you no longer need it.
*
* <p>
* Errors such as bad permissions or a full disk may cause this operation to
* fail, but future attempts may succeed if the problem is fixed.
* </p>
*
* @throws SQLiteException
* if the database cannot be opened for writing
* @return a read/write database object valid until {@link #close} is called
*/
public synchronized SQLiteDatabase getWritableDatabase() {
if (sdDatabase != null && sdDatabase.isOpen()
&& !sdDatabase.isReadOnly()) {
return sdDatabase; // The database is already open for business

}

if (isInitializing) {
throw new IllegalStateException(
"getWritableDatabase called recursively");
}

// If we have a read-only database open, someone could be using it
// (though they shouldn't), which would cause a lock to be held on
// the file, and our attempts to open the database read-write would
// fail waiting for the file lock. To prevent that, we acquire the
// lock on the read-only database, which shuts out other users.

boolean success = false;
SQLiteDatabase db = null;
try {
isInitializing = true;
if (dbName == null) {
db = SQLiteDatabase.create(null);
} else {
String path = getDatabaseFile(dbName).getPath();
db = SQLiteDatabase.openOrCreateDatabase(path, factory);
}

int version = db.getVersion();
if (version != newVersion) {
db.beginTransaction();
try {
if (version == 0) {
onCreate(db);
} else {
onUpgrade(db, version, newVersion);
}
db.setVersion(newVersion);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
}

onOpen(db);
success = true;
return db;
} finally {
isInitializing = false;
if (success) {
if (sdDatabase != null) {
try {
sdDatabase.close();
} catch (Exception e) {
}
}
sdDatabase = db;
} else {
if (db != null)
db.close();
}
}
}

/**
* Create and/or open a database. This will be the same object returned by
* {@link #getWritableDatabase} unless some problem, such as a full disk,
* requires the database to be opened read-only. In that case, a read-only
* database object will be returned. If the problem is fixed, a future call
* to {@link #getWritableDatabase} may succeed, in which case the read-only
* database object will be closed and the read/write object will be returned
* in the future.
*
* @throws SQLiteException
* if the database cannot be opened
* @return a database object valid until {@link #getWritableDatabase} or
* {@link #close} is called.
*/
public synchronized SQLiteDatabase getReadableDatabase() {
if (sdDatabase != null && sdDatabase.isOpen()) {
return sdDatabase; // The database is already open for business

}

if (isInitializing) {
throw new IllegalStateException(
"getReadableDatabase called recursively");
}

try {
return getWritableDatabase();
} catch (SQLiteException e) {
if (dbName == null)
throw e; // Can't open a temp database read-only!

Log.e(TAG, "Couldn't open " + dbName
+ " for writing (will try read-only):", e);
}

SQLiteDatabase db = null;
try {
isInitializing = true;
String path = getDatabaseFile(dbName).getPath();
db = SQLiteDatabase.openDatabase(path, factory,
SQLiteDatabase.OPEN_READWRITE);
if (db.getVersion() != newVersion) {
throw new SQLiteException(
"Can't upgrade read-only database from version "
+ db.getVersion() + " to " + newVersion + ": "
+ path);
}

onOpen(db);
Log.w(TAG, "Opened " + dbName + " in read-only mode");
sdDatabase = db;
return sdDatabase;
} finally {
isInitializing = false;
if (db != null && db != sdDatabase)
db.close();
}
}

/**
* Close any open database object.
*/
public synchronized void close() {
if (isInitializing)
throw new IllegalStateException("Closed ring initialization");

if (sdDatabase != null && sdDatabase.isOpen()) {
sdDatabase.close();
sdDatabase = null;
}
}

public File getDatabaseFile(String name) {
String EXTERN_PATH = null;
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED) == true) {
EXTERN_PATH = dbPath;
File f = new File(EXTERN_PATH);
if (!f.exists()) {
f.mkdirs();
}
}
return new File(EXTERN_PATH + name);
}

/**
* Called when the database is created for the first time. This is where the
* creation of tables and the initial population of the tables should
* happen.
*
* @param db
* The database.
*/
public abstract void onCreate(SQLiteDatabase db);

/**
* Called when the database needs to be upgraded. The implementation should
* use this method to drop tables, add tables, or do anything else it needs
* to upgrade to the new schema version.
*
* <p>
* The SQLite ALTER TABLE documentation can be found <a
* href="http://sqlite.org/lang_altertable.html">here</a>. If you add new
* columns you can use ALTER TABLE to insert them into a live table. If you
* rename or remove columns you can use ALTER TABLE to rename the old table,
* then create the new table and then populate the new table with the
* contents of the old table.
*
* @param db
* The database.
* @param oldVersion
* The old database version.
* @param newVersion
* The new database version.
*/
public abstract void onUpgrade(SQLiteDatabase db, int oldVersion,
int newVersion);

/**
* Called when the database has been opened. Override method should check
* {@link SQLiteDatabase#isReadOnly} before updating the database.
*
* @param db
* The database.
*/
public void onOpen(SQLiteDatabase db) {
}

public String getDBFilePath() {
return dbPath + dbName;
}

}

7. 新手,cocos2dx移植到android平台不知道怎麼導入sqlite3庫

創建windows平台項目 一、 搭建windows開發環境 官網上顯示cocos2d-x支持win7、winxp、vista系統但我只在win7上用過以下說明均是在win7系統上的編譯器使用vs2010。 1、 安裝vs2010 2、 安裝Visual Assist X可選 3、 執行build-win32.bat編譯cocos2d-x 4、 運行cocos2d-x根目錄下的install-templates-msvc.bat將cocos2d-x模板加入vs模板庫。 5、將.\Debug.win32目錄下的所有dll文件拷貝至Windows\system32\ (iconv.dll libcocos2d.dll libcocosDenshion.dll libcuri.dll libEGL.dll libgles_cm.dll libxml.dll pthreadVCE2.dll zlib1.dll )6、 雙擊cocos2d-win32.vc2010.sln進入vs編譯環境 二、 新建win32項目 1、在「解決方案」上單擊右鍵選擇「添加」-〉「新建項目」。 2、選擇「左側Cocos2d-x模板」在右側列表中選擇「Cocos2d-win32 Application」輸入項目名稱「Cocos2dDemo」點「確定」。3、在彈出的對話框中點擊「下一步」。 4、如果需要使用Box2D引擎則選中否則不要選。點擊「finish」創建成功。 三、 編譯運行 1、右鍵Cocos2dDemo項目選擇「生成」進行編譯。 2、右鍵Cocos2dDemo項目選擇「設為啟動項目」。然後F5或者點擊菜單欄的運行鍵 運行。四、 創建新的解決方案 新建項目有兩種方式一是直接在cocos2d-x解決方案中添加新項目上面已經介紹過了。當你剛開始接觸cocos2d-x還在嘗試寫demo時用這種方式可以快捷的創建demo而且方便查看例子中的代碼。另一種方式是創建一個新的解決方案方法如下 1、「文件」-〉「新建」-〉「項目」 2、進入新建項目的窗口選擇「左側Cocos2d-x模板」在右側列表中選擇「Cocos2d-win32 Application」輸入項目名稱「Cocos2dDemo」點「確定」。然後執行「下一步」。 3、使用Box2D引擎則選中否則不要選。點擊「finish」創建成功。 4、單擊左下角的屬性標簽或者選擇「視圖」-〉「屬性管理器」打開屬性管理器5、在屬性管理器中雙擊「Microsoft.Cpp.Win32.user」彈出屬性頁在裡面設置VC++目錄即可。這樣該設置對所有VC項目都有效見下圖。 6、在「包含目錄」中, 加入下列項目 (D:\android\cocos2d-2.0-x-2.0.3\cocos2dx\platform\third_party\win32) 7、在「庫目錄」中, 加入下列項目(D:\adroid\cocos2d-2.0x-2.0.3\Debug.win32)(發布時用release目錄下的!!) 8、上面的第4、5、6、7條也可以通過如下方法解決: 定位到%USERPROFILE%\appdata\local\microsoft\msbuild\v4.0可以通過運行或直接在windows資源管理器的地址欄中輸入使用其他文本編輯工具修改Microsoft.Cpp.Win32.user.props或Microsoft.Cpp.X64.user.props文件中的相關欄位。以下是我的文件內容 <IncludePath>D:\android\cocos2d-2.0-x-2.0.3;D:\android\cocos2d-2.0-x-2.0.3\cocos2dx;D:\android\cocos2d-2.0-x-2.0.3\cocos2dx\include;D:\android\cocos2d-2.0-x-2.0.3\cocos2dx\platform;D:\android\cocos2d-2.0-x-2.0.3\cocos2dx\platform\win32;D:\android\cocos2d-2.0-x-2.0.3\cocos2dx\platform\third_party\win32\OGLES;D:\android\cocos2d-2.0-x-2.0.3\external\Box2D;D:\android\cocos2d-2.0-x-2.0.3\CocosDenshion\include;D:\android\cocos2d-2.0-x-2.0.3\cocos2dx\platform\third_party\win32;D:\android\cocos2d-2.0-x-2.0.3\cocos2dx\kazmath\include;$(IncludePath)</IncludePath><LibraryPath>D:\android\cocos2d-2.0-x-2.0.3\Debug.win32;$(LibraryPath)</LibraryPath> 第4至8條的作用是加入解決方案所依賴的頭文件和庫文件以保證項目的正常編譯運行。 五、 如何刪除vs里的cocos2d-x模板 1、 打開如下目錄 (Example as default install) 1VS2008: C:\Program Files\Microsoft Visual Studio 9.0\VC\vcprojects\ 2VC2008Express: C:\Program Files\Microsoft Visual Studio 9.0\VC\Express\VCProjects 3VS2010: C:\Program Files\Microsoft Visual Studio 10.0\VC\vcprojects\ 4VC2010Express: C:\Program Files\Microsoft Visual Studio 10.0\VC\Express\VCProjects 2、 刪除此目錄下的 Cocos2d-x文件夾 3、 刪除前綴為 CCAppWiz (0.8.0 or later) 或 CCXAppWiz (0.7.2 or earlier)的文件
將項目由win32移植到android平台 一、 快速搭建android開發環境 1、將伺服器上的eclipse、java、android sdk、android ndk、cygwin、cocos2d-x文件夾拷貝到本地請注意保存的路徑為英文、不包含空格、且不要過長。 2、 運行eclipse添加android-sdk的路徑。(preferences->Android:SDK Location) 3、 添加ADT(Help->Install New Software:Work with) 4、添加java環境變數。 新建變數JAVA_HOME變數值為java目錄的絕對路徑。 在path中添加 %JAVA_HOME%\D:\android\Java\jdk1.6.0_20\bin; %JAVA_HOME%\D:\android\Java\jdk1.6.0_20\jre\bin; D:\android\Java\jre6\bin; 5、運行一次Cygwin.bat 6、修改\cygwin\home\用戶名\.bash_profile文件添加NDK_ROOT=/cygdrive/d/android/android-ndk-r8b export NDK_ROOT 7、 修改\cocos2d-2.0-x-2.0.3\create-android-project.bat中的如下路徑 set _CYGBIN=e:\cygwin\bin set _ANDROIDTOOLS=e:\android\android-sdk\tools set _NDKROOT=e:\android\android-ndk-r8 二、 新建android項目 1、 運行create-android-project.bat創建新的android項目例如項目名稱為Cocos2dDemo。 2、 將win32下完成的.cpp和.h文件拷貝到\Cocos2dDemo\Classes 3、將win32下使用的資源拷貝到\Cocos2dDemo\proj.android\assets 4、將cpp文件的路徑、使用的頭文件的目錄加入到Android.mk文件。 5、打開Cygwincd 到/Cocos2dDemo/proj.android目錄下 6、執行./build_native.sh進行編譯 7、在eclipse中編譯打包。 三、 補充 1、2.0以上版本的引擎基本無法在android模擬器上運行建議在xcode和vs上調試好直接在真機上運行。 2、2.0以上版本的引擎要求android的開發和運行環境都要2.2以上的sdk。
將項目由win32移植到ios 一、 創建ios項目 1、 安裝XCode 2、執行install-templates-msvc.bat將模板添加到XCode 3、打開xcode選擇Create a new Xcode project選擇cocos2d-x模版 4、點擊next填入項目名稱Man 5、找到Man的主項目在這里新建一個iOS文件夾。點擊create創建完成 6、刪除Resource和Classes下的多餘文件Helloworld的代碼和資源 7、右鍵點擊Resource 選擇Add Files to "Man"... 7、將Man主項目文件夾下的Resource文件夾中的資源添加到當前工程。注意選擇create folder references for any added folders只是添加一個引用 8、同樣的方法添加源文件和頭文件到Classes下 9、點擊Run編譯運行 二、 補充 1、Mac 平台的OS X需要10.6及以上版本 2、IOS平台需要4.0及以上版本 3、IOS真機支持iPad 1、iPad 2、iPad 3、 iPhone 3GS、iPhone 4、iPhone 4S、iPhone 5、iphone6、iPod Touch 3 and iPod Touch 4 不支持iPhone and iPhone 3G。

8. android sqlite如何進行封裝

一、[代碼]DatabaseUtil.java

packagecom.dbexample;
importandroid.content.ContentValues;
importandroid.content.Context;
importandroid.database.Cursor;
importandroid.database.SQLException;
importandroid.database.sqlite.SQLiteDatabase;
importandroid.database.sqlite.SQLiteOpenHelper;
importandroid.util.Log;
publicclassDatabaseUtil{
privatestaticfinalStringTAG="DatabaseUtil";
/**
*DatabaseName
*/
_NAME="student_database";
/**
*DatabaseVersion
*/
privatestaticfinalintDATABASE_VERSION=1;
/**
*TableName
*/
_TABLE="tb_student";
/**
*Tablecolumns
*/
publicstaticfinalStringKEY_NAME="name";
publicstaticfinalStringKEY_GRADE="grade";
publicstaticfinalStringKEY_ROWID="_id";
/**
*Databasecreationsqlstatement
*/
_STUDENT_TABLE=
"createtable"+DATABASE_TABLE+"("+KEY_ROWID+","
+KEY_NAME+"textnotnull,"+KEY_GRADE+"textnotnull);";
/**
*Context
*/
privatefinalContextmCtx;
;
privateSQLiteDatabasemDb;
/**
*Innerprivateclass..
*/
{
DatabaseHelper(Contextcontext){
super(context,DATABASE_NAME,null,DATABASE_VERSION);
}
/**
*'texists.
*/
@Override
publicvoidonCreate(SQLiteDatabasedb){
Log.i(TAG,"CreatingDataBase:"+CREATE_STUDENT_TABLE);
db.execSQL(CREATE_STUDENT_TABLE);
}
/**
*.
*/
@Override
publicvoidonUpgrade(SQLiteDatabasedb,intoldVersion,intnewVersion){
Log.w(TAG,"Upgradingdatabasefromversion"+oldVersion+"to"
+newVersion);
}
}
/**
*Constructor-
*opened/created
*
*@
*/
publicDatabaseUtil(Contextctx){
this.mCtx=ctx;
}
/**
*Thismethodisusedforcreating/openingconnection
*@returninstanceofDatabaseUtil
*@throwsSQLException
*/
publicDatabaseUtilopen()throwsSQLException{
mDbHelper=newDatabaseHelper(mCtx);
mDb=mDbHelper.getWritableDatabase();
returnthis;
}
/**
*.
*/
publicvoidclose(){
mDbHelper.close();
}
/**
*Thismethodisusedtocreate/insertnewrecordStudentrecord.
*@paramname
*@paramgrade
*@returnlong
*/
publiclongcreateStudent(Stringname,Stringgrade){
ContentValuesinitialValues=newContentValues();
initialValues.put(KEY_NAME,name);
initialValues.put(KEY_GRADE,grade);
returnmDb.insert(DATABASE_TABLE,null,initialValues);
}
/**
*.
*@paramrowId
*@returnboolean
*/
publicbooleandeleteStudent(longrowId){
returnmDb.delete(DATABASE_TABLE,KEY_ROWID+"="+rowId,null)>0;
}
/**
*.
*@returnCursor
*/
publicCursorfetchAllStudents(){
returnmDb.query(DATABASE_TABLE,newString[]{KEY_ROWID,KEY_NAME,
KEY_GRADE},null,null,null,null,null);
}
/**
*.
*@paramid
*@returnCursor
*@throwsSQLException
*/
publicCursorfetchStudent(longid)throwsSQLException{
CursormCursor=
mDb.query(true,DATABASE_TABLE,newString[]{KEY_ROWID,
KEY_NAME,KEY_GRADE},KEY_ROWID+"="+id,null,
null,null,null,null);
if(mCursor!=null){
mCursor.moveToFirst();
}
returnmCursor;
}
/**
*.
*@paramid
*@paramname
*@paramstandard
*@returnboolean
*/
publicbooleanupdateStudent(intid,Stringname,Stringstandard){
ContentValuesargs=newContentValues();
args.put(KEY_NAME,name);
args.put(KEY_GRADE,standard);
returnmDb.update(DATABASE_TABLE,args,KEY_ROWID+"="+id,null)>0;
}
}

二、[代碼]使用方法

//插入
DatabaseUtildbUtil=newDatabaseUtil(this);
dbUtil.open();
dbUtil.createStudent("PrashantThakkar","10th");
dbUtil.close();
//查詢
DatabaseUtildbUtil=newDatabaseUtil(this);
dbUtil.open();
Cursorcursor=dbUtil.fetchAllStudents();
if(cursor!=null){
while(cursor.moveToNext()){
Log.i("Student","StudentName:"+cursor.getString(1)+
"Grade"+cursor.getString(2));
}
}
dbUtil.close();
閱讀全文

與android打包sqlite相關的資料

熱點內容
推特app界面如何設置成中文 瀏覽:452
太空工程師轉子編程屬性 瀏覽:32
windowscmd關機命令 瀏覽:342
雲桌面只要伺服器裝一套軟體 瀏覽:247
電腦右鍵按到什麼導致文件夾全屏 瀏覽:454
我的世界如何製造伺服器主城 瀏覽:365
linuxssh連不上 瀏覽:297
永宏plc用什麼編程電纜 瀏覽:371
win激活命令行 瀏覽:886
新手學電腦編程語言 瀏覽:893
雲空間在哪個文件夾 瀏覽:926
編程游戲小貓抓小魚 瀏覽:790
安卓dosbox怎麼打開 瀏覽:774
伺服器無影響是怎麼回事 瀏覽:958
比德電子采購平台加密 瀏覽:202
加密貨幣400億 瀏覽:524
植發2次加密 瀏覽:44
vc6查看編譯的錯誤 瀏覽:595
心理大全pdf 瀏覽:1002
區域鏈加密幣怎麼樣 瀏覽:343