否Sqlite现有数据库的此类表

时间:2015-04-20 13:58:34

标签: android database sqlite

我想在我的应用程序中使用现有的sqlite数据库来获取用户地址详细信息。数据库连接一切正常。但始终显示“没有这样的表”错误消息。我已经在sqlite manager中检查了数据库。它有pincode表和值我不知道这些代码有什么问题

这是我的代码

public class DataBaseHelper extends SQLiteOpenHelper{
 //The Android's default system path of your application database.

    public static String DB_PATH;

    public static String DB_NAME;
    public SQLiteDatabase database;
    public final Context context;
    public static String TABLE_NAME="pincode";

    public SQLiteDatabase getDb() {
        return database;
    }

    public DataBaseHelper(Context context, String databaseName) {
        super(context, databaseName, null, 3);
        this.context = context;

        String packageName = context.getPackageName();
        DB_PATH = String.format("//data//data//%s//databases//", packageName);
        DB_NAME = "pinnew1.sqlite";
        openDataBase();
    }


    public void createDataBase() {
        boolean dbExist = checkDataBase();
        if (!dbExist) {
            this.getReadableDatabase();
            try {
                copyDataBase();
            } catch (IOException e) {
                Log.e(this.getClass().toString(), "Copying error");
                throw new Error("Error copying database!");
            }
        } else {
            Log.i(this.getClass().toString(), "Database already exists");
        }
    }

    private boolean checkDataBase() {
        SQLiteDatabase checkDb = null;
        try {
            String path = DB_PATH + DB_NAME;
            checkDb = SQLiteDatabase.openDatabase(path, null,
                    SQLiteDatabase.OPEN_READONLY);
        } catch (SQLException e) {
            Log.e(this.getClass().toString(), "Error while checking db");
        }

        if (checkDb != null) {
            checkDb.close();
        }
        return checkDb != null;
    }

    private void copyDataBase() throws IOException {

        InputStream externalDbStream = context.getAssets().open(DB_NAME);


        String outFileName = DB_PATH + DB_NAME;


        OutputStream localDbStream = new FileOutputStream(outFileName);


        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = externalDbStream.read(buffer)) > 0) {
            localDbStream.write(buffer, 0, bytesRead);
        }

        localDbStream.close();
        externalDbStream.close();

    }

    public SQLiteDatabase openDataBase() throws SQLException {
        String path = DB_PATH + DB_NAME;
        if (database == null) {
            createDataBase();
            database = SQLiteDatabase.openDatabase(path, null,
                SQLiteDatabase.OPEN_READWRITE);
        }
        return database;
    }
    @Override
    public synchronized void close() {
        if (database != null) {
            database.close();
        }
        super.close();
    }
      public HashMap<String, String> getPinDetails(){
            HashMap<String,String> user = new HashMap<String,String>();
            String selectQuery = "SELECT  * FROM " + TABLE_NAME;

            SQLiteDatabase db = this.getReadableDatabase();
            Cursor cursor = db.rawQuery(selectQuery, null);
            // Move to first row
            cursor.moveToFirst();
            if(cursor.getCount() > 0){
                user.put("city", cursor.getString(1));
                user.put("state", cursor.getString(2));
            }
            cursor.close();
            db.close();
            // return user
            return user;
        }
    @Override
    public void onCreate(SQLiteDatabase db) {}
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
}

0 个答案:

没有答案