我创建了一个" sqlite"数据库使用Java应用程序,我将数据插入到数据库的表中。我也用sqliteStudio检查了它。我将数据库复制到Android应用程序的资产文件夹中以使用它。但是,当我尝试从表格中选择数据时,它给了我一个例外"没有这样的表OSOLDB"。我按照this answer打开现有数据库,这是我的代码:
public class DBHelper extends SQLiteOpenHelper{
private static final String DBName="OSOL";
private static final String TABLE_NAME="OSOLDB";
private static final String PARAGRAPH_ID_COL="ID";
private static final String PARAGRAPH_COL="PAR";
private static File DATABASE_FILE;
private static boolean InvalidDB=false;
private static final int VERSION = 1;
private static DBHelper myHelper;
private Context context;
private static String DB_PATH="";
private SQLiteDatabase DB;
public DBHelper(Context context) {
super(context, DBName, null, VERSION);
// TODO Auto-generated constructor stub
Log.d("DBHELPER","constructor");
if(android.os.Build.VERSION.SDK_INT >= 17){
DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
}
else
{
DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
}
this.context = context;
}
public void createDataBase() throws IOException
{
//If database not exists copy it from the assets
boolean mDataBaseExist = checkDataBase();
if(!mDataBaseExist)
{
this.getReadableDatabase();
this.close();
try
{
//Copy the database from assests
copyDataBase();
Log.e("DBHELPER", "createDatabase database created");
}
catch (IOException mIOException)
{
throw new Error("ErrorCopyingDataBase");
}
}
}
//Open the database, so we can query it
public boolean openDataBase() throws SQLException
{
String mPath = DB_PATH + DBName;
DB = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.CREATE_IF_NECESSARY);
//mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
return DB != null;
}
private boolean checkDataBase()
{
File dbFile = new File(DB_PATH + DBName);
Log.v("learn", dbFile.getName() + " "+ dbFile.exists());
return dbFile.exists();
}
/*
synchronized static DBHelper getInstance( Context context)
{
if(myHelper==null)
{
myHelper= new DBHelper(context.getApplicationContext());
}
return myHelper;
}*/
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
InvalidDB=true;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
InvalidDB=true;
//isUpgraded=true;
}
@Override
public synchronized void onOpen(SQLiteDatabase db)
{
super.onOpen(db);
if(!db.isReadOnly())
{
DATABASE_FILE = context.getDatabasePath(DBName);
}
}
@Override
public synchronized void close()
{
super.close();
}
//Copy the database from assets
private void copyDataBase() throws IOException
{
InputStream mInput = context.getAssets().open(DBName);
String outFileName = DB_PATH + DBName;
OutputStream mOutput = new FileOutputStream(outFileName);
byte[] mBuffer = new byte[1024];
int mLength;
while ((mLength = mInput.read(mBuffer))>0)
{
mOutput.write(mBuffer, 0, mLength);
}
mOutput.flush();
mOutput.close();
mInput.close();
}
}
和此:
public class DataAdapter {
private final Context context;
private SQLiteDatabase DB;
private DBHelper helper;
public DataAdapter(Context contex)
{
this.context=contex;
helper= new DBHelper(contex);
}
public DataAdapter createDatabase() throws SQLiteException, IOException
{
helper.createDataBase();
return this;
}
public DataAdapter open() throws SQLException
{
try
{
helper.openDataBase();
helper.close();
DB = helper.getReadableDatabase();
Log.d("adapter","shiiiiiit");
}
catch (SQLException mSQLException)
{
Log.e("dataAdapter", "open >>"+ mSQLException.toString());
throw mSQLException;
}
return this;
}
public void close()
{
helper.close();
}
public Cursor getTestData()
{
try
{
String sql ="SELECT * FROM OSOLDB";
Cursor mCur = DB.rawQuery(sql, null);
if (mCur!=null)
{
mCur.moveToNext();
}
return mCur;
}
catch (SQLException mSQLException)
{
Log.e("dataAdapter", "getTestData >>"+ mSQLException.toString());
throw mSQLException;
}
}
}
为什么即使表存在,我仍然会获得异常?