2012-08-28 56 views
0

我想要做的是将我通过SQLite管理器创建的数据库复制到我的应用程序中。数据库副本不起作用

我已检查数据库确实已创建,但数据库只有3 KB(我的数据库没有被复制)。该setupdb的代码from here

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()); 
      } 
} 

*/ 

} 
+0

试试这个:HTTP://stackoverflow.com/questions/9109438/how-to-use-existing-database-with-android-app/9109728#9109728 –

+0

@YaqubAhmad感谢阿古柏 – dreamer1989

回答

0

这对我启动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(); 
    } 
+0

先生,这是修改代码 – dreamer1989

+0

而不是调用dbobj.getReadableDatabase();调用open方法并返回该对象。 – wtsang02

+0

与变化我得到一个致命的异常 – dreamer1989