我遇到sqlite open helper的问题我收到此错误:
sqlite3_open_v2(“/ data / data / lam.ztl.lamztlbologna / databases / ztlBolo.db”,& handle,2,NULL)失败 无法打开数据库。关闭它。 android.database.sqlite.SQLiteCantOpenDatabaseException:无法打开数据库文件 我不明白为什么会收到这个错误。
My Open助手类是:
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 :(得分: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);
}
}
}