Android DatabaseHelper错误。光标不会再次打开同一个表

时间:2017-03-21 15:46:14

标签: android android-sqlite sqliteopenhelper

我的系统中现在有两个表。 admin_tbluser_tbl。我在user_tbl上成功添加了CRUD操作的代码,但遗憾的是我的其他两个选择查询不适用于其他两个登录。

这是我的 DatabaseHelper.java

package com.mms;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class DatabaseHelper extends SQLiteOpenHelper {

    // Database Version
    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "mmsDB";

    //Tables Names
    private static final String TABLE_ADMIN = "admin_tbl";
    private static final String TABLE_USER = "user_tbl";

    //Columns of TABLE_ADMIN
    private static final String ADMIN_ID = "a_id";
    private static final String ADMIN_NAME = "a_name";
    private static final String ADMIN_PASSWORD = "a_password";

    //Columns of TABLE_USER
    private static final String USER_ID = "u_id";
    private static final String USER_NAME = "u_name";
    private static final String USER_PASSWORD = "u_password";
    private static final String USER_TYPE = "u_type";

    // Create statements for Tables
    private static final String CREATE_TABLE_ADMIN = "CREATE TABLE " + TABLE_ADMIN +
        "(" + ADMIN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + ADMIN_NAME + " TEXT, " + ADMIN_PASSWORD + " TEXT" + ")";

    private static final String CREATE_TABLE_USER = "CREATE TABLE IF NOT EXISTS " + TABLE_USER +
        "(" + USER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + USER_NAME + " TEXT, " + USER_PASSWORD + " TEXT, " + USER_TYPE + " TEXT" + ")";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // Creating tables

        db.execSQL(CREATE_TABLE_ADMIN);
        db.execSQL("insert into " + TABLE_ADMIN + "(" + ADMIN_ID + "," + ADMIN_NAME + "," + ADMIN_PASSWORD + ") values(1,'admin','root')");

        db.execSQL(CREATE_TABLE_USER);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // On upgrade drop older tables
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_ADMIN);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_USER);
        // Create new tables
        onCreate(db);
    }

    // admin_tbl methods

    public Cursor getAllAdminValues() {
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor res = db.rawQuery("select * from " + TABLE_ADMIN, null);
        //res.close();
        return res;
    }

    // user_tbl methods

    public boolean insertUserValue(String u_name, String u_password, String u_type) {
        //Inserting Records
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues contentValues = new ContentValues();
        //contentValues.put(USER_ID, u_id);
        contentValues.put(USER_NAME, u_name);
        contentValues.put(USER_PASSWORD, u_password);
        contentValues.put(USER_TYPE, u_type);
        long result = db.insert(TABLE_USER, null, contentValues);

        db.close();
        if (result == -1) {
            return false;
        } else {
            return true;
        }
    }

    public Cursor getAllUserValues() {
        SQLiteDatabase db = this.getWritableDatabase();

        Cursor res = db.rawQuery("select * from " + TABLE_USER, null);
        if (res != null) {
            res.close();
        }

        db.close();
        return res;
    }

    public boolean updateUserValues(String u_id, String u_name, String u_password, String u_type) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(USER_ID, u_id);
        contentValues.put(USER_NAME, u_name);
        contentValues.put(USER_PASSWORD, u_password);
        contentValues.put(USER_TYPE, u_type);
        db.update(TABLE_USER, contentValues, "u_id=?", new String[] { u_id });

        db.close();
        return true;
    }

    public Integer deleteUserValues(String u_id) {
        SQLiteDatabase db = this.getWritableDatabase();
        return db.delete(TABLE_USER, "u_id=?", new String[] { u_id });
    }

    public Cursor getAllRectorValues() {
        SQLiteDatabase db = this.getWritableDatabase();

        Cursor res = db.rawQuery("select u_name, u_password from " + TABLE_USER + " WHERE u_name='Rector'", null);
        if (res != null) {
            res.close();
        }
        db.close();
        return res;
    }

    public Cursor getAllStudentValues() {
        SQLiteDatabase db = this.getWritableDatabase();

        Cursor res = db.rawQuery("select u_name, u_password from " + TABLE_USER + " WHERE u_name='Student'", null);
        if (res != null) {
            res.close();
        }
        db.close();
        return res;
    }
}

我的 LoginRectorActivity.java

package com.mms;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginRectorActivity extends Activity implements View.OnClickListener {

    DatabaseHelper helper;
    EditText userRector, passwordRector;
    Button btnRectorLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rector_login);

        helper = new DatabaseHelper(this);

        userRector = (EditText) findViewById(R.id.userRector);
        passwordRector = (EditText) findViewById(R.id.passwordRector);
        btnRectorLogin = (Button) findViewById(R.id.btnRectorLogin);

        btnRectorLogin.setOnClickListener(this);
        helper.close();
    }

    public void onClick(View v) {

        Cursor res = helper.getAllRectorValues();

        String uname = userRector.getText().toString();
        String pass = passwordRector.getText().toString();

        while (res.moveToNext()) {
            if (uname.equals(res.getString(1)) && pass.equals(res.getString(2))) {
                Intent intent = new Intent(LoginRectorActivity.this, RectorHomeActivity.class);
                startActivity(intent);
                finish();
            } else {
                Toast.makeText(LoginRectorActivity.this, "Password Doesn't Match", Toast.LENGTH_SHORT).show();
            }
        }
    }
}

如果我的代码中有任何错误,请告诉我。 Logcat在getRectorValues()方法中显示错误。

1 个答案:

答案 0 :(得分:0)

不要关闭Cursor& getAllRectorValues中的数据库对象& getAllStudentValuesres.close()

  

关闭Cursor,释放所有资源并使其完全无效。

参考docs