admin 管理员组文章数量: 894082
乐学成语——数据库创建(导入、打开)
1.创建数据库
通过Navicat Premium软件新建一个SQLite文件idioms.db,建表animal信息为
id int 自动增长 主键
name text(20) //成语名称
pronounce text(24) //成语发音
explain text(200) //成语的解释
antonym text(20) //反义词
homolonym text(20) //同义词
derivation text(200) //出处
examples text(200) //例子
把excel表中录入的数据导入新建的数据库中(注意:excel中的字段要和animal表中的字段名一一对应起来,方便导入)
把数据库idioms.db导出。
2.创建项目HappyIdiom并添加数据库
(1)新建项目HappyIdiom,在src目录下新建cn.edu.bztc.happyidiom.activity,cn.edu.bztc.happyidiom.dao,cn.edu.bztc.happyidiom.db,cn.edu.bztc.happyidiom.entity,cn.edu.bztc.happyidiom.test,cn.edu.bztc.happyidiom.util包。
(2)在res目录下新建raw目录,把建好的数据库idioms.db复制到此目录下。
(3)在db包下新建类DBOpenHelper,
package cn.edu.bztc.happyidiom.db;import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;import cn.edu.bztc.happyidiom.R;import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
import android.util.Log;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){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;}
}
(4)搭建单元测试环境。
在AndroidManifest.xml文件中添加测试环境
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""package="cn.edu.bztc.happyidiom"android:versionCode="1"android:versionName="1.0" ><uses-sdkandroid:minSdkVersion="14"android:targetSdkVersion="18" /><applicationandroid:allowBackup="true"android:icon="@drawable/logo"android:label="@string/app_name"android:theme="@style/AppTheme" ><uses-library android:name="android.test.runner"/><activityandroid:name="cn.edu.bztc.happyidiom.activity.MainActivity"android:label="@string/title_actity_main"android:theme="@android:style/Theme.NoTitleBar" ><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activity android:name="cn.edu.bztc.happyidiom.activity.StudyActivity"android:label="@string/title_actity_main"></activity><activity android:name="cn.edu.bztc.happyidiom.activity.StudyAnimalActivity"android:label="@string/title_actity_main"></activity></application><instrumentation android:name="android.test.InstrumentationTestRunner"android:targetPackage="cn.edu.bztc.happyidiom"></instrumentation></manifest>
(5)在test包下新建类DBOpenHelperTest,继承自AndroidTestCase,
package cn.edu.bztc.happyidiom.test;import cn.edu.bztc.happyidiom.db.DBOpenHelper;
import android.test.AndroidTestCase;public class DBOpenHelperTest extends AndroidTestCase {public void testDBCopy(){DBOpenHelper dbOpenHelper = new DBOpenHelper(getContext());dbOpenHelper.openDatabase();}}
(6)在DBOpenHelperTest类里右击选择Run as再选择Android JUnit Test进行单元测试,如果在JUnit状态栏里出现 绿色运行条,代码测试成功,如果为红色,则代码有错误。
(7)在切换到DDMS,选择所运行的虚拟机,在File Explorer下/data/data应用的所属项目下的databases下有没有idioms.db数据库,若有则成功。
本文标签: 乐学成语数据库创建(导入打开)
版权声明:本文标题:乐学成语——数据库创建(导入、打开) 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/jishu/1687327793h90026.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论