我不明白为什么我无法将我的db文件(index.db)复制到应用程序目录“/data/data/com.cmc.sqlitedb/databases/”这是我的DataBaseHelper类< / p>
package com.cmc.sqlitedb;
import java.io.*;
import java.util.Arrays;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import android.content.Context;
import android.content.res.AssetManager;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DataBaseHelper extends SQLiteOpenHelper{
//The Android's default system path of your application database.
private static String DB_PATH = "/data/data/com.cmc.sqlitedb/databases/";
private static String DB_NAME = "index.db";
private SQLiteDatabase myDataBase;
//private final Context myContext;
private final Context Ctxt;
/**
* Constructor
* Takes and keeps a reference of the passed context in order to access to the application assets and resources.
* @param context
*/
public DataBaseHelper(Context context) {
super(context, DB_NAME, null, 1);
this.Ctxt = context;
}
/**
* Creates a empty database on the system and rewrites it with your own database.
* */
public void createDataBase() throws IOException
{
File Path = Ctxt.getDir("Data", 0);
File DBFile = new File(Path, "Index.db");
Log.v("4","four");
boolean dbExist = checkDataBase();
if(dbExist)
{
Log.v("5","five");
//do nothing - database already exist
}
else
{
Log.v("6","six");
//By calling this method and empty database will be created into the default system path
//of your application so we are gonna be able to overwrite that database with our database.
this.getReadableDatabase();
try
{
Log.v("7","seven");
//copyDataBase(Ctxt, DBFile);
copyDataBase();
Log.v("copyDataBase","after copyDataBase");
}
catch (IOException e)
{
throw new Error("Error copying database");
}
}
}
/**
* Check if the database already exist to avoid re-copying the file each time you open the application.
* @return true if it exists, false if it doesn't
*/
private boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
//database does't exist yet.
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
/**
* Copies your database from your local assets-folder to the just created empty database in the
* system folder, from where it can be accessed and handled.
* This is done by transfering bytestream.
* */
private void copyDataBase() throws IOException{
Log.v("8","eight");
//Open your local db as the input stream
InputStream myInput = Ctxt.getAssets().open(DB_NAME);
BufferedInputStream bis = new BufferedInputStream(myInput);
// InputStream myInput = myContext.getResources().openRawResource(R.raw.freq_app);
Log.v("9","nine");
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
Log.v("DB_PATH",DB_NAME);
//Open the empty db as the output stream
File f = new File( DB_PATH );
if ( !f.exists() )
f.mkdir();
OutputStream myOutput = new FileOutputStream(outFileName);
//.createNewFile();
Log.v("OutputStream","OutputStream");
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[6624256];
int length;
这里它在while循环IOexception中抛出错误
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myInput.close();
Log.v("COPY DATA BASE","end of copyDataBase");
}
public void openDataBase() throws SQLException{
//Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close() {
if(myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
// Add your public helper methods to access and get content from the database.
// You could return cursors by doing "return myDataBase.query(....)" so it'd be easy
// to you to create adapters for your views.
}
main class :
package com.cmc.sqlitedb;
import java.io.IOException;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.cmc.sqlitedb.DataBaseHelper;
import com.cmc.sqlitedb.R;
public class SqLiteTest extends Activity {
/** Called when the activity is first created. */
private SQLiteDatabase db;
private TextView Colors;
private Button click;
private String result1=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
db = openOrCreateDatabase(
"index.db"
, SQLiteDatabase.CREATE_IF_NECESSARY
, null
);
Log.v("size of db", ""+db.getMaximumSize());
DataBaseHelper myDbHelper = new DataBaseHelper(this);
myDbHelper = new DataBaseHelper(this);
try
{
myDbHelper.createDataBase();
Log.v("done", "Done");
}
catch (IOException ioe)
{
throw new Error("Unable to create database");
}
setContentView(R.layout.main);
Colors=(TextView)findViewById(R.id.color);
click=(Button)findViewById(R.id.Button);
click.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
try{
//code to connect db and get data frm db
result1=colorDetails();
Log.v("Result1",""+result1);
}
catch(Exception e)
{
System.out.println(e);
}
db.close();
}
});
}
public String colorDetails()
{
Log.v("Reading", "reading");
Cursor cur = db.rawQuery("select * from chip LIMIT 20",null);
Log.v("Reading ok", "reading ok");
Log.v("",""+cur.getString(9));
return result1;
}
}
答案 0 :(得分:1)
private void copyDataBase(Context myContext) throws IOException {
File fileTest = myContext.getFileStreamPath(DB_NAME);
boolean exists = fileTest.exists();
if (!exists) {
// Open the empty db as the output stream
OutputStream databaseOutputStream = new FileOutputStream(DB_PATH + DB_NAME);
InputStream databaseInputStream;
databaseInputStream = myContext.getAssets().open(DBpart1);
byte[] buffer = new byte[1024];
int length;
while ((length = databaseInputStream.read(buffer)) > 0) {
databaseOutputStream.write(buffer);
}
databaseInputStream.close();
databaseInputStream = myContext.getAssets().open(DBpart2);
while ((length = databaseInputStream.read(buffer)) > 0) {
databaseOutputStream.write(buffer);
}
// Close the streams
databaseInputStream.close();
databaseOutputStream.flush();
databaseOutputStream.close();
}
}