2011-03-18 101 views
0

你好我開發Android手機一個簡單的應用程序,我創建第一次被工作,但我想在其他項目箱同樣的事情,但我無法做到讓錯誤,如無法打開數據庫Android的數據庫連接

package com.exampleHelloText; 

import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
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.os.Environment; 
import android.util.Log; 

public class DataBaseHelper extends SQLiteOpenHelper{ 
    //private static String DB_PATH = "/data/data/com.drager/databases/"; 
    private static String DB_PATH = Environment.getDataDirectory()+"/data/com.exampleHelloText/databases/"; 
    final static String DB_NAME = "myDBName"; 
    private SQLiteDatabase myDataBase=null; 
    private final Context myContext; 
    private DataBaseHelper myDbHelper; 
    private static String TAG ="MyActivity"; 

    public DataBaseHelper(Context context){ 
     super(context, DB_NAME, null, 1); 
     this.myContext = context; 

    } 

    public DataBaseHelper createDataBase() throws IOException{ 
     boolean dbExist =checkDataBase(); 
     //SQLiteDatabase db_read =null; 
     Log.i(TAG,"############value of dbExist"+dbExist+"##########"); 
     if (dbExist){ 
      //db must exist 
     } 
     else{ 
     myDbHelper = new DataBaseHelper(myContext); 
     myDataBase = myDbHelper.getReadableDatabase(); 
     myDataBase.close(); 
     //this.getReadableDatabase(); 
      //db_read.close(); 

      try { 
       copyDataBase(); 
      } catch (IOException e) { 
       throw new Error("error copying database"); 
      } 
     } 
     return this; 

    } 



    public void copyDataBase() throws IOException{ 
     // open db as input stream 
     InputStream myInput; 
     //open empty db as output stream 
     OutputStream myOutPut; 
     try { 
      myInput = myContext.getAssets().open(DB_NAME); 

      //path to newly created db 
      String outFileName =DB_PATH + DB_NAME; 

      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); 
      } 
      myOutPut.flush(); 
      myOutPut.close(); 
      myInput.close(); 
      } 
     catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 




    } 

    private boolean checkDataBase() { 
     SQLiteDatabase checkDB = null; 

     String myPath = DB_PATH + DB_NAME; 

     try { 
      checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE); 
     } catch (SQLException e) { 

      e.printStackTrace(); 
      return false; 
     } 

     if (checkDB != null){ 
      checkDB.close(); 
     } 
     return true; 
     //return checkDB !=null ? true : false; 
    } 

    public void openDataBase()throws SQLException{ 
     //open the database 
     String myPath = DB_PATH + DB_NAME; 
     myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE); 
    } 

    @Override 
    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) { 
     // TODO Auto-generated method stub 

    } 
} 

請幫我提前

+0

您的代碼將要求copyDataBase()首先運行..是什麼呢?你是否證實數據庫確實存在於指定的位置?這也有助於查看引發錯誤的代碼。你有沒有堆棧跟蹤?另外,要在openDatabase()中獲取數據庫對象,可以調用myDbHelper.getWritableDatabase()來獲取SQLiteDatabase對象。 – rogerkk 2011-03-18 11:01:14

回答

0

感謝您的的onCreateonUpgrade方法體爲空。
確保您正在調用您的方法createDataBase()並通過myDataBase獲取數據庫。

+0

但他有一個copyDatabase()方法,他正在複製一個現有的數據庫。如果他實際運行該方法目前不容易說明。 – rogerkk 2011-03-18 11:03:21

+0

我接受他正在使用標準方法。但你有權利,我的假設是不正確的。 – pawelzieba 2011-03-18 11:26:59

0

對於數據庫,你可以參考this ..下載Zip文件...