Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 数据库副本不工作_Android_Sqlite - Fatal编程技术网

Android 数据库副本不工作

Android 数据库副本不工作,android,sqlite,Android,Sqlite,我试图做的是复制我在应用程序中通过SQLite manager创建的数据库 我已经检查了数据库是否被创建,但数据库只有3KB(我的数据库没有被复制)。setupdb的代码是 Setupdb.java public class Setupdb extends SQLiteOpenHelper { private static String DB_PATH = ""; private static final String DB_NAME = "fergi.sqlite"; p

我试图做的是复制我在应用程序中通过SQLite manager创建的数据库

我已经检查了数据库是否被创建,但数据库只有3KB(我的数据库没有被复制)。setupdb的代码是

Setupdb.java

public class Setupdb extends SQLiteOpenHelper {

  private static String DB_PATH = "";
    private static final String DB_NAME = "fergi.sqlite";
    private SQLiteDatabase myDataBase;
    private final Context myContext;

    private static Setupdb mDBConnection;


public Setupdb(Context context) {
    super(context, DB_NAME, null, 1);
    this.myContext=context;
    DB_PATH="/data/data/"
          + context.getApplicationContext().getPackageName()
          + "/databases/";
    Log.e(DB_NAME, DB_PATH);
}
public static synchronized Setupdb getDBAdapterInstance(Context context) {
  if (mDBConnection == null) {
      mDBConnection = new Setupdb(context);
  }
     return mDBConnection;
}

    public void createDataBase() throws IOException {
        boolean dbExist = checkDataBase();
        if (dbExist) {
            Log.e("db","exist");
            // do nothing - database already exist
        } else {
            // By calling following method
            // 1) an empty database will be created into the default system path of your application
            // 2) than we overwrite that database with our database.
            this.getReadableDatabase();
            try {
                Log.e("calling", "copy");
                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) {
            // database does't exist yet.
        }
        if (checkDB != null) {
            checkDB.close();
        }
        return checkDB != null ? true : false;
    }

    private void copyDataBase() throws IOException {
        Log.e("copy", "coppying db");
      // 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 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();
  } 


    public void openDataBase() throws SQLException {
        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();
    }

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

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {


}
 }
MainActivity.java

public class MainActivity extends Activity {
String CNAME=" ques",TABLE_NAME=" JAVAQ";
Context context;
Setupdb  myDbHelper;



/* public MainActivity(Context someContext){
     context=someContext;
     startSQL();
 }  */

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    context=this;
    startSQL();

}




 private void startSQL() {
        myDbHelper = new Setupdb(context);
        try {

            myDbHelper.createDataBase();

        } catch (IOException ioe) {

            throw new Error(ioe);

        }

        open();
    }
    public void open()throws SQLException{
        myDbHelper.openDataBase();
    }





   /* 

    Setupdb dbobj=new Setupdb(this); 

    SQLiteDatabase sqobj=dbobj.getWritableDatabase();


    try {
        dbobj.createDataBase();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace(); 
    }  
   try{
   String query = "SELECT " + CNAME + " FROM " + TABLE_NAME+ " WHERE"+" _id=1";
   Log.e("ghgh", query);
            Cursor c2 = sqobj.rawQuery(query, null);

            while (c2.moveToNext()) {

            String name =
            c2.getString(c2.getColumnIndex(CNAME));
            Log.i("LOG_TAG", " HAS NAME " + name);
            }}
            catch(Exception e){

                Log.e("err", e.toString());
            }
}

*/

}

这是我启动sql的代码,我们拥有的数据库类是相同的

public YOURCLASS(Context someContext){
        context=someContext;
        startSQL();
    }    
private void startSQL() {
        myDbHelper = new DataBaseHelper(context);
        try {

            myDbHelper.createDataBase();

        } catch (IOException ioe) {

            throw new Error(ioe);

        }

        open();
    }
    public void open()throws SQLException{
        myDbHelper.openDataBase();
    }

而不是调用dbobj.getReadableDatabase();调用open方法并返回该对象。通过更改,我得到了一个致命的例外Tanks先生,通过您的代码,我的数据库被复制了,但仍然给了我errorsTry这个:@YaqubAhmad谢谢yaqub