在 android studio 中,我想将编辑文本字段中输入的值与数据库进行比较
In android studio, I want to compare the values entered in a Edit text fields to the DB
我有一个注册页面并通过编辑文本字段获取用户名和密码并将它们存储在没有 ID 的数据库中,现在我需要将这些存储在数据库中的用户名和密码与编辑文本中的用户名和密码进行比较另一个名为 "login" 的 activity 想检查它们是否匹配。
这是我的 Signup.java 代码:
package com.example.admin.studentdb;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Signup extends AppCompatActivity implements OnClickListener{
EditText username,password,cpassword;
Button signup;
SQLiteDatabase sdb;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.signup);
username = ((EditText) findViewById(R.id.username));
password = ((EditText) findViewById(R.id.password));
cpassword = ((EditText) findViewById(R.id.cpassword));
signup = ((Button) findViewById(R.id.btnsing));
signup.setOnClickListener(this);
sdb = openOrCreateDatabase("SignUP", Context.MODE_PRIVATE, null);
sdb.execSQL("CREATE TABLE IF NOT EXISTS sign ( username VARCHAR ,password VARCHAR);");
}
public void onClick(View view) {
String chkpass = password.getText().toString();
String cmpass = cpassword.getText().toString();
if (view == signup) {
if (username.getText().toString().trim().length() == 0) {
showMessage("Error", "Please enter the username");
return;
} else if (password.getText().toString().trim().length() == 0) {
showMessage("Error", "Please enter the password");
return;
} else if (cpassword.getText().toString().trim().length() == 0) {
showMessage("Error", "Confirm password");
return;
} else if (!chkpass.equals(cmpass)) {
showMessage("Error", "passwords dont match");
return;
} else if (getCount()==0){
sdb.execSQL("INSERT INTO sign VALUES('" + username.getText() + "','" + password.getText() + "');");
Toast a = Toast.makeText(getApplicationContext(),"Registered Sucessfully",Toast.LENGTH_LONG);
a.show();
Intent abc = new Intent(Signup.this,Login.class);
startActivity(abc);
clearText();
}
else{
showMessage("Error", "Username already Exists");
}
}
}
public int getCount() {
Cursor c = null;
try {
c = sdb.rawQuery("SELECT COUNT(*) FROM sign WHERE username='" + username.getText() + "'", null);
if (c.moveToFirst()) {
return c.getInt(0);
}
return 0;
}
finally {
}
}
public void showMessage(String title,String message)
{
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
public void clearText()
{
username.setText("");
password.setText("");
cpassword.setText("");
username.requestFocus();
}
}
你有没有忘记在 username.getText()
之后添加 .toString()
因为它是 returns Editable
而不是 String
?
c = sdb.rawQuery("SELECT COUNT(*) FROM sign WHERE username='" + username.getText() + "'", null);
尝试以下操作:
Cursor cursor = db.rawQuery("SELECT * FROM studentdatabase WHERE id='" + e1.getText() + "'", null);
if (cursor.getCount() == 0) {
Toast.makeText(this, "No id found:", Toast.LENGTH_SHORT).show();
return;
}
我有一个注册页面并通过编辑文本字段获取用户名和密码并将它们存储在没有 ID 的数据库中,现在我需要将这些存储在数据库中的用户名和密码与编辑文本中的用户名和密码进行比较另一个名为 "login" 的 activity 想检查它们是否匹配。 这是我的 Signup.java 代码:
package com.example.admin.studentdb;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Signup extends AppCompatActivity implements OnClickListener{
EditText username,password,cpassword;
Button signup;
SQLiteDatabase sdb;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.signup);
username = ((EditText) findViewById(R.id.username));
password = ((EditText) findViewById(R.id.password));
cpassword = ((EditText) findViewById(R.id.cpassword));
signup = ((Button) findViewById(R.id.btnsing));
signup.setOnClickListener(this);
sdb = openOrCreateDatabase("SignUP", Context.MODE_PRIVATE, null);
sdb.execSQL("CREATE TABLE IF NOT EXISTS sign ( username VARCHAR ,password VARCHAR);");
}
public void onClick(View view) {
String chkpass = password.getText().toString();
String cmpass = cpassword.getText().toString();
if (view == signup) {
if (username.getText().toString().trim().length() == 0) {
showMessage("Error", "Please enter the username");
return;
} else if (password.getText().toString().trim().length() == 0) {
showMessage("Error", "Please enter the password");
return;
} else if (cpassword.getText().toString().trim().length() == 0) {
showMessage("Error", "Confirm password");
return;
} else if (!chkpass.equals(cmpass)) {
showMessage("Error", "passwords dont match");
return;
} else if (getCount()==0){
sdb.execSQL("INSERT INTO sign VALUES('" + username.getText() + "','" + password.getText() + "');");
Toast a = Toast.makeText(getApplicationContext(),"Registered Sucessfully",Toast.LENGTH_LONG);
a.show();
Intent abc = new Intent(Signup.this,Login.class);
startActivity(abc);
clearText();
}
else{
showMessage("Error", "Username already Exists");
}
}
}
public int getCount() {
Cursor c = null;
try {
c = sdb.rawQuery("SELECT COUNT(*) FROM sign WHERE username='" + username.getText() + "'", null);
if (c.moveToFirst()) {
return c.getInt(0);
}
return 0;
}
finally {
}
}
public void showMessage(String title,String message)
{
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
public void clearText()
{
username.setText("");
password.setText("");
cpassword.setText("");
username.requestFocus();
}
}
你有没有忘记在 username.getText()
之后添加 .toString()
因为它是 returns Editable
而不是 String
?
c = sdb.rawQuery("SELECT COUNT(*) FROM sign WHERE username='" + username.getText() + "'", null);
尝试以下操作:
Cursor cursor = db.rawQuery("SELECT * FROM studentdatabase WHERE id='" + e1.getText() + "'", null);
if (cursor.getCount() == 0) {
Toast.makeText(this, "No id found:", Toast.LENGTH_SHORT).show();
return;
}