您的位置:首页 > 移动开发 > 微信开发

Android小程序——乐学成语实现(一)

2016-06-06 19:30 369 查看

一、首先在res目录下新建raw目录,将idiom.db数据库复制到此目录下,这是因为raw目录的东西,android会原封不动的copy到应用程序中去,而不会被转化为二进制文件


二、在db包下新建DBOpenHelper类,一下的代码实现的功能主要是使用输入出流将idiom.db复制到手机中默认存放数据库的位置

<span style="font-size:18px;">public class DBOpenHelper {
private final int BUFFER_SIZE = 400000;//缓冲区大小
public static final String DB_NAME = "idioms.db";//保存的数据库名称
public static final String PACKAGE_NAME = "cn.edu.bztc.happyidiom";//应用的包名
public static final String DB_PATH = "/data"
+ Environment.getDataDirectory().getAbsolutePath()+ "/"
+ PACKAGE_NAME + "/databases"; //在手机里存放数据库的位置
private Context context;
public DBOpenHelper(Context context){
this.context = context;
}
public SQLiteDatabase openDatabase(){
try {
File myDataPath = new File(DB_PATH);
if(!myDataPath.exists()){
myDataPath.mkdirs();//如果没有这个目录则创建
}
String dbfile = myDataPath+"/"+DB_NAME;
if(!(new File(dbfile).exists())){//判断数据库文件是否存在,若不存在则执行导入,否则直接打开数据库
InputStream is = context.getResources().openRawResource(R.raw.idioms);
FileOutputStream fos = new FileOutputStream(dbfile);
byte[] buffer = new byte[BUFFER_SIZE];
int count = 0;
while((count=is.read(buffer))>0){
<span style="white-space:pre">	</span>fos.write(buffer,0,count);
}
fos.close();
is.close();
}
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
return db;
} catch (FileNotFoundException e) {
Log.e("Database", "File not found");
e.printStackTrace();
} catch (IOException e) {
Log.e("Database", "IO exception");
e.printStackTrace();
}
return null;
}
}	</span>

三、检测是否复制成功,首先需要修改AndroidManifest.xml配置文件搭建起单元测试的环境,代码如下:

<pre name="code" class="html"><span style="font-size:18px;"><span style="font-weight: normal;"><?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.edu.bztc.happyidiom"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="18" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
</span><uses-library android:name="android.test.runner"/><span style="font-weight: normal;">
</application></span></span>
<span style="font-size:18px;"><span style="font-weight: normal;">    </span><instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="cn.edu.bztc.happyidiom">
</instrumentation><span style="font-weight: normal;">
</manifest></span></span>


加上上述代码,单元测试环境就搭建起来了,接下来在test包下,新建DBOpenHelperTest继承AndroidTestCase

<span style="font-size:18px;">public class DBOpenHelperTest extends AndroidTestCase{
public void testDBCopy(){
DBOpenHelper dbOpenHelper = new DBOpenHelper(getContext());
dbOpenHelper.openDatabase();
}
}</span>

该类中只封装了一个方法,测试方法通常命名为testXXX(),该方法调用了DBOpenHelper类里面定义的openDatabase()方法,运行DBOpenHelperTest文件进行测试,如下:



接下来我们来看看是否复制成功,切换到DDMS,我们发现在data/data/应用的包下成功创建了数据库,如图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android