请我尝试从预加载的数据库创建登录。但我在复制数据库时遇到问题。以下是我的调试错误:
E / AndroidRuntime:致命异常:主要 过程:com.babbangona.preloadedtestingagain,PID:11863 java.lang.Error:复制数据库时出错 在com.babbangona.preloadedtestingagain.DBHandler.createDatabase(DBHandler.java:42) at com.babbangona.preloadedtestingagain.DBcopyActivity $ 1.onClick(DBcopyActivity.java:28) 在android.view.View.performClick(View.java:6199) 在android.view.View $ PerformClick.run(View.java:23235) 在android.os.Handler.handleCallback(Handler.java:836) 在android.os.Handler.dispatchMessage(Handler.java:103) 在android.os.Looper.loop(Looper.java:203) 在android.app.ActivityThread.main(ActivityThread.java:6251) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1073) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934)我/艺术: 输入while循环。我/艺术:输入while循环。断开了 目标VM,地址:'localhost:8600',transport:'socket'
我的DBHandler java文件:
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class DBHandler extends SQLiteOpenHelper{
String DB_PATH = null;
private static String DB_NAME = "rehoboth.db";
private SQLiteDatabase myDatabase;
private final Context myContext;
public DBHandler(Context context){
super(context, DB_NAME, null, 2);
this.myContext = context;
this.DB_PATH = "/data/data" + context.getPackageName() + "/databases/";
Log.e("Path 1", DB_PATH);
}
public void createDatabase() throws IOException {
boolean dbExist = checkDatabase();
if(dbExist){
/* openDatabase();
int cVersion = myDatabase.getVersion();
if(cVersion != 2){
onUpgrade(myDatabase, myDatabase.getVersion(),2);
close();
}*/
}else{
this.getReadableDatabase();
try{
copyDatabase();
}catch (IOException e){
throw new Error("Error copying database");
}
}
}
private boolean checkDatabase(){
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDatabase() throws IOException{
InputStream myInput = myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[10];
int length;
while((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDatabase() throws SQLException{
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){
if(newVersion > oldVersion)
try{
copyDatabase();
}catch (IOException e){
e.printStackTrace();
}
}
public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy){
return myDatabase.query("rehoboth", null, null, null, null, null, null);
}
}
我的主要活动:
import android.app.Activity;
import android.database.Cursor;
import android.database.SQLException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.IOException;
public class DBcopyActivity extends Activity {
Cursor c = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dbcopy);
((Button) findViewById(R.id.button1)).setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
DBHandler myDbHelper = new DBHandler(DBcopyActivity.this);
try{
myDbHelper.createDatabase();
}catch(IOException ioe){
throw new Error("Unable to create database");
}try{
myDbHelper.openDatabase();
}catch(SQLException sqle){
throw sqle;
}
Toast.makeText(DBcopyActivity.this, "Successfully Imported", Toast.LENGTH_SHORT).show();
c = myDbHelper.query("rehoboth", null, null, null, null, null, null);
if(c.moveToFirst()){
do{
Toast.makeText(DBcopyActivity.this,
"_id: " + c.getString(0) + "\n" +
"NAME: " + c.getString(1) + "\n" +
"PASSWORD: " + c.getString(2),
Toast.LENGTH_LONG).show();
}while (c.moveToNext());
}
}
});
}
}