从分离的类调用sqlite查询

时间:2014-07-02 04:45:10

标签: android

我的数据库中存在一些问题......这里是场景

我想像这样分开数据库

第1类:声明数据库 - > master.sqlite

第2课:向第1课中的数据库声明查询

第3课:从第2课调用查询并获取结果

在我的课程1中

我声明我的数据库

public class Database_Login extends SQLiteOpenHelper {

public static int iconPos;

private static String DB_NAME = "Master.sqlite";

private SQLiteDatabase loginDataBase;

private final Context myContext;

private static String DB_PATH;

public Database_Login(Context context) {
    super(context, DB_NAME, null, 1);
    this.myContext = context;
    DB_PATH = "/data/data/" + myContext.getApplicationContext().getPackageName() + "/" + "databases/"; 
}

public void createDataBase() throws IOException {

    boolean dbExist = checkDataBase();

    if (dbExist) {

    } else {
        this.getReadableDatabase();

        try {

            copyDataBase();

        } catch (IOException e) {

            throw new Error("Error copying database");

        }
    }

}

public boolean checkDataBase() {
    SQLiteDatabase checkDB = null;
    try {
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READWRITE);

    } 

    catch (SQLiteException e){


    }

    if (checkDB != null) {

        checkDB.close();

    }

    return checkDB != null ? true : false;
}

/**
 * Copies your database from your local assets-folder to the just created
 * empty database in the system folder, from where it can be accessed and
 * handled. This is done by transfering bytestream.
 * */
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();

}

public void openDataBase() throws SQLException {

    // Open the database
    String myPath = DB_PATH + DB_NAME;
    loginDataBase = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READWRITE);

}

@Override
public synchronized void close() {

    if (loginDataBase != null)
        loginDataBase.close();

    super.close();

}

@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

,在第2课我声明如下

public class Login{
public static int iconPos;

Database_Login login;
SQLiteDatabase loginDataBase;

public Login(Context context) {
    login = new Database_Login(context);
    loginDataBase = login.getReadableDatabase();
}

public boolean cekLogin(String username, String password) throws SQLException {
    login.openDataBase();
    loginDataBase = login.getReadableDatabase();
    Cursor mCursor = loginDataBase.rawQuery("SELECT * FROM tblUser WHERE UserName=? AND Password=?", new String[] { username, password});
    if (mCursor != null) {
        if (mCursor.getCount() > 0) {
            return true;
        }
    }
    login.close();
    return false;
}

public String UserType(String username) throws SQLException{
    login.openDataBase();
    loginDataBase = login.getReadableDatabase();
    String UserType = "";
    Cursor mCursor = loginDataBase.rawQuery("SELECT UserType FROM tblUser WHERE UserName=?", new String[] { username });
    if(mCursor.moveToFirst()){
        UserType = mCursor.getString(mCursor.getColumnIndex("UserType"));
        return UserType;
    }
    login.close();
    return UserType;
}

public int checkIcon() {
    switch (iconPos) {
    case 0:
        iconPos = R.drawable.ic_action_help;
        break;
    case 1:
        iconPos = R.drawable.ic_action_good;
        break;
    case 2:
        iconPos = R.drawable.ic_action_labels;
        break;
    case 3:
        iconPos = R.drawable.ic_action_email;
        break;
    case 4:
        iconPos = R.drawable.ic_action_gamepad;
        break;
    case 5:
        iconPos = R.drawable.ic_action_search;
        break;
    case 6:
        iconPos = R.drawable.ic_action_cloud;
        break;
    case 7:
        iconPos = R.drawable.ic_action_camera;
        break;
    case 8:
        iconPos = R.drawable.ic_action_video;
        break;
    case 9:
        iconPos = R.drawable.ic_action_group;
        break;
    case 10:
        iconPos = R.drawable.ic_action_import_export;
        break;
    case 11:
        iconPos = R.drawable.ic_action_help;
        break;
    case 12:
        iconPos = R.drawable.ic_action_about;
        break;
    case 13:
        iconPos = R.drawable.ic_action_settings;
        break;
    case 14:
        iconPos = R.drawable.ic_action_gamepad;
        break;
    case 15:
        iconPos = R.drawable.ic_action_cloud;
        break;
    case 16:
        iconPos = R.drawable.ic_action_camera;
        break;
    case 17:
        iconPos = R.drawable.ic_action_video;
        break;
    case 18:
        iconPos = R.drawable.ic_action_import_export;
        break;
    case 19:
        iconPos = R.drawable.ic_action_about;
        break;
    case 20:
        iconPos = R.drawable.ic_action_good;
        break;
    case 21:
        iconPos = R.drawable.ic_action_gamepad;
        break;
    case 22:
        iconPos = R.drawable.ic_action_cloud;
        break;
    default:
        break;
    }
    return iconPos;
}

}

,在第3课我只是打电话给第2课方法

但是当我运行它时,日志猫说没有找到tblUser ......

我应该如何让我的数据库工作?

谢谢你的帮助......

1 个答案:

答案 0 :(得分:0)

您可能在首次安装后添加了表格。安装应用程序以进行调试时,它不会再次从资产中复制数据库。要获取最新的数据库,您必须卸载该应用程序并进行全新安装。