无法从sqlite查询

时间:2017-10-24 11:38:48

标签: java android sqlite android-sqlite

我无法从sqlite中选择列权重 如果用户和密码在吐司中检查打印重量(查询) 有人帮帮我吗?

enter image description here

Button btnLogin;
EditText editUsername;
EditText editPassword;
TextView result;
DatabaseHelperTwinIGE databaseHelperTwinIGE;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.tab5twinoge, container, false);


    btnLogin = (Button)view.findViewById(R.id.btnAdd);
    editUsername = (EditText)view.findViewById(R.id.txtNumber1);
    editPassword = (EditText)view.findViewById(R.id.txtNumber2);
    result = (TextView)view .findViewById(R.id.txtResult);

    databaseHelperTwinIGE = new DatabaseHelperTwinIGE(getActivity());

    btnLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            boolean isExist = databaseHelperTwinIGE.checkUserExist(editUsername.getText().toString(),editPassword.getText().toString());

            if (isExist){
                Toast.makeText(getActivity(),"Login Success",Toast.LENGTH_SHORT).show();

            }else {
                editPassword.setText(null);
                Toast.makeText(getActivity(),"Login failed. Invalid username or password.",Toast.LENGTH_SHORT).show();

            }
        }
    });


    return view;

}
}
public class DatabaseHelperTwinIGE extends SQLiteOpenHelper{

    private static final String DATABASE_NAME = "test.db";
    private static final int DATABASE_VERSION = 1;
    private final Context context;
    SQLiteDatabase db;

    private static final String DATABASE_PATH = "/data/data/com.app.army.tab/databases/";
    private final String USER_TABLE = "user";


    public DatabaseHelperTwinIGE(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.context = context;
        createDb();
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
    }

    public void createDb(){
        boolean dbExist = checkDbExist();

        if (!dbExist){
            this.getReadableDatabase();
            copyDatabase();
        }
    }

    private boolean checkDbExist(){
        SQLiteDatabase sqLiteDatabase = null;

        try {
            String path = DATABASE_PATH + DATABASE_NAME;
            sqLiteDatabase = SQLiteDatabase.openDatabase(path,null,SQLiteDatabase.OPEN_READONLY);
        }catch (Exception ex){
        }


        if (sqLiteDatabase != null){
            sqLiteDatabase.close();
            return  true;
        }
        return false;

    }

    private void copyDatabase(){
        try {
            InputStream inputStream = context.getAssets().open(DATABASE_NAME);

            String ourFileName = DATABASE_PATH + DATABASE_NAME ;

            OutputStream outputStream = new FileOutputStream(ourFileName);

            byte[] b = new byte[1024];
            int length;


            while ((length = inputStream.read(b)) > 0 ){
                outputStream.write(b,0,length);
            }

            outputStream.flush();
            outputStream.close();
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private SQLiteDatabase openDatabase(){
        String path = DATABASE_PATH + DATABASE_NAME;
        db = SQLiteDatabase.openDatabase(path,null,SQLiteDatabase.OPEN_READWRITE);
        return db;
    }
    public void  close(){
        if (db !=null){
            db.close();
        }

    }


    public boolean checkUserExist(String username,String password){
        String[] column = {"username"};
        db = openDatabase();

        String selection = "username=? and password = ?" ;
        String[] selectionArgs = {username,password};

        Cursor cursor = db.query(USER_TABLE,column,selection,selectionArgs,null,null,null);
        int count = cursor.getCount();

        cursor.close();
        close();
        if (count>0){
            return true;
        } else {
            return false;
        }

    }

我不想要打印登录成功
我想从sqlite查询打印重量

我无法从sqlite中选择列重量 如果用户和密码在吐司中检查打印重量(查询)

1 个答案:

答案 0 :(得分:1)

您可以尝试这样的事情,我们选择从数据库中选择extension UINavigationController { func popToController<T: UIViewController>(_ type: T.Type, animated: Bool) { if let vc = viewControllers.first(where: { $0 is T }) { popToViewController(vc, animated: animated) } } func popToControllerOrToRootControllerIfNotInTheStack<T: UIViewController>(_ type: T.Type, animated: Bool) { if let vc = viewControllers.first(where: { $0 is T }) { popToViewController(vc, animated: animated) } else { popToRootViewController(animated: animated) } } } 。只有密码和用户名正确,才会返回相应的权重。我们只选择第一个结果。

weight

然后你可以这样显示public int getUserWeigth(String username, String password) { String[] column = {"weight"}; db = openDatabase(); String selection = "username=? and password = ?"; String[] selectionArgs = {username, password}; int weight = -1; Cursor cursor = db.query(USER_TABLE, column, selection, selectionArgs, null, null, null); if (cursor.moveToFirst()) { weight = cursor.getInt(0); } cursor.close(); close(); // if the user credentials were valid, a weight will be returned, otherwise -1 return weight; }

Toast