package com.yu.testSQL;import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import android.content.Context;public class ProvinceDB {
Context mContext;
public ProvinceDB(Context context){
this.mContext=context;
}
private final static String DATABASE_PATH = android.os.Environment
.getExternalStorageDirectory().getAbsolutePath() + "/testsql";
private final static String DATABASE_FILENAME = "testsql.db"; public void openDB(){
try{
String databaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME;
File dir = new File(DATABASE_PATH);
if (!dir.exists())
//exists 存在的
dir.mkdir();
//判断数据库文件是否存在,若不存在则执行导入,否则直接打开数据库
if(!(new File(databaseFilename)).exists())
{
//欲导入的数据库
InputStream is = mContext.getResources()
.openRawResource(R.raw.testsql);
//每次都这里报错,说是找不到文件还是什么的!明明在raw文件夹下面有那个数据库文件,真心郁闷
FileOutputStream fos = new FileOutputStream(databaseFilename);
byte[] buffer = new byte[8192];
int count = 0;
while((count=is.read(buffer))>0){
fos.write(buffer, 0, count);
}
fos.close();
is.close();
}
}catch(Exception e){
e.printStackTrace();
}
}}
android数据库
import java.io.FileOutputStream;
import java.io.InputStream;
import android.content.Context;public class ProvinceDB {
Context mContext;
public ProvinceDB(Context context){
this.mContext=context;
}
private final static String DATABASE_PATH = android.os.Environment
.getExternalStorageDirectory().getAbsolutePath() + "/testsql";
private final static String DATABASE_FILENAME = "testsql.db"; public void openDB(){
try{
String databaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME;
File dir = new File(DATABASE_PATH);
if (!dir.exists())
//exists 存在的
dir.mkdir();
//判断数据库文件是否存在,若不存在则执行导入,否则直接打开数据库
if(!(new File(databaseFilename)).exists())
{
//欲导入的数据库
InputStream is = mContext.getResources()
.openRawResource(R.raw.testsql);
//每次都这里报错,说是找不到文件还是什么的!明明在raw文件夹下面有那个数据库文件,真心郁闷
FileOutputStream fos = new FileOutputStream(databaseFilename);
byte[] buffer = new byte[8192];
int count = 0;
while((count=is.read(buffer))>0){
fos.write(buffer, 0, count);
}
fos.close();
is.close();
}
}catch(Exception e){
e.printStackTrace();
}
}}
android数据库
解决方案 »
- XmlPullParserException 异常
- 关于集合遍历 问题。。。
- Android应用程序能否监听到手机有i/o数据传输与传输完毕的事件
- 求教:帮忙看看 android-serialport-api 要怎么下载源码下来
- Android ImageView 动态刷新显示图片 速度 渲染等问题
- 寻找安卓通过webservice远程连接sqlserver 实现数据读取实例或讲解
- VS 2013装好xamarin后为什么Xamarin.Android工具栏没有显示可用设备
- Android fragment退出view时怎么设置退出动画?求救...
- android studio AVD虚拟机启动不了
- opencv小白请教一个问题
- androidpn客户端连接服务端出现异常
- 请问myeclipse 8.5怎么安装android插件?
.openRawResource(R.raw.testsql);一个是流 一个是文件 你是否要转化一下?
{
//欲导入的数据库
InputStream is = mContext.getResources()
.openRawResource(R.raw.testsql);
//还执行下面一句,怎么想的
FileOutputStream fos = new FileOutputStream(databaseFilename);