我正在创建一个使用现有 sqlite 数据库的字典应用程序。我已将数据库放置在资产文件夹中,并在应用程序首次启动时使用以下代码来复制数据库。 (我借用了这个想法this http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/ post)
数据库助手.java
public class DatabaseHelper {
private static String DB_PATH = "";
private static String DB_NAME = "abc.sqlite";
private SQLiteDatabase myDatabase;
private Context myContext;
public DatabaseHelper(Context context) {
myContext = context;
if (android.os.Build.VERSION.SDK_INT >= 17)
DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
else
DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
Log.d("path", DB_PATH);
}
public void copyDatabase() {
InputStream myInput;
OutputStream outStream;
try {
myInput = myContext.getAssets().open(DB_NAME);
String file = DB_PATH + DB_NAME;
outStream = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int length = 0;
while ((length = myInput.read(buffer)) >= 0) {
outStream.write(buffer, 0, length);
}
outStream.flush();
myInput.close();
outStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
然后我调用上面的copyDatabase()
方法形成我的MainActivity
。这是代码。
public class MainActivity extends Activity {
DatabaseHelper myDbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
myDbHelper = new DatabaseHelper(getApplicationContext());
myDbHelper.copyDatabase();
}
}
但问题是Android不复制数据库。我使用了 Log 工具,发现 while 循环从未运行。
这段代码有什么问题。
谢谢。
如下更改 while 循环:
除了你的条件之外>=
大于或等于将其更改为仅>
比...更棒
while ((length = myInput.read(buffer)) > 0) {
outStream.write(buffer, 0, length);
}
尝试下面的代码,它会像魅力一样为您工作。
public class DataBaseHelper extends SQLiteOpenHelper {
private Context mycontext;
private String DB_PATH;
private static String DB_NAME = "abc.sqlite";
public SQLiteDatabase myDataBase;
public DataBaseHelper(Context context) throws IOException {
super(context,DB_NAME,null,1);
this.mycontext=context;
boolean dbexist = checkdatabase();
if (dbexist) {
opendatabase();
} else {
System.out.println("Database doesn't exist");
createdatabase();
}
}
public void createdatabase() throws IOException {
boolean dbexist = checkdatabase();
if(!dbexist) {
this.getReadableDatabase();
try {
copydatabase();
} catch(IOException e) {
throw new Error("Error copying database");
}
}
}
private boolean checkdatabase() {
boolean checkdb = false;
try {
String myPath = DB_PATH + DB_NAME;
File dbfile = new File(myPath);
checkdb = dbfile.exists();
} catch(SQLiteException e) {
System.out.println("Database doesn't exist");
}
return checkdb;
}
private void copydatabase() throws IOException {
//Open your local db as the input stream
InputStream myinput = mycontext.getAssets().open(DB_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 byte to inputfile to 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();
}
public void opendatabase() throws SQLException {
//Open the database
String mypath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(mypath, null, SQLiteDatabase.OPEN_READWRITE);
}
public synchronized void close() {
if(myDataBase != null) {
myDataBase.close();
}
super.close();
}
}
In your MainActivity
您现在需要创建您的实例DatabaseHelper
其他班级将自行管理。
public class MainActivity extends Activity {
DatabaseHelper myDbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
myDbHelper = new DatabaseHelper(MainActivity.this);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)