在android我想要把一个9M的数据库能够在手机上使用(模拟器上可以用)该怎么做 我连找带查写了一些代码但是失败 望大家能够帮我

chensir_play 2012-03-15 01:55:17
只是我test页面的代码
srbt.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
query();
// srDb.close();
}
});
}
private void query()
{

try{
db.createDataBase();
srDb=db.getWritableDatabase();
// Cursor cs=srDb.query(false, null, null, null, null, null, null, null, null);
String str=srText.getText().toString();
String sql = "Select * from chengyucidian where chengyu='"+str+"'";
Cursor cs=srDb.rawQuery(sql, null);
cs.moveToFirst();
srText.setText(cs.getString(cs.getColumnIndex("pinyin")));
System.out.println(cs.getString(cs.getColumnIndex("pinyin")));
//srDb=new copyDB(this);
// srDb=SQLiteDatabase.openOrCreateDatabase("data/data/practice.content/data.db", null);
/*
//srDb=SQLiteDatabase.openOrCreateDatabase("/assets/data.db", null);
String str=srText.getText().toString();
String sql = "Select * from chengyucidian where chengyu='"+str+"'";
Cursor cr=srDb.rawQuery(sql, null);
cr.moveToFirst();
//for(int i=0;i<cr.getCount()-1)
srText.setText(cr.getString(cr.getColumnIndex("pinyin")));*/
}catch(Exception e){
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
System.out.println(e.toString());
}

这是复制数据库到SDK上的代码 copyDB.class的代码
package practice.content;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;
import android.widget.Toast;

public class copyDB extends SQLiteOpenHelper{

private static final int DB_VERSION=1;
private static final String DB_PATH="data/data/practice.content/";

// private static String DB_PATH=Environment.getExternalStorageDirectory()+"/data.db";
private static final String DB_NAME="data.db";
private static final String ASSETS_NAME="data.db";
private SQLiteDatabase mydb=null;
private Context mycontext;

private final int ASSETS_START=101;
private final int ASSETS_END=110;

public copyDB(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
this.mycontext=context;
}
public copyDB(Context context, String name,int version)
{
this(context,name,null,version);
}
public copyDB(Context context, String name)
{
this(context,name,DB_VERSION);
}
public copyDB(Context context)
{
this(context,DB_PATH+DB_NAME);
}
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist){
//数据库已存在,do nothing.
}else{
//创建数据库
try {
File dir = new File(DB_PATH);
if(!dir.exists()){
dir.mkdirs();
}
File dbf = new File(DB_PATH + DB_NAME);
if(dbf.exists()){
dbf.delete();
}
SQLiteDatabase.openOrCreateDatabase(dbf, null);
// 复制asseets中的db文件到DB_PATH下
copyDataBase();
} catch (IOException e) {
// throw new Error("数据库创建失败");
// Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
System.out.println(e+"");
}
}
}
private boolean checkDataBase(){
SQLiteDatabase checkDB = null;
String myPath = DB_PATH + DB_NAME;
try{
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
//database does't exist yet.
System.out.println(e+"");
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}

private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = mycontext.getAssets().open(ASSETS_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
//复制assets下的大数据库文件时用这个
private void copyBigDataBase() throws IOException{
InputStream myInput;
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
for (int i = ASSETS_START; i < ASSETS_END+1; i++) {
myInput = mycontext.getAssets().open(ASSETS_NAME + "." + i);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myInput.close();
}
myOutput.close();
}
@Override
public synchronized void close() {
if(mydb != null){
mydb.close();
}
super.close();
}




@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub

}

@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub

}

}
...全文
79 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
mikebai 2012-03-15
  • 打赏
  • 举报
回复
sdk2.1???好像
不支持sd卡数据库

DB_PATH 是存放到哪里了????????????

保存到sdcard的话,非要这么做的话,就在程序启动copydb的处理中判断客户端版本,低于2.1的直接拷贝到data包下面,还有assets下的资源文件不能大于1MB,你要分割拷贝

80,493

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧