无法从SQLite数据库查询:没有此类表错误

时间:2015-11-11 12:25:38

标签: 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="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/<packagename>/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;

    }

找不到表pasurams的运行时错误。相同的查询在SQLite数据库中独立运行。

此代码有什么问题?任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

应用程序使用的数据库与您在SQLiteManager中查询的数据库不同 您可能在第一次运行后添加了表格。

现在运行时,它已找到数据库,并且不会从create or replace type myrec is object (myid number); create or replace type mytemp_collection is table of myrec; declare v_temp_collection mytemp_collection; begin v_temp_collection := mytemp_collection(); select myrec (t.field_type_id ) bulk collect into v_temp_collection from fs_field_types t where mod(t.field_type_id+1,3)=0; -- for example FOR i IN 1 .. v_temp_collection.count LOOP DBMS_OUTPUT.put_line(v_temp_collection(i).myid); End loop; delete fs_field_types_back t where t.field_type_id in (select myid from table(v_temp_collection)); end; 文件夹中复制。 此行assets(正确)可防止将数据库复制到上。

您必须从if (!dbExist){路径中删除数据库并重新运行该应用。

<强> [编辑]

这是我复制db

的方法
databases

注意:ctx是我传递的上下文。