Android 无法从SQLite数据库查询:没有此类表错误
我正在尝试复制SQLite数据库。Android 无法从SQLite数据库查询:没有此类表错误,android,sqlite,android-sqlite,Android,Sqlite,Android Sqlite,我正在尝试复制SQLite数据库。 复制数据库的代码为: public class DBHelper extends SQLiteOpenHelper { private static final String DB_PATH="/data/data/com.nirmalam.vaishnavismeclass.mydatabaseapplication/databases/"; private static final String DB_NAME="vaishnavisme
复制数据库的代码为:
public class DBHelper extends SQLiteOpenHelper {
private static final String DB_PATH="/data/data/com.nirmalam.vaishnavismeclass.mydatabaseapplication/databases/";
private static final String DB_NAME="vaishnavismeclass.db";
private static final int SCHEMA_VERSION=1;
public SQLiteDatabase dbSQLite;
private final Context myContext;
public DBHelper(Context context){
super(context, DB_NAME, null, SCHEMA_VERSION);
this.myContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if(newVersion>oldVersion)
copyDBFromResource();
}
public void createDatabase(){
createDB();
}
private void createDB() {
boolean dbExist = DBExist();
if (!dbExist){
this.getReadableDatabase();
copyDBFromResource();
}
}
private void copyDBFromResource() {
InputStream inputStream = null;
OutputStream outputStream = null;
String dbFilePath = DB_PATH + DB_NAME;
try{
inputStream = myContext.getAssets().open(DB_NAME);
outputStream = new FileOutputStream(dbFilePath);
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer))>0){
outputStream.write(buffer,0,length);
}
outputStream.flush();
outputStream.close();
inputStream.close();
}
catch (IOException e) {
e.printStackTrace();
throw new Error("Problem copying data to storage");
}
}
private boolean DBExist(){
SQLiteDatabase db=null;
try {
String databasePath = DB_PATH + DB_NAME;
db = SQLiteDatabase.openOrCreateDatabase(databasePath, null);
db.setLocale(Locale.getDefault());
db.setVersion(SCHEMA_VERSION);
}
catch (SQLiteException e){
Log.e("VC","SQLite Database not found");
}
if (db != null){
db.close();
}
return db !=null ?true :false;
}
@Override
public synchronized void close(){
if (dbSQLite != null){
dbSQLite.close();
}
super.close();
}
}
使用Android设备监视器,我可以看到在/data/data//databases
当我尝试使用以下代码执行查询以检索数据时
public Cursor getSinglePasuram(int id){
Cursor mCursor;
String databasePath = DB_PATH + DB_NAME;
// SQLiteDatabase db = this.getReadableDatabase();
SQLiteDatabase db = SQLiteDatabase.openDatabase(databasePath, null,SQLiteDatabase.OPEN_READONLY);
Log.d("VC","db is "+db.getPath()+db.getVersion());
mCursor = db.rawQuery("select * from pasurams where _id = 1", null);
return mCursor;
}
获取运行时错误,因为找不到表PASURAM。同一查询在SQLite数据库中独立运行良好
这个代码有什么问题?非常感谢您的帮助。应用程序使用的数据库与您在SQLItemManager中查询的数据库不同。
您可能在第一次运行后添加了表 当它现在运行时,它已经找到旧的数据库,并且不会从
资产
文件夹复制新的数据库。
如果(!dbExist){(正确)阻止在旧数据库上复制新数据库,则此行
您必须从数据库路径中删除数据库,然后重新运行应用程序
[编辑]
这就是我复制数据库的方式
// Open your local db as the input stream
final InputStream is = ctx.getAssets().open(DB_NAME);
// Path to the just created empty db
final OutputStream os = new FileOutputStream(new File(DB_PATH));
// Transfer the bytes from the inputfile to the outputfile
final byte[] bfr = new byte[8192];//[1024];
int len = 0;
while ((len = is.read(bfr)) > 0)
{
os.write(bfr, 0, len);
}
// Close the streams
os.flush();
os.close();
is.close();
注意:ctx是我传递的上下文。从要检查的设备检索到数据库文件,没有表。表示复制未正确进行。尝试将数据库直接复制到设备,但效果良好。如果我删除数据库,它将复制空白数据库。怀疑copyDBFromResource()中存在问题