我在我的应用程序中使用sqlite db它几乎每次运行但有时它会给我错误nosuchtablefoundexception,当我从sdcard拉出数据库时它只显示我带有android_metadeta表的空数据库。如果没有复制错误或任何其他异常,它是如何可能的。 我将我的数据库存储在sdcard位置而不是/ data / data文件夹中。 在哪里我可能会弄错,如果有人可以找到。
这是我的dbhelper代码
public static String DB_PATH =Environment.getExternalStorageDirectory()+Constants.DB_PATH;
public static String DB_NAME = "local_db.sqlite";
public static SQLiteDatabase _database;
private final Context myContext;
public static String apstorphe = "'";
public static String sep = ",";
private static final int DB_VERSION_BUILD_1 = 1;
private static final int DB_VERSION_BUILD_2 = 2;
public DatabaseHelper(Context context)
{
super(context, DB_NAME, null, DB_VERSION_BUILD_2);
this.myContext = context;
DB_PATH = Environment.getExternalStorageDirectory()+Constants.DB_PATH;
File fDbDir = new File(DB_PATH);
if(!fDbDir.exists())
fDbDir.mkdirs();
}
/**
* Creates a empty database on the system and rewrites it with your own database.
* */
public void createDataBase() throws IOException
{
boolean dbExist = checkDataBase();
/*SQLiteDatabase db_Read = null;*/
if(!dbExist)
{
//By calling this method and empty database will be created into the default system path
//of your application so we are gonna be able to overwrite that database with our database.
/*db_Read = this.getReadableDatabase();
db_Read.close();*/
try
{
copyDataBase();
}
catch (IOException e)
{
//throw new Error("Error copying database");
e.printStackTrace();
}
}
}
/**
* Check if the database already exist to avoid re-copying the file each time you open the application.
* @return true if it exists, false if it doesn't
*/
private boolean checkDataBase()
{
SQLiteDatabase checkDB = null;
try
{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
catch(SQLiteException e)
{
//database does't exist yet.
}
if(checkDB != null)
{
checkDB.close();
}
return checkDB != null ? true : false;
}
/**
* copy database file from assets to the application directory
* @throws IOException
*/
private void copyDataBase() throws IOException
{
//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();
}
/**
* open the connection to database
* @return
* @throws SQLException
*/
public SQLiteDatabase openDataBase() throws SQLException
{
//Open the database
if(_database == null)
{
_database = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null, SQLiteDatabase.OPEN_READWRITE
| SQLiteDatabase.CREATE_IF_NECESSARY);
this.getWritableDatabase();
}
else if(!_database.isOpen())
{
_database = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null, SQLiteDatabase.OPEN_READWRITE
| SQLiteDatabase.CREATE_IF_NECESSARY);
}
return _database;
}
/**
* close connection to database
*/
public static void closedatabase()
{
if(_database != null)
_database.close();
}
@Override
public void onCreate(SQLiteDatabase db)
{
Log.e("DB onCreate","db :"+db.getVersion());
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.e("DB Version Upgraded","From old version : "+oldVersion+" to : "+newVersion);
String upgradeQueryTblOne = "ALTER TABLE "+DbTbls.TBL_ONE+" ADD COLUMN m_type VARCHAR DEFAULT '0'";
String upgradeQueryTblTwo = "ALTER TABLE "+DbTbls.TBL_TWO+" ADD COLUMN f_type VARCHAR DEFAULT '0'";
if (oldVersion == DB_VERSION_BUILD_1_34 && newVersion == DB_VERSION_BUILD_1_35)
{
if(_database != null)
{
_database.execSQL(upgradeQueryTblOne);
_database.execSQL(upgradeQueryTblTwo);
}
}
}
public static boolean deleteDir(File dir)
{
if (dir.isDirectory())
{
String[] children = dir.list();
for (int i=0; i<children.length; i++)
{
boolean success = deleteDir(new File(dir, children[i]));
if (!success)
{
return false;
}
}
}
// The directory is now empty so delete it return dir.delete(); }
return dir.delete();
}