<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Oracle Berkeley DB 中国研发团队的博客 &#187; Android</title>
	<atom:link href="http://www.bdbchina.com/tag/android/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bdbchina.com</link>
	<description>Oracle Berkeley DB 中国研发团队的博客</description>
	<lastBuildDate>Thu, 15 Dec 2011 10:35:52 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Berkeley DB 5.2 Android平台编译过程介绍</title>
		<link>http://www.bdbchina.com/2011/07/berkeley-db-5-2-android%e5%b9%b3%e5%8f%b0%e7%bc%96%e8%af%91%e8%bf%87%e7%a8%8b%e4%bb%8b%e7%bb%8d/</link>
		<comments>http://www.bdbchina.com/2011/07/berkeley-db-5-2-android%e5%b9%b3%e5%8f%b0%e7%bc%96%e8%af%91%e8%bf%87%e7%a8%8b%e4%bb%8b%e7%bb%8d/#comments</comments>
		<pubDate>Tue, 12 Jul 2011 06:19:00 +0000</pubDate>
		<dc:creator>赵汝聪</dc:creator>
				<category><![CDATA[Berkeley DB]]></category>
		<category><![CDATA[赵汝聪]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[DBSQL]]></category>
		<category><![CDATA[Drop-in]]></category>

		<guid isPermaLink="false">http://www.bdbchina.com/?p=1584</guid>
		<description><![CDATA[在Berkeley DB 5.2版本中，Android Drop-in编译方式正式纳入官方发布文档。5.2的Android编译过程和先前我基于DBSQL 5.0写的博文稍有不同。以下介绍Oracle Berkeley DB 5.2官方文档中Android安装文档。欢迎交流。
Android默认提供的数据库功能是SQLite。Berkeley DBSQL完全兼容SQLite API，可以直接替换SQLite库而无需改动现有的应用程序或服务。依本方法编译得到的Berkeley DBSQL库将完全替换Android的SQLite库(drop-in模式)。在这种模式下，Android平台上所有的应用程序(包括我们自己编译的应用程序)都将链接到Berkeley DBSQL库以获取更高的存储和并发性能。
完整原文：http://download.oracle.com/docs/cd/E17076_02/html/installation/build_android_intro.html
中文介绍：

Android编译过程
本节提供了Berkeley DB Drop-in方式的Android编译指令。
首先要下载并且编译Android源代码树。
第一次编译过程需要花费较长一段时间，不过后面再编译就快多了。可以从这里下载Android的源代码并且按照Android文档完成编译过程。
把Berkeley DB代码目录拷到Android的代码目录里面：

$ cd ${root}/external/sqlite/dist
$ tar zxvf db-xx.tar.gz

这里的${root}指的是Android源代码树的根路径。
通过以下命令将源代码树中默认的Android.mk文件替换成Berkeley DB的版本：

$ cd ${root}/external/sqlite/dist
$ mv Android.mk Android.mk.sqlite
$ cp db-xx/build_android/Android.mk ./

提示：编译之前，可通过这里查看Android平台上的几种调优参数。
重新编译数据库部分，这样Berkeley DB就替换到最终生成的系统镜像了：

$ cd ${root}
$ . build/envsetup.sh
$ make clean-libsqlite
$ mmm -B external/sqlite/dist
$ make snod

以上指令完成后，新的基于Berkeley DB SQL的Android系统镜像即编译完毕。系统会打印出新的镜像所在目录，一般来说这个目录是：${root}/out/target/product/generic。 
从SQLite迁移到Berkeley DB
如果你需要使DBSQL能够自动打开并转换现有的SQLite数据库文件，请参照本节的步骤。
首先，我们需要通过以下脚本来为Android平台编译一个静态的SQLite shell, 这个shell对自动转换功能而言是必需的。

#!/bin/bash
# This script shows how to use built-in toolchain [...]]]></description>
			<content:encoded><![CDATA[<p>在Berkeley DB 5.2版本中，Android Drop-in编译方式正式纳入官方发布文档。5.2的Android编译过程和先前我基于DBSQL 5.0写的<a href="http://www.bdbchina.com/2010/04/%E5%9C%A8android%E5%B9%B3%E5%8F%B0%E4%B8%8A%E5%BA%94%E7%94%A8berkeley-db-11gr2-sqldrop-in%E6%A8%A1%E5%BC%8F/">博文</a>稍有不同。以下介绍Oracle Berkeley DB 5.2官方文档中Android安装文档。欢迎交流。</p>
<p>Android默认提供的数据库功能是SQLite。Berkeley DBSQL完全兼容SQLite API，可以直接替换SQLite库而无需改动现有的应用程序或服务。依本方法编译得到的Berkeley DBSQL库将完全替换Android的SQLite库(drop-in模式)。在这种模式下，Android平台上所有的应用程序(包括我们自己编译的应用程序)都将链接到Berkeley DBSQL库以获取<a href="http://www.bdbchina.com/2010/03/%E6%9B%B4%E5%A4%9A%E5%85%B3%E4%BA%8Eoracle-berkeley-db-sql%E7%9A%84%E6%8A%A5%E9%81%93/">更高的存储和并发性能</a>。</p>
<p>完整原文：http://download.oracle.com/docs/cd/E17076_02/html/installation/build_android_intro.html</p>
<p>中文介绍：<br />
<span id="more-1584"></span></p>
<h2>Android编译过程</h2>
<p>本节提供了Berkeley DB Drop-in方式的Android编译指令。</p>
<p>首先要下载并且编译Android源代码树。<br />
第一次编译过程需要花费较长一段时间，不过后面再编译就快多了。可以从<a href="http://source.android.com/source/download.html">这里</a>下载Android的源代码并且按照Android文档完成编译过程。</p>
<p>把Berkeley DB代码目录拷到Android的代码目录里面：</p>
<pre name="code" class="java:nogutter">
$ cd ${root}/external/sqlite/dist
$ tar zxvf db-xx.tar.gz
</pre>
<p>这里的${root}指的是Android源代码树的根路径。</p>
<p>通过以下命令将源代码树中默认的Android.mk文件替换成Berkeley DB的版本：</p>
<pre name="code" class="java:nogutter">
$ cd ${root}/external/sqlite/dist
$ mv Android.mk Android.mk.sqlite
$ cp db-xx/build_android/Android.mk ./
</pre>
<p>提示：编译之前，可通过<a href="http://download.oracle.com/docs/cd/E17076_02/html/installation/build_android_config.html">这里</a>查看Android平台上的几种调优参数。</p>
<p>重新编译数据库部分，这样Berkeley DB就替换到最终生成的系统镜像了：</p>
<pre name="code" class="java:nogutter">
$ cd ${root}
$ . build/envsetup.sh
$ make clean-libsqlite
$ mmm -B external/sqlite/dist
$ make snod
</pre>
<p>以上指令完成后，新的基于Berkeley DB SQL的Android系统镜像即编译完毕。系统会打印出新的镜像所在目录，一般来说这个目录是：${root}/out/target/product/generic。 </p>
<h2>从SQLite迁移到Berkeley DB</h2>
<p>如果你需要使DBSQL能够自动打开并转换现有的SQLite数据库文件，请参照本节的步骤。</p>
<p>首先，我们需要通过以下脚本来为Android平台编译一个静态的SQLite shell, 这个shell对自动转换功能而言是必需的。</p>
<pre name="code" class="java:nogutter">
#!/bin/bash
# This script shows how to use built-in toolchain to build
# sqlite3 shell, which is required by Berkeley DB SQL
# on-the-fly migration feature.

# Note: these variables should be set per active Android source tree
# We assume $PWD=$ROOT/external/sqlite/dist
ROOT=${PWD}/../../..
TOOLCHAIN=${ROOT}/prebuilt/linux-x86/toolchain/arm-eabi-4.4.0
CC=${TOOLCHAIN}/bin/arm-eabi-gcc
LIB="${ROOT}/out/target/product/generic/obj/lib"
INCLUDE="${ROOT}/ndk/build/platforms/android-8/arch-arm/usr/include"

# CFLAGS should be set per Android.mk.sqlite (the original
# version of SQLite's Android.mk)
CFLAGS="-DHAVE_USLEEP=1 -DSQLITE_THREADSAFE=1 -DNDEBUG=1 \
 -DSQLITE_DEFAULT_JOURNAL_SIZE_LIMIT=1048576 \
 -DSQLITE_ENABLE_MEMORY_MANAGEMENT=1 \
 -DSQLITE_DEFAULT_AUTOVACUUM=1 \
 -DSQLITE_TEMP_STORE=3 -DSQLITE_ENABLE_FTS3 \
 -DSQLITE_ENABLE_FTS3_BACKWARDS -DTHREADSAFE=1"
CFLAGS="${CFLAGS} -I${INCLUDE}"

LDFLAGS="-ldl -nostdlib -Wl,--gc-sections -lc -llog -lgcc \
 -Wl,--no-undefined,-z,nocopyreloc ${LIB}/crtend_android.o \
 ${LIB}/crtbegin_dynamic.o -L${LIB} -Wl,-rpath,${LIB}"

${CC} -DANDROID -DOS_ANDROID --sysroot="${SYSROOT}" -mandroid \
      -fvisibility=hidden -ffunction-sections -fdata-sections \
      -fPIC ${LDFLAGS} ${CFLAGS} \
      sqlite3.c shell.c -o sqlite3orig
</pre>
<p>提示：你可能需要根据当前环境来修改以上环境变量。</p>
<p>运行以上脚本，成功的话我们就得到了一个静态的sqlite3 shell工具 &#8211; sqlite3orig</p>
<p>接下来我们需要修改系统image文件。首先用xyaffs2工具解压镜像文件</p>
<pre name="code" class="java:nogutter">$ xyaffs2 ./system.img system</pre>
<p>把编译好的sqlite3工具拷进去。</p>
<pre name="code" class="java:nogutter">
$ cp ${root}/external/sqlite/dist/sqlite3orig system/xbin/sqlite3orig
</pre>
<p>使用mkyaffs2image工具来重新生成system.img.</p>
<pre name="code" class="java:nogutter">
$ mkyaffs2image -f $PWD/system system.img
</pre>
<p>这样，修改好的系统镜像即可自动转换并打开现成的SQLite3数据库文件。需要注意的是：在adb shell模式下，打开sqlite3数据库的命令是sqlite3orig；而打开Berkeley DBSQL数据库的命令是sqlite3。</p>
<h2>结束语</h2>
<p>以上介绍了DBSQL Android平台上Drop-in模式的编译使用过程。如果你在使用过程中有任何问题，欢迎和我交流！</p>
<p>BDB Blog：<a href="http://www.bdbchina.com">http://www.bdbchina.com</a><br />
BDB微博：<a href="http://weibo.com/bdbchina">http://weibo.com/bdbchina</a><br />
个人微博：<a href="http://weibo.com/2153755234">http://weibo.com/2153755234</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.bdbchina.com/2011/07/berkeley-db-5-2-android%e5%b9%b3%e5%8f%b0%e7%bc%96%e8%af%91%e8%bf%87%e7%a8%8b%e4%bb%8b%e7%bb%8d/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>在Android平台上应用Berkeley DB 11gR2 SQL(drop-in模式)</title>
		<link>http://www.bdbchina.com/2010/04/%e5%9c%a8android%e5%b9%b3%e5%8f%b0%e4%b8%8a%e5%ba%94%e7%94%a8berkeley-db-11gr2-sqldrop-in%e6%a8%a1%e5%bc%8f/</link>
		<comments>http://www.bdbchina.com/2010/04/%e5%9c%a8android%e5%b9%b3%e5%8f%b0%e4%b8%8a%e5%ba%94%e7%94%a8berkeley-db-11gr2-sqldrop-in%e6%a8%a1%e5%bc%8f/#comments</comments>
		<pubDate>Thu, 01 Apr 2010 11:28:50 +0000</pubDate>
		<dc:creator>赵汝聪</dc:creator>
				<category><![CDATA[Berkeley DB]]></category>
		<category><![CDATA[赵汝聪]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[bdb]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.bdbchina.com/?p=980</guid>
		<description><![CDATA[前文我们介绍了如何编译Berkeley DB SQL(DBSQL)库并放到Android平台上使用。现在我们来介绍另一种更为彻底的使用方式。依本方法编译得到的BDB库将完全替换Android的SQLite库，我们称其为drop-in模式。在这种模式下，Android平台上所有的应用程序(包括我们自己编译的应用程序)都将链接到DBSQL库以获取更高的存储和并发性能。
本文将展示如何在Ubuntu Linux x32平台上使用完整的Android源代码来编译构建我们的全新Android系统镜像，而Android源代码包中包含了交叉编译器，因此我们不需要事先安装NDK。

1. 编译Android代码
从Android官方网站获取一份Android源代码，这份源代码有2~3G之巨。
在Linux平台上，把源代码解压到$HOME/android/android_src
安装java 1.5版本，(1.6不行)，并设置JAVA_HOME和PATH:

$ export JAVA_HOME=/usr/lib/jvm/java-1.5.0-sun
$ export PATH=$JAVA_HOME/bin:$PATH

先把原始的Android代码完全编译成功(这过程可能要花2~3小时)：

$ cd $HOME/android/android_src
$ . build/envsetup.sh
$ make

可看到生成三个镜像：

Install system fs image: out/target/product/generic/system.img
Target ram disk: out/target/product/generic/ramdisk.img
Target userdata fs image: out/target/product/generic/userdata.img

2. 安装Android SDK并测试镜像
安装Android SDK: http://developer.android.com/sdk/installing.html, 安装目录：$HOME/android/android-sdk-linux_86
安装Android SDK组件: http://developer.android.com/sdk/adding-components.html,选择安装SDK Platform Android 2.1 API 7和Google APIs by Google Inc., Android API 7
用我们编译得到的镜像替换系统的默认镜像。如果需要替换Windows环境下的sdk，那么以下目录&#8221;android-sdk-linux_86&#8243;相应应该改成 &#8220;android-sdk-windows&#8221;。

$ cd $HOME/android/android-sdk-linux_86/platforms/android-2.1
$ cp -r images images.bak
$ cp $HOME/android/android_src/out/target/product/generic/*.img [...]]]></description>
			<content:encoded><![CDATA[<p>前文我们介绍了如何编译Berkeley DB SQL(DBSQL)库并放到Android平台上使用。现在我们来介绍另一种更为彻底的使用方式。依本方法编译得到的BDB库将完全替换Android的SQLite库，我们称其为drop-in模式。在这种模式下，Android平台上所有的应用程序(包括我们自己编译的应用程序)都将链接到DBSQL库以获取更高的存储和并发性能。</p>
<p>本文将展示如何在Ubuntu Linux x32平台上使用完整的Android源代码来编译构建我们的全新Android系统镜像，而Android源代码包中包含了交叉编译器，因此我们不需要事先安装NDK。</p>
<p><span id="more-980"></span></p>
<h2>1. 编译Android代码</h2>
<p>从<a href="http://source.android.com/download">Android官方网站</a>获取一份Android源代码，这份源代码有2~3G之巨。</p>
<p>在Linux平台上，把源代码解压到$HOME/android/android_src</p>
<p>安装java 1.5版本，(1.6不行)，并设置JAVA_HOME和PATH:</p>
<pre name="code" class="java:nogutter">
$ export JAVA_HOME=/usr/lib/jvm/java-1.5.0-sun
$ export PATH=$JAVA_HOME/bin:$PATH
</pre>
<p>先把原始的Android代码完全编译成功(这过程可能要花2~3小时)：</p>
<pre name="code" class="java:nogutter">
$ cd $HOME/android/android_src
$ . build/envsetup.sh
$ make
</pre>
<p>可看到生成三个镜像：</p>
<pre name="code" class="java:nogutter">
Install system fs image: out/target/product/generic/system.img
Target ram disk: out/target/product/generic/ramdisk.img
Target userdata fs image: out/target/product/generic/userdata.img
</pre>
<h2>2. 安装Android SDK并测试镜像</h2>
<p>安装Android SDK: <a href="http://developer.android.com/sdk/installing.html">http://developer.android.com/sdk/installing.html</a>, 安装目录：$HOME/android/android-sdk-linux_86<br />
安装Android SDK组件: <a href="http://developer.android.com/sdk/adding-components.html">http://developer.android.com/sdk/adding-components.html</a>,选择安装SDK Platform Android 2.1 API 7和Google APIs by Google Inc., Android API 7</p>
<p>用我们编译得到的镜像替换系统的默认镜像。如果需要替换Windows环境下的sdk，那么以下目录&#8221;android-sdk-linux_86&#8243;相应应该改成 &#8220;android-sdk-windows&#8221;。</p>
<pre name="code" class="java:nogutter">
$ cd $HOME/android/android-sdk-linux_86/platforms/android-2.1
$ cp -r images images.bak
$ cp $HOME/android/android_src/out/target/product/generic/*.img images
</pre>
<p>创建并运行一个新的设备:</p>
<pre name="code" class="java:nogutter">
$ cd $HOME/android/android-sdk-linux_86/tools/
$ ./android
</pre>
<p><a href="http://www.bdbchina.com/wp-content/uploads/2010/03/bdb_drop_in_3-e1269938549584.png"><img src="http://www.bdbchina.com/wp-content/uploads/2010/03/bdb_drop_in_3-e1269938549584.png" alt="" title="bdb_drop_in_3" width="498" height="287" class="alignnone size-full wp-image-995" /></a></p>
<p>(如图)使用图形界面工具创建一个Android 2.1的设备BDBSQL,并点击&#8221;start&#8221;启动。新的设备第一次启动的时间会非常长，这是正常现象。等到Android界面出现，就说明我们的镜像编译基本没问题了。接下来我们进一步把SQLite库替换为BDB。</p>
<h2>3. 编译DBSQL</h2>
<p>从<a href="http://www.oracle.com/technology/software/products/berkeley-db/index.html">http://www.oracle.com/technology/software/products/berkeley-db/index.html</a>下载BDB 11gR2源文件(db-5.0.x.tar.gz)，然后</p>
<pre name="code" class="java:nogutter">
$ cd $HOME/android/android_src/external/sqlite/dist
$ tar zxvf db-5.0.x.tar.gz
$ cp Android.mk Android.mk.bak
$ cp db-5.0.x/build_android/Android.mk .
</pre>
<p>编辑Android.mk，默认的BDB Log和Region文件大小总和可达到十几兆，因此我们有必要修改Android.mk来减小它(可能需要为Android.mk去掉只读属性)。修改第一处LOCAL_CFLAGS如下所示(相对于默认配置，我们删除了-DSQLITE_OMIT_TRUNCATE_OPTIMIZATION，新增了-DSQLITE_DEFAULT_CACHE_SIZE=256 -DBDBSQL_MAX_LOCKS=1000 -DBDBSQL_MAX_LOCK_OBJECTS=1000)：</p>
<pre name="code" class="java:nogutter">
LOCAL_CFLAGS += -Wall -DHAVE_USLEEP=1 \
        -DSQLITE_DEFAULT_JOURNAL_SIZE_LIMIT=1048576 \
        -DSQLITE_THREADSAFE=1 -DNDEBUG=1 -DSQLITE_TEMP_STORE=3 \
        -DSQLITE_OS_UNIX=1 \
        -D_HAVE_SQLITE_CONFIG_H -DSQLITE_THREAD_OVERRIDE_LOCK=-1 \
        -DSQLITE_ENABLE_FTS3 -DSQLITE_ENABLE_FTS3_BACKWARDS -Dfdatasync=fsync \
        -DSQLITE_DEFAULT_CACHE_SIZE=256 \
        -DBDBSQL_MAX_LOCKS=1000 \
        -DBDBSQL_MAX_LOCK_OBJECTS=1000
</pre>
<p>如果确定不会在多进程间共享数据库，还可以在上述LOCAL_CFLAGS中加入&#8221;-DBDBSQL_OMIT_SHARING&#8221;，这样日志文件不会被写入文件系统，进一步节省了设备空间。</p>
<p>编译系统镜像中的SQLite部分:</p>
<pre name="code" class="java:nogutter">
$ cd $HOME/android/android_src
$ . build/envsetup.sh
$ make clean-libsqlite
$ mmm -B external/sqlite/dist
$ make snod
</pre>
<p>编译好了，再来一次镜像替换：</p>
<pre name="code" class="java:nogutter">
$ cp $HOME/android/android_src/out/target/product/generic/*.img $HOME/android/android-sdk-linux_86/platforms/android-2.1/images
</pre>
<p>使用图形界面工具删除先前的设备BDBSQL，重创建一个新的Android 2.1设备BDBSQL,并点击&#8221;start&#8221;启动。这里一定要创建新的设备，因为启动过的设备，其数据库文件已被SQLite生成，DBSQL无法访问。</p>
<h2>4. 使用DBSQL</h2>
<p>现在Android里所有应用程序的SQL存储引擎都已被替换为DBSQL了。我们使用&#8221;联系人&#8221;来试用一下我们的新设备吧，在Setting里面把Locate设置为中文简体，安装一个中文输入法(比如搜狗)，然后打开“联系人”，随便增加一条记录：<br />
<a href="http://www.bdbchina.com/wp-content/uploads/2010/03/bdb_drop_in_5.jpg"><img src="http://www.bdbchina.com/wp-content/uploads/2010/03/bdb_drop_in_5.jpg" alt="" title="bdb_drop_in_5" width="600" height="421" class="alignnone size-full wp-image-1001" /></a></p>
<p>在主机上我们可以使用adb shell来查看并操作程序所生成的数据库。请留意sqlite3 shell输出的版本信息是Berkeley DB 11gR，同时shell的提示符是dbsql>，这说明我们已经把SQLite成功替换为DBSQL引擎：</p>
<pre name="code" class="java:nogutter">
$ adb shell
# cd /data/data/com.android.providers.contacts/databases/
# sqlite3 contacts2.db
Berkeley DB 11g Release 2, library version 11.2.5.0.21: (March 30, 2010)
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
dbsql> .tables
_sync_state                       status_updates
_sync_state_metadata              v1_settings
activities                        view_contacts
agg_exceptions                    view_contacts_restricted
android_metadata                  view_data
calls                             view_data_restricted
contact_entities_view             view_groups
contact_entities_view_restricted  view_raw_contacts
contacts                          view_raw_contacts_restricted
data                              view_v1_contact_methods
groups                            view_v1_extensions
mimetypes                         view_v1_group_membership
name_lookup                       view_v1_groups
nickname_lookup                   view_v1_organizations
packages                          view_v1_people
phone_lookup                      view_v1_phones
raw_contacts                      view_v1_photos
settings
dbsql> select * from contacts;
2|逍遥 李|||0|0|0|0|1|1|0nE08B4A8BFA37D4||0
dbsql> select * from data;
1||4|1|0|0|0|li|li|||||||||||||||||
2||6|2|0|0|0|66666666|1||66666666|||||||||||||||
3||6|2|0|0|0|88888888|2||88888888|||||||||||||||
4||8|2|0|0|0|蜀山|1|||||||||||||||||
5||4|2|0|0|0|逍遥 李|逍遥|李||||||||||||||||
dbsql>
</pre>
<h2>5. 结束语</h2>
<p>关于DBSQL库在Android平台上的编译和使用的介绍到这里就结束了。我们用两篇博文分别介绍了side-by-side(并存)和Drop-in(替换)这两种使用方式。在Drop-in模式下，现有的应用程序无修改就可以直接使用DBSQL引擎。</p>
<p>DBSQL在开发过程中始终给予Android平台高度重视，在开发过程中投入了多位资深工程师致力于将DBSQL移植到Android平台上，并将Android列为产品周期测试的主流平台。OARDC的工程师Zengfa.Dou和Stephen.Tang给予了我这个初学者极大帮助。在此一并致谢！</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bdbchina.com/2010/04/%e5%9c%a8android%e5%b9%b3%e5%8f%b0%e4%b8%8a%e5%ba%94%e7%94%a8berkeley-db-11gr2-sqldrop-in%e6%a8%a1%e5%bc%8f/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>在Android平台上应用Berkeley DB 11gR2 SQL(side-by-side模式)</title>
		<link>http://www.bdbchina.com/2010/04/%e5%9c%a8android%e5%b9%b3%e5%8f%b0%e4%b8%8a%e5%ba%94%e7%94%a8berkeley-db-11gr2-sqlside-by-side%e6%a8%a1%e5%bc%8f/</link>
		<comments>http://www.bdbchina.com/2010/04/%e5%9c%a8android%e5%b9%b3%e5%8f%b0%e4%b8%8a%e5%ba%94%e7%94%a8berkeley-db-11gr2-sqlside-by-side%e6%a8%a1%e5%bc%8f/#comments</comments>
		<pubDate>Thu, 01 Apr 2010 11:28:14 +0000</pubDate>
		<dc:creator>赵汝聪</dc:creator>
				<category><![CDATA[Berkeley DB]]></category>
		<category><![CDATA[赵汝聪]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[bdb]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.bdbchina.com/?p=946</guid>
		<description><![CDATA[Berkeley DB(BDB)从新发布的版本11gR2开始支持SQL。BDB在性能上的优势详见博文&#8221;Oracle Berkeley DB 支持SQL啦&#8221;以及更多关于Oracle Berkeley DB SQL的报道。本文将介绍如何在Android上开发平台上编译并运行BDB 11gR2 SQL库(以下简称DBSQL)。
本文介绍在Linux平台上如何编译得到适用于Android的DBSQL库，依本文方法编译得到的BDB库将和Android自带的SQLite库并存，我们称其为side-by-side(并存)模式。在这种模式下，Android平台上已有的应用程序还是基于原始的SQLite库，而我们自己编译的应用程序则链接到DBSQL库以获取更高的存储和并发性能。

1. 安装开发环境
(本文假定读者已具备初步的Android Emulator使用经验)
安装SDK
从http://developer.android.com/sdk/index.html下载Android SDK并解压缩得到$HOME/android/android-sdk-linux_86/目录。
安装NDK
从http://developer.android.com/intl/zh-TW/sdk/ndk/index.html下载Linux版Android NDK(android-ndk-r3-linux-x86.zip)并解压缩得到$HOME/android/android-ndk-1.6_r3/目录。

$ cd android-ndk-1.6_r3
$ build/host-setup.sh

安装NDK Wapper
从开心网共享文件夹下载ndk-wrappers.tgz到$HOME/android

$ tar zxvf ndk-wrappers.tgz

ndk-wrapper需要以下变量，因此我们需要把以下变量增加到$HOME/.bashrc并使之生效：
export PATH=$HOME/android/ndk-wrappers/bin:$PATH
$ export NDK_DIR=$HOME/android/android-ndk-1.6_r3
$ export PATH=$HOME/android/android-sdk-linux_86/tools:$PATH
$ . $HOME/.bashrc

(NDK-Wrapper默认使用arm-eabi-gcc 4.2.1, 如果你想使用ndk里面的arm-eabi-gcc 4.4.0，把scripts/env-utils.sh 和setup.sh里面的4.2.1改为4.4.0)
开始安装ndk-wrapper：

$ cd ndk-wrappers
$ ./setup.sh

修改Ndk-Wrapper
修改ndk-wrappers/bin/arm-linux-gcc:

1. 在LINK_FLAGS中增加"-ldl"
2. 删除-fvisibility=hidden

试验Ndk-Wrapper是否可工作
$ arm-linux-gcc --version
arm-eabi-gcc (GCC) 4.4.0
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying [...]]]></description>
			<content:encoded><![CDATA[<p>Berkeley DB(BDB)从新发布的版本11gR2开始支持SQL。BDB在性能上的优势详见<a href="http://www.bdbchina.com/2010/03/oracle-berkeley-db-%E6%94%AF%E6%8C%81sql%E5%95%A6%EF%BC%81/">博文&#8221;Oracle Berkeley DB 支持SQL啦&#8221;</a>以及<a href="http://www.bdbchina.com/2010/03/%E6%9B%B4%E5%A4%9A%E5%85%B3%E4%BA%8Eoracle-berkeley-db-sql%E7%9A%84%E6%8A%A5%E9%81%93/">更多关于Oracle Berkeley DB SQL的报道</a>。本文将介绍如何在Android上开发平台上编译并运行BDB 11gR2 SQL库(以下简称DBSQL)。</p>
<p>本文介绍在Linux平台上如何编译得到适用于Android的DBSQL库，依本文方法编译得到的BDB库将和Android自带的SQLite库并存，我们称其为side-by-side(并存)模式。在这种模式下，Android平台上已有的应用程序还是基于原始的SQLite库，而我们自己编译的应用程序则链接到DBSQL库以获取更高的存储和并发性能。</p>
<p><span id="more-946"></span></p>
<h2>1. 安装开发环境</h2>
<p>(本文假定读者已具备初步的Android Emulator使用经验)<br />
<strong>安装SDK</strong><br />
从<a href="http://developer.android.com/sdk/index.html">http://developer.android.com/sdk/index.html</a>下载Android SDK并解压缩得到$HOME/android/android-sdk-linux_86/目录。</p>
<p><strong>安装NDK</strong><br />
从<a href="http://developer.android.com/intl/zh-TW/sdk/ndk/index.html">http://developer.android.com/intl/zh-TW/sdk/ndk/index.html</a>下载Linux版Android NDK(android-ndk-r3-linux-x86.zip)并解压缩得到$HOME/android/android-ndk-1.6_r3/目录。</p>
<pre class="java:nogutter">
$ cd android-ndk-1.6_r3
$ build/host-setup.sh
</pre>
<p><strong>安装NDK Wapper</strong><br />
从开心网<a "http://www.kaixin001.com/group/file.php?gid=627784&#038;fid=557046&#038;start=0">共享文件夹</a>下载ndk-wrappers.tgz到$HOME/android</p>
<pre class="java:nogutter">
$ tar zxvf ndk-wrappers.tgz
</pre>
<p>ndk-wrapper需要以下变量，因此我们需要把以下变量增加到$HOME/.bashrc并使之生效：</p>
<pre class="java:nogutter">export PATH=$HOME/android/ndk-wrappers/bin:$PATH
$ export NDK_DIR=$HOME/android/android-ndk-1.6_r3
$ export PATH=$HOME/android/android-sdk-linux_86/tools:$PATH
$ . $HOME/.bashrc
</pre>
<p>(NDK-Wrapper默认使用arm-eabi-gcc 4.2.1, 如果你想使用ndk里面的arm-eabi-gcc 4.4.0，把scripts/env-utils.sh 和setup.sh里面的4.2.1改为4.4.0)</p>
<p>开始安装ndk-wrapper：</p>
<pre class="java:nogutter">
$ cd ndk-wrappers
$ ./setup.sh
</pre>
<p><strong>修改Ndk-Wrapper</strong><br />
修改ndk-wrappers/bin/arm-linux-gcc:</p>
<pre class="java">
1. 在LINK_FLAGS中增加"-ldl"
2. 删除-fvisibility=hidden
</pre>
<p><strong>试验Ndk-Wrapper是否可工作</strong></p>
<pre class="java:nogutter">$ arm-linux-gcc --version
arm-eabi-gcc (GCC) 4.4.0
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
</pre>
<h2>2. 编译DBSQL</h2>
<p>以下我们使用上面搭建的NDK编译环境来编译适用于Android平台上的DBSQL库。首先从<a href="http://www.oracle.com/technology/software/products/berkeley-db/index.html">Oracle官网</a>上下载Berkeley DB 11gR2(.tar.gz), 解压并改名为db/</p>
<p><strong>设置CFLAGS</strong></p>
<pre class="java:nogutter">
$ export CFLAGS="-DHAVE_USLEEP=1 -DSQLITE_DEFAULT_JOURNAL_SIZE_LIMIT=1048576 \
-DNDEBUG=1 -DSQLITE_TEMP_STORE=3 -DSQLITE_OS_UNIX=1 -D_HAVE_SQLITE_CONFIG_H \
-DSQLITE_THREAD_OVERRIDE_LOCK=-1 -Dfdatasync=fsync -DNO_ANDROID_FUNCS \
-DSQLITE_THREADSAFE=1"
</pre>
<p><strong>设置为动态编译方式</strong></p>
<pre class="java:nogutter">
$ cd db/build_unix
$ ../dist/configure --prefix=$HOME/android/install_db_arm --target=arm-linux \
--host=arm-linux --enable-smallbuild --enable-sql --enable-sql_compat \
--enable-statistics --enable-amalgamation
</pre>
<p><strong>修改build_unix/db_config.h</strong><br />
每次configure完成后，切记都要编辑build_unix/db_config.h，删除以下两行：</p>
<pre class="java:nogutter">#define HAVE_PREAD 1
#define HAVE_PWRITE 1
</pre>
<p><strong>开始编译</strong></p>
<pre class="java:nogutter">
$ make dbsql
</pre>
<p>生成的动态库文件为.libs/libdb_sql-5.0.so，Shell文件为.libs/dbsql。</p>
<h2>3. 使用DBSQL库</h2>
<p>我们使用现成的DBSQL范例来演示如何在自己的程序中使用DBSQL库</p>
<p><strong>编译范例</strong></p>
<pre class="java:nogutter">
$ cd  build_unix
$ make ex_sql_query
</pre>
<p>编译完成后生成范例程序.libs/ex_sql_query /data/bdb</p>
<p><strong>运行范例</strong><br />
启动Android Emulator，上传，并运行DBSQL库和范例</p>
<pre class="java:nogutter">
$ adb shell mkdir /data/bdb
$ adb shell mkdir /data/sql/examples/data
$ adb shell mkdir /data/sql
$ adb shell mkdir /data/sql/examples
$ adb shell mkdir /data/sql/examples/data
$ adb push .libs/libdb_sql-5.0.so /data/bdb
$ adb push .libs/ex_sql_query /data/bdb
$ adb push ../sql/examples/data/ /data/sql/examples/data
$ adb shell "cd /data/bdb; export LD_LIBRARY_PATH=/data/bdb; ./ex_sql_query"
</pre>
<p>现在终端应该已经输出query范例的运行结果了。以下是GUI终端的输出截图：</p>
<p><a href="http://www.bdbchina.com/wp-content/uploads/2010/03/bdbsql_android_sidebyside.jpg"><img class="alignnone size-full wp-image-975" title="bdbsql_android_sidebyside" src="http://www.bdbchina.com/wp-content/uploads/2010/03/bdbsql_android_sidebyside.jpg" alt="" width="480" height="332" /></a></p>
<h2>4. 结束语</h2>
<p>至此，关于BDB 11gR2 SQL库的side-by-side编译模式的介绍就结束了。我们可将编译得到的库应用到自己的程序中去。side-by-side模式的优点不改动Android系统底层库和镜像，使用简单。缺点是只能用C/JNI方式调用，无法用Android自带的SQL接口直接调用。而与此对应的是Drop-in模式，Drop-in模式可将Android系统的SQLite层整个替换为BDB 11gR2 SQL，应用程序无修改就可以直接使用，是一种更为强大彻底的应用模式。我们将在后续文章中介绍Drop-in模式的编译方式。</p>
<input id="gwProxy" type="hidden" />
<input id="jsProxy" onclick="jsCall();" type="hidden" />
]]></content:encoded>
			<wfw:commentRss>http://www.bdbchina.com/2010/04/%e5%9c%a8android%e5%b9%b3%e5%8f%b0%e4%b8%8a%e5%ba%94%e7%94%a8berkeley-db-11gr2-sqlside-by-side%e6%a8%a1%e5%bc%8f/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Berkeley DB Java Edition Android/Google Maps Demo</title>
		<link>http://www.bdbchina.com/2010/02/berkeley-db-java-edition-androidgoogle-maps-demo/</link>
		<comments>http://www.bdbchina.com/2010/02/berkeley-db-java-edition-androidgoogle-maps-demo/#comments</comments>
		<pubDate>Wed, 24 Feb 2010 11:06:03 +0000</pubDate>
		<dc:creator>chaohuang</dc:creator>
				<category><![CDATA[Berkeley DB JE]]></category>
		<category><![CDATA[Chao Huang]]></category>
		<category><![CDATA[Android]]></category>

		<guid isPermaLink="false">http://www.bdbchina.com/?p=801</guid>
		<description><![CDATA[Thanks to Chris Eastland at Nebula Software Systems for the screen shot of this cool Google Maps/Android app built on BDB JE. The location data is stored in a JE database running on the device.
See the details at Charles Lamb&#8217;s blog at              [...]]]></description>
			<content:encoded><![CDATA[<p>Thanks to Chris Eastland at Nebula Software Systems for the screen shot of this cool Google Maps/Android app built on BDB JE. The location data is stored in a JE database running on the device.</p>
<p>See the details at Charles Lamb&#8217;s blog at                                         <a title="New window will open" href="http://www.linkedin.com/redirect?url=http%3A%2F%2Fblogs%2Eoracle%2Ecom%2FcharlesLamb%2F2010%2F02%2Fberkeley_db_java_edition_andro%2Ehtml&amp;urlhash=gH3q" target="_blank">http://blogs.oracle.com/charlesLamb/2010/02/berkeley_db_java_edition_andro.html</a> .</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bdbchina.com/2010/02/berkeley-db-java-edition-androidgoogle-maps-demo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Berkeley DB Java Edition/Android Whitepaper</title>
		<link>http://www.bdbchina.com/2010/02/berkeley-db-java-editionandroid-whitepaper/</link>
		<comments>http://www.bdbchina.com/2010/02/berkeley-db-java-editionandroid-whitepaper/#comments</comments>
		<pubDate>Wed, 24 Feb 2010 11:04:26 +0000</pubDate>
		<dc:creator>chaohuang</dc:creator>
				<category><![CDATA[Berkeley DB JE]]></category>
		<category><![CDATA[Chao Huang]]></category>
		<category><![CDATA[Android]]></category>

		<guid isPermaLink="false">http://www.bdbchina.com/?p=799</guid>
		<description><![CDATA[Charles Lamb (BDB-JE 架构师) 最近发表的一篇关于在Google Android运行Berkeley DB Java Edition的白皮书。书中简要描述了JE在在Android平台上运行的一些优势和特点，诸如性能，可扩展性，索引，并发控制，事务和多对多的 事物-线程映射模型。
原文地址： http://www.oracle.com/technology/products/berkeley-db/pdf/bdb-je-android.pdf .
]]></description>
			<content:encoded><![CDATA[<p>Charles Lamb (BDB-JE 架构师) 最近发表的一篇关于在Google Android运行Berkeley DB Java Edition的白皮书。书中简要描述了JE在在Android平台上运行的一些优势和特点，诸如性能，可扩展性，索引，并发控制，事务和多对多的 事物-线程映射模型。</p>
<p>原文地址： <a title="New window will open" href="http://www.linkedin.com/redirect?url=http%3A%2F%2Fwww%2Eoracle%2Ecom%2Ftechnology%2Fproducts%2Fberkeley-db%2Fpdf%2Fbdb-je-android%2Epdf&amp;urlhash=zko9" target="_blank">http://www.oracle.com/technology/products/berkeley-db/pdf/bdb-je-android.pdf</a> .</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bdbchina.com/2010/02/berkeley-db-java-editionandroid-whitepaper/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>在Google Android平台上运行Berkeley DB Java Edition</title>
		<link>http://www.bdbchina.com/2009/11/%e5%a6%82%e4%bd%95%e5%9c%a8android%e5%b9%b3%e5%8f%b0%e4%b8%8a%e9%85%8d%e7%bd%aeberkeley-db-java-edition/</link>
		<comments>http://www.bdbchina.com/2009/11/%e5%a6%82%e4%bd%95%e5%9c%a8android%e5%b9%b3%e5%8f%b0%e4%b8%8a%e9%85%8d%e7%bd%aeberkeley-db-java-edition/#comments</comments>
		<pubDate>Fri, 20 Nov 2009 06:53:49 +0000</pubDate>
		<dc:creator>haomianwang</dc:creator>
				<category><![CDATA[Berkeley DB JE]]></category>
		<category><![CDATA[Haomian Wang]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[bdb]]></category>
		<category><![CDATA[JE]]></category>

		<guid isPermaLink="false">http://www.bdbchina.com/?p=735</guid>
		<description><![CDATA[Google 推出Android平台一年多以来，Android已经成为最受欢迎的手机操作系统之一（另外几个成熟的手机操作平台包括Symbian, iPhone/MacOS, Windows Mobile等）。目前，已经至少有12款手机使用Android平台，并且据报道将会有更多的手机采用Android平台。
Android平台内置的存储数据库（或者说存储引擎）是SQLite。但由于SQLite其自身的一些缺陷，比如非Java语言、并发读写性能不 佳、技术支持困难等等，所以我们的用户倾向于Berkeley DB。我在这里向大家推荐Berkeley DB Java Edition（简称BDB JE）。我们可以把BDB JE的优势（它除了是存Java语言实现外，还有很高的存储性能和良好的并发性支持、Oracle的开发及技术支持团队等等）充分运用到Android 平台上。众所周知，Android平台是类Java语言的，所以，BDB JE对Android平台的开发者而言是一款利器。
以下将手把手教你将BDB JE快速配置到Android平台上（本文，我们用Android在PC机上的模拟器 (avd) 作为实例平台）。

准备工作

 如果你的电脑上尚未安装Android SDK。那么首先点击这里下载并且安装 Android SDK。Android的官方主页上有详细的如何安装并且启动Android 模拟器（avd）的文档，这里就不再涉及。必须注意的一点是， 将你所安装的Android目录下（假如是&#60;android-installation -home&#62;）的 “tools” 文件夹路径加入到你电脑的路径（path）中去（相信有一定编程经验的读者一定很熟悉如何添加系统路径了），这样，你就可以直接在命令行上运行 Android的各种命令了。


点此下载最新版本的BDB JE。解压缩之后（假如你解压路径为JE_HOME/）可以看到在JE_HOME/lib/ 下有一个“je-android-4.0.71.jar”文件。如果找不到，说明你下载的不是最新版本的BDB JE。请到Oracle BDB JE官方网址进行下载：http://www.oracle.com/technology/products/berkeley-db/je/index.html。


在开始下面教程之前，我默认大家已经有一定的Android编程基础，如果没有，请大家到Android的官方主页上阅读相关文档（比如，如 何实现“hello world”）.

选择一：在装有Android 插件的Eclipse 编程环境下进行配置

 如果你编程的IDE是Eclipse，并且你已经在上面安装了Android编程插件（如何在Eclipse上安装Android插件，请看这里），那么就继续阅读本小节以下步骤。注意，我用的是Eclipse3.4.2（Ganymede）版本， 也许不同的版本配置上有稍微的区别。这里建议Eclipse3.4版本以上。


打开Eclipse，确认在Windows-&#62;Preference-&#62;Android下已经写入Android SDK的路径。进入"File-&#62;New-&#62;Project-&#62;Android-&#62;Android Project-&#62;Next Project"，在弹出的建立project的面板上选择build target（比如Android2.0），并填写Project name (JEExample)、 application name (JEExample)、package name (com.sleepycat.je) 和 activity name (JEExample)。最后点击Finish。Eclipse将会自动帮你生成一个Android工程。假设你所建立的工程路径是&#60;eclipse-je-android-dir&#62;


 进入”Project-&#62;Properties-&#62;Libraries-&#62;Add External [...]]]></description>
			<content:encoded><![CDATA[<p>Google 推出Android平台一年多以来，Android已经成为最受欢迎的手机操作系统之一（另外几个成熟的手机操作平台包括Symbian, iPhone/MacOS, Windows Mobile等）。目前，已经至少有12款手机使用Android平台，并且据报道将会有更多的手机采用Android平台。</p>
<p>Android平台内置的存储数据库（或者说存储引擎）是SQLite。但由于SQLite其自身的一些缺陷，比如非Java语言、并发读写性能不 佳、技术支持困难等等，所以我们的用户倾向于Berkeley DB。我在这里向大家推荐Berkeley DB Java Edition（简称BDB JE）。我们可以把BDB JE的优势（它除了是存Java语言实现外，还有很高的存储性能和良好的并发性支持、Oracle的开发及技术支持团队等等）充分运用到Android 平台上。众所周知，Android平台是类Java语言的，所以，BDB JE对Android平台的开发者而言是一款利器。</p>
<p>以下将手把手教你将BDB JE快速配置到Android平台上（本文，我们用Android在PC机上的模拟器 (avd) 作为实例平台）。</p>
<p><span id="more-735"></span></p>
<h2>准备工作</h2>
<ul>
<li> 如果你的电脑上尚未安装Android SDK。那么首先<a onclick="javascript:pageTracker._trackPageview('/outgoing/developer.android.com/sdk/index.html');" href="http://developer.android.com/sdk/index.html" target="_blank">点击这里下载</a>并且安装 Android SDK。Android的官方主页上有详细的如何安装并且启动Android 模拟器（avd）的文档，这里就不再涉及。必须注意的一点是， 将你所安装的Android目录下（假如是<code>&lt;android-installation -home&gt;）</code>的 “tools” 文件夹路径加入到你电脑的路径（path）中去（相信有一定编程经验的读者一定很熟悉如何添加系统路径了），这样，你就可以直接在命令行上运行 Android的各种命令了。</li>
</ul>
<ul>
<li><a onclick="javascript:pageTracker._trackPageview('/outgoing/www.oracle.com/technology/software/products/berkeley-db/je/index.html');" href="http://www.oracle.com/technology/software/products/berkeley-db/je/index.html" target="_blank">点此下载</a>最新版本的BDB JE。解压缩之后（假如你解压路径为JE_HOME/）可以看到在JE_HOME/lib/ 下有一个“je-android-4.0.71.jar”文件。如果找不到，说明你下载的不是最新版本的BDB JE。请到Oracle BDB JE官方网址进行下载：<a href="http://www.oracle.com/technology/products/berkeley-db/je/index.html" target="_blank"><span class="moz-txt-link-freetext">http://www.oracle.com/technology/products/berkeley</span>-db/je/index.html</a>。</li>
</ul>
<ul>
<li>在开始下面教程之前，我默认大家已经有一定的Android编程基础，如果没有，请大家到Android的官方主页上阅读相关文档（比如，如 何实现“hello world”）.</li>
</ul>
<h2>选择一：在装有Android 插件的Eclipse 编程环境下进行配置</h2>
<ul>
<li> 如果你编程的IDE是Eclipse，并且你已经在上面安装了Android编程插件（如何在Eclipse上安装Android插件，请看<a onclick="javascript:pageTracker._trackPageview('/outgoing/developer.android.com/intl/zh-CN/sdk/eclipse-adt.html#installing');" href="http://developer.android.com/intl/zh-CN/sdk/eclipse-adt.html#installing" target="_blank">这里</a>），那么就继续阅读本小节以下步骤。注意，我用的是Eclipse3.4.2（Ganymede）版本， 也许不同的版本配置上有稍微的区别。这里建议Eclipse3.4版本以上。</li>
</ul>
<ul>
<li>打开Eclipse，确认在Windows-&gt;Preference-&gt;Android下已经写入Android SDK的路径。进入<code>"File-&gt;New-&gt;Project-&gt;Android-&gt;Android Project-&gt;Next Project"</code>，在弹出的建立project的面板上选择build target（比如Android2.0），并填写Project name (JEExample)、 application name (JEExample)、package name (<code>com.sleepycat.je</code>) 和 activity name (<code>JEExample</code>)。最后点击Finish。Eclipse将会自动帮你生成一个Android工程。假设你所建立的工程路径是<code>&lt;eclipse-je-android-dir&gt;</code></li>
</ul>
<ul>
<li> 进入”Project-&gt;Properties-&gt;Libraries-&gt;Add External JARs”，在弹出的窗口中选择JE_HOME/lib/je-android-4.0.71.jar。点击OK，你将可以看到在你建立的工程 （JEExample）下面有Referenced Libraries目录，点击之后可以看到je-android-4.0.71.jar。</li>
</ul>
<ul>
<li> <code>复制代码（这些代码可在下面的"源代码"小节找到）</code><code>：</code>
<ul>
<li><code>将JEExample.java</code> 复制到<code>&lt;eclipse-je-android-dir&gt;/src/com/sleepycat/je</code></li>
<li><code>将main.xml</code> 复制到<code>&lt;eclipse-je-android-dir&gt;/</code> <code>res/layout/main.xml</code> and</li>
<li><code>将strings.xml</code> 复制到 <code>&lt;eclipse-je-android-dir&gt;/</code><code>res/values/strings.xml</code></li>
</ul>
</li>
<li> 在Eclipse中，打开main.xml，你可以看到在上一步中复制的代码。点击Run，运行JEExample。在正确运行之前，你还需要在 Android上面建立一个用于存储JE数据的文件夹，具体步骤看“在Android模拟器上运行程序”小节中的第一步。</li>
</ul>
<h2>选择二：在非IDE环境下进行配置</h2>
<ul>
<li> 如果你不喜欢用IDE（如Eclipse)进行编程，只想通过命令行进行配置，请继续阅读本小节以下步骤。</li>
</ul>
<ul>
<li>首先建立一个Android 的模拟器（AVD）（如果你还没有在你的电脑上建立过Android 的模拟器）：
<ul>
<li><code>在命令行运行 android create avd --target 3 --name my_avd</code> （注意，我在这里建立的是Android1.6的模拟器，你也可以建立Android2.0模拟器）</li>
<li>在你想要建立Android工程的目录下面，运行
<ul>
<li><code>android create project --path JEExample --package com.sleepycat.je --name JEExample --activity JEExample --target 3</code></li>
</ul>
<p>命令运行之后，将在你所在目录下面建立Android工程，文件夹的名称叫做JEExample，其中会有 <code>JEExample/src/com/sleepycat/je/JEExample.java 文件。<br />
</code></li>
</ul>
</li>
<li> 将JE_HOME/lib/je-android-4.0.71.jar 文件复制到<code>JEExample/libs</code></li>
</ul>
<ul>
<li> 将三个文件：<code>JEExample/src/com/sleepycat/je/JEExample.java</code>，     <code>JEExample/res/layout/main.xml </code><code>和 JEExample/res/values/strings.xml</code> 替换成下面“源代码”小节中对应的三个文件：<code>JEExample.java</code>，     <code>main.xml</code> 和 <code>strings.xml</code>。</li>
</ul>
<ul>
<li> 进入<code>&lt;android-installation-home&gt;/platforms/android-1.6/tools</code>， 打开 <code>dx.bat</code> 文件， 将其中一行 “<code>REM set javaOpts=-Xmx256M</code>” 改成 “<code>set javaOpts=-Xmx512M</code>“。注意“REM”要去掉。另外，如果你之前建立的Android模拟器 （AVD）是Android2.0，那么就更改<code>android-2.0/tools下的dx.bat文件。</code></li>
</ul>
<ul>
<li> 运行Android 模拟器：emulator -avd <code>my_avd</code></li>
</ul>
<ul>
<li> 用命令行进入JEExample目录，然后运行命令
<ul>
<li><code>ant install</code></li>
</ul>
</li>
</ul>
<p style="padding-left: 30px;">这个命令将会编译<code>JEExample.java</code> ，然后生成<code>JEExample/bin/JEExample-debug.apk</code> ，并将它安装到你所运行的Android 模拟器中。</p>
<h2>在Android模拟器上运行示例程序</h2>
<ul>
<li> 首先，为Android程序建立JE environment 路径，用于存储JE的数据（这一步不可忽略，并且路径名要跟程序一致，否则无法正确运行程序）。具体做法是 ：
<ul>
<li><code>在命令行运行adb shell mkdir /data/local/je</code>。或者你首先运行adb shell，然后cd data/local，最后mkdir je。</li>
<li>你也可以删除这个文件夹：<code>rm /data/local/je/*。更多adb shell的命令请参照Android文档。</code></li>
</ul>
</li>
<li> 在Android模拟器窗口上点击三角按钮（在Home界面）进入程序列表，在这里你可以看到已经安装上去的 JEExample 程序图标, 点击它，运行JEExample程序。 进入程序之后，在程序界面最上方可以看到  “JEExample”  字样，下面是一个 文字编辑框（TextEdit box）, 还有两个按钮，分别是 “Put Data” 和  “Get Data” 。</li>
</ul>
<ul>
<li> 存数据：
<ul>
<li>在文字编辑框里输入 key/data 数据对（比如 k1/d1）然后点击 “Put Data” 按钮，这样一条数据（键是k1，值是d1）便存进JE数据库中。</li>
</ul>
</li>
</ul>
<p style="padding-left: 30px;">你可以在命令行运行adb shell，进入<code>/data/local/je， 就可以看到JE的存储数据。</code></p>
<ul>
<li> 读数据：
<ul>
<li>在文字在文字编辑框里输入 你之前存储的键值key（比如k1），然后点击 “Get Data” 按钮，将会弹出“Get Data”的对话框，上面显示你之前存储的数据（比如d1）。</li>
</ul>
</li>
</ul>
<h2>JE示例源代码</h2>
<ul>
<li> JEExample.java
<pre><code>
package com.sleepycat.je;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.util.Log;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.File;

import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseConfig;
import com.sleepycat.je.DatabaseEntry;
import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.je.OperationStatus;
import com.sleepycat.je.Transaction;

public class JEExample extends Activity {

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        try {
            final File envDir = new File("/data/local/je");
            final EnvironmentConfig envConfig = new EnvironmentConfig();
            envConfig.setTransactional(true);
            envConfig.setAllowCreate(true);
            final Environment env = new Environment(envDir, envConfig);
            final DatabaseConfig dbConfig = new DatabaseConfig();
            dbConfig.setTransactional(true);
            dbConfig.setAllowCreate(true);
            dbConfig.setSortedDuplicates(true);
            final Database db = env.openDatabase(null, "exampledb", dbConfig);

            setContentView(R.layout.main);
            final Button button1 = (Button) findViewById(R.id.do_put);
            button1.setOnClickListener(new Button.OnClickListener() {
                public void onClick(View v) {

                    final EditText editText =
                        (EditText) findViewById(R.id.entry);
                    final String keyData = editText.getText().toString();
                    final int idx = keyData.indexOf("/");
                    String key = null;
                    String data = null;
                    String result = null;
                    if (idx &lt; 0) {
                        result = "enter key/data to put";
                    } else {
                        key = keyData.substring(0, idx);
                        data = keyData.substring(idx + 1);
                        result = key + "/" + data;
                        final DatabaseEntry keyEntry =
                            new DatabaseEntry(key.getBytes());
                        final DatabaseEntry dataEntry =
                            new DatabaseEntry(data.getBytes());

                        try {
                            final Transaction txn =
                                env.beginTransaction(null, null);
                            final OperationStatus res =
                                db.put(txn, keyEntry, dataEntry);
                            if (res != OperationStatus.SUCCESS) {
                                result = "Error: " + res.toString();
                            }
                            txn.commit();
                        } catch (DatabaseException DE) {
                            result = "Caught exception: " + DE.toString();
                        }
                    }
                    Log.d("JE", "did put of: " + result);

                    if (result.contains("Caught exception:")) {
                    	new AlertDialog.Builder(JEExample.this).
                    	    setTitle("Put Data").setMessage(result).
                    	    setPositiveButton("Quit", new DialogInterface.OnClickListener() {
                    		    public void onClick(DialogInterface dialog, int whichButton) {
                    		    }
                    		}).show();
                    } else {
                    	new AlertDialog.Builder(JEExample.this).
                	        setTitle("Put Data").setMessage("You put the key/data pair: " + result).
                	        setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
                		        public void onClick(DialogInterface dialog, int whichButton) {
                		        }
                		    }).show();
                    }
                }
            });

            final Button button2 = (Button) findViewById(R.id.do_get);
            button2.setOnClickListener(new Button.OnClickListener() {
                public void onClick(View v) {

                    final EditText editText =
                        (EditText) findViewById(R.id.entry);
                    final String key = editText.getText().toString();
                    final DatabaseEntry keyEntry =
                        new DatabaseEntry(key.getBytes());
                    final DatabaseEntry dataEntry = new DatabaseEntry();
                    String result = null;
                    try {
                        final Transaction txn =
                            env.beginTransaction(null, null);
                        final OperationStatus res =
                            db.get(txn, keyEntry, dataEntry, null);
                        if (res != OperationStatus.SUCCESS) {
                            result = "Error: " + res.toString();
                        } else {
                            result = new String(dataEntry.getData());
                        }
                        txn.commit();
                    } catch (DatabaseException DE) {
                        result = "Caught exception: " + DE.toString();
                    }
                    Log.d("JE", "did get of: " + result);
                    if (result.contains("Caught exception:")) {
                    	new AlertDialog.Builder(JEExample.this).
                	        setTitle("Get Data").setMessage(result).
                	        setPositiveButton("Quit", new DialogInterface.OnClickListener() {
                		        public void onClick(DialogInterface dialog, int whichButton) {
                		        }
                		    }).show();
                    } else {
                    	new AlertDialog.Builder(JEExample.this).
                	        setTitle("Get Data").setMessage("Get result: " + result).
                	        setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
                		        public void onClick(DialogInterface dialog, int whichButton) {
                		        }
                		    }).show();
                    }
                }
            });
        } catch (Exception DE) {
            TextView tv = new TextView(this);
            tv.setText("blew chunks " + DE);
            setContentView(tv);
        }
    }
}
</code></pre>
</li>
<li> res/layout/main.xml
<pre><code>
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    &gt;

  &lt;TextView android:id="@+id/label"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="JEExample"
    /&gt;

  &lt;EditText android:id="@+id/entry"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@android:drawable/editbox_background"
            android:layout_below="@id/label"
    /&gt;

  &lt;Button android:id="@+id/do_put"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/button_put"
    /&gt;

  &lt;Button android:id="@+id/do_get"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/button_get"
    /&gt;
&lt;/LinearLayout&gt;
</code></pre>
</li>
<li> res/values/strings.xml
<pre><code>
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;resources&gt;
    &lt;string name="app_name"&gt;JEExample&lt;/string&gt;
    &lt;string name="button_put"&gt;Put Data&lt;/string&gt;
    &lt;string name="button_get"&gt;Get Data&lt;/string&gt;
&lt;/resources&gt;</code></pre>
</li>
</ul>
<h2>结束语</h2>
<p>怎么样，是不是很简单就可以将JE配置到Android中了？你也赶紧试试吧。如果在配置或者安装过程中出错，欢迎给我留言。</p>
<p>注：英文好的同学，可以参照JE最新发布包中的&lt;je-home&gt;/docs/HOWTO-Android.html的英文原文。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bdbchina.com/2009/11/%e5%a6%82%e4%bd%95%e5%9c%a8android%e5%b9%b3%e5%8f%b0%e4%b8%8a%e9%85%8d%e7%bd%aeberkeley-db-java-edition/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
Դ
