2013-04-22 91 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); 
    } 
} 

}