2013-04-22 122 views
0

我有源碼開放的幫手一個問題,我收到此錯誤:SQLite不打開數據庫在Android中

sqlite3_open_v2( 「/數據/數據/ lam.ztl.lamztlbologna /數據庫/ ztlBolo.db」 &句柄,2,NULL)失敗 無法打開數據庫。關閉它。 android.database.sqlite.SQLiteCantOpenDatabaseException:無法打開數據庫文件 我不明白爲什麼會收到此錯誤。

我打開輔助類是:

enter code here 



public class DBHelper extends SQLiteOpenHelper { 
    private final static String DATABASE_NAME = "ztlBolo.db"; 
    static SQLiteDatabase db; 
    //private static String DB_PATH = "/data/data/lam.ztl.lamztlbologna/databases/"; 
    static Context context; 
    private static String DB_PATH = "/data/data/"+context.getPackageName()+"/databases/"; 



    private final static String TABLE_NAME = "manageZtlStreet"; 
    private final static String SQL_PATH = "databaseZtlBolo.sql"; 
    MapsActivity ma = new MapsActivity(); 
    Double LATITUDE= 0.0; 
    Double LONGITUDE = 0.0; 
    private final Context myContext; 
    private final String CREATE_TABLE_ZTL_STREET = "CREATE TABLE manageZtlstreet (" 
      + " via text not null," 
      +"latitude DOUBLE," 
      + "longitude DOUBLE);"; 

    public DBHelper(Context context,int version) { 

     super(context, DBHelper.DATABASE_NAME,null,1); 
     this.myContext = context; 
    } 

    @Override 
    public void onCreate(SQLiteDatabase database) { 
     // database.openOrCreateDatabase(database.getPath(), null); 
     database=SQLiteDatabase.openDatabase(database.getPath(), null, SQLiteDatabase.NO_LOCALIZED_COLLATORS|SQLiteDatabase.OPEN_READWRITE|SQLiteDatabase.CREATE_IF_NECESSARY); 
     database.execSQL(this.CREATE_TABLE_ZTL_STREET); 

     //DB_PATH = db.getPath(); 
     try { 

      copyDataBase(database); 
      Log.e("copy","copy"); 

     } catch (IOException e) { 

      throw new Error("Error copying database"); 

     } 
     database.close(); 
    } 






    /** 
    * Copies your database from your local assets-folder to the just created empty database in the 
    * system folder, from where it can be accessed and handled. 
    * This is done by transfering bytestream. 
    * */ 
    private void copyDataBase(SQLiteDatabase db) throws IOException{ 


     //Open your local db as the input stream 
     InputStream myInput = myContext.getAssets().open(SQL_PATH); 
     /*leggo il file*/ 
     BufferedReader buffread = new BufferedReader(new InputStreamReader(myInput)); 
     String line = null; 

     while((line = buffread.readLine()) != null) { 

      //Log.e("line",""+line); 
      //db.execSQL(line);! 
      if(!line.equals(new String(""))){ 
       db.execSQL(line); 
      } 
     } 

     myInput.close(); 
    } 

     public void openDataBase() throws SQLException{ 
     SQLiteDatabase myDataBase = null; 
     String myPath = DATABASE_NAME; 
     myDataBase = SQLiteDatabase.openDatabase(DB_PATH+DATABASE_NAME, null, SQLiteDatabase.OPEN_READWRITE); 
     Log.e("path",""+myDataBase); 

    } 

    @Override 
    public synchronized void close() { 

     /* if(myDataBase != null) 
      myDataBase.close(); 
     */ 
     super.close(); 

    } 


    @Override 
    public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) { 
     database.execSQL("DROP TABLE IF EXISTS manageZtlStreet;"); 
     //database.close(); 
     this.onCreate(database); 
    } 


    public Cursor getZtlStreet(){ 
     String query = "select latitude ,longitude from manageZtlstreet"; 

     SQLiteDatabase rdb = getWritableDatabase(); 
     /** Cursor cursor=rdb.rawQuery(query,null); 
     rdb.close(); 
     return cursor;*/return null; 
    } 
+0

我想看看這個答案:http://stackoverflow.com/questions/6356170/how-should-i - 打開並關閉我的數據庫 - 正確 – HalR 2013-04-22 14:59:46

+0

你能寫出異常 – bmavus 2013-04-22 15:00:54

+0

我收到異常無法打開數據庫文件,我沒有一些異常 – Michele 2013-04-22 17:39:24

回答

1

你不應該直接使用文件ztlBolo.db。

壞主意:private static String DB_PATH = "/data/data/"+context.getPackageName()+"/databases/";

我的實現:

public class UtilDB { 

private static final String DATABASE_NAME = "cool_db.db"; 
private static final int DATABASE_VERSION = 1; 

private final String SQL_CREATE_MY_COOL_TABLE = "CREATE TABLE `cool_table` ..."; 


private SQLiteDatabase mDB = null; 
private MyDBHelper mDBHelper = null; 
private static UtilDB mInstance = null; 

//Using pattern singleton 
public static UtilDB getInstance(Context context) { 
    if (mInstance == null) { 
     mInstance = new UtilDB(context); 
    } 

    mInstance.open(); 

    return mInstance; 
} 

private UtilDB(Context context) {  
    mDBHelper = new MyDBHelper (context, DATABASE_NAME, null, DATABASE_VERSION); 
} 

private void open() throws SQLException { 
    //If connection to db is not open then will open connection 
    if ((mDB == null) || (!mDB.isOpen())) { 
     mDB = mDBHelper.getWritableDatabase(); 
    } 
} 

public void close() { 
    mDB.close(); 
} 


private class MyDBHelper extends SQLiteOpenHelper { 

    public MyDBHelper (Context context, String name, CursorFactory factory, int version) { 
     super(context, name, factory, version); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase _db) { 
     _db.execSQL(SQL_CREATE_MY_COOL_TABLE); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) { 
     _db.execSQL("DROP TABLE IF EXISTS \"cool_table\""); 

     onCreate(_db); 
    } 
} 

}