使用此代码解决您的问题
/**
* The Class DataBaseHelper.
*/
public class DataBaseHelper extends SQLiteOpenHelper {
/** The D b_ path. */
private final String DB_PATH = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/folder/";
/** The D b_ name. */
private static String DB_NAME = "abc.db";
/** The my data base. */
private SQLiteDatabase myDataBase;
/** The my context. */
private final Context myContext;
/**
* Constructor Takes and keeps a reference of the passed context in order to
* access to the application assets and resources.
*
* @param context
* the context
*/
public DataBaseHelper(Context context) {
super(context, DB_NAME, null, 1);
myContext = context;
createDataBase();
}
/**
* Creates a empty database on the system and rewrites it with your own
* database.
* */
public void createDataBase() {
boolean dbExist = checkDataBase();
if (!dbExist) {
getWritableDatabase();
try {
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_READWRITE);
} catch (SQLiteException e) {
e.printStackTrace();
}
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.
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
private void copyDataBase() throws IOException {
File f=new File(DB_PATH);
f.mkdirs();
InputStream myInput = myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
/**
* Open data base.
*
* @return the sQ lite database
* @throws SQLException
* the sQL exception
*/
public SQLiteDatabase openDataBase() throws SQLException {
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
return myDataBase;
}
/*
* (non-Javadoc)
*
* @see android.database.sqlite.SQLiteOpenHelper#close()
*/
@Override
public synchronized void close() {
if (myDataBase != null)
myDataBase.close();
super.close();
}
/*
* (non-Javadoc)
*
* @see
* android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite
* .SQLiteDatabase)
*/
@Override
public void onCreate(SQLiteDatabase db) {
}
/*
* (non-Javadoc)
*
* @see
* android.database.sqlite.SQLiteOpenHelper#onUpgrade(android.database.sqlite
* .SQLiteDatabase, int, int)
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}