我想从数据库中获取数据并将其与一些输入文本进行比较,即如果用户名已经在数据库中存在,则想要检查数据。
这是我的DBHelper代码。
public Boolean checkusername(String name) throws SQLException {
Cursor mCursor =
db.query(true,Login_Table, new String[] { KEY_ROWID, KEY_USERNAME,KEY_STATUS }, KEY_USERNAME
+ "=" + "'" + name + "'", null, null, null, null, null);
if (mCursor.moveToFirst()) {
return true;
}
return false;
}
进行比较的代码
public class Registration extends Activity implements OnClickListener, OnItemSelectedListener
{
// Variable Declaration should be in onCreate()
private Button mSubmit;
private Button mCancel;
private EditText mFname;
private EditText mLname;
private EditText mUsername;
private EditText mPassword;
private EditText mEmail;
private Spinner mGender;
private String Gen;
boolean check;
private DBHelper DB = new DBHelper(Registration.this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
//Assignment of UI fields to the variables
mSubmit = (Button)findViewById(R.id.submit);
mSubmit.setOnClickListener(this);
mCancel = (Button)findViewById(R.id.cancel);
mCancel.setOnClickListener(this);
mFname = (EditText)findViewById(R.id.efname);
mLname = (EditText)findViewById(R.id.elname);
mUsername = (EditText)findViewById(R.id.reuname);
mPassword = (EditText)findViewById(R.id.repass);
mEmail = (EditText)findViewById(R.id.eemail);
mGender = (Spinner)findViewById(R.id.spinner1);
// Spinner method to read the on selected value
ArrayAdapter<State> spinnerArrayAdapter = new ArrayAdapter<State>(this,
android.R.layout.simple_spinner_item, new State[] {
new State("Male"),
new State("Female")});
mGender.setAdapter(spinnerArrayAdapter);
mGender.setOnItemSelectedListener(this);
}
public static boolean isvalidEmail(String email) {
boolean isValid = false;
String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
CharSequence inputStr = email;
Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(inputStr);
if (matcher.matches()) {
isValid = true;
}
return isValid;
}
public void onClick(View v)
{
switch(v.getId()){
case R.id.cancel:
Intent i = new Intent(getBaseContext(), Home.class);
startActivity(i);
//finish();
break;
case R.id.submit:
String fname = mFname.getText().toString();
String lname = mLname.getText().toString();
String uname = mUsername.getText().toString();
String pass = mPassword.getText().toString();
String email = mEmail.getText().toString();
String status="s";
boolean invalid = false;
boolean checkEmail=isvalidEmail(email);
if(fname.equals(""))
{
invalid = true;
Toast.makeText(getApplicationContext(), "Enter your Firstname", Toast.LENGTH_SHORT).show();
}
else
if(fname.matches("[0-9]+" ))
{
invalid = true;
Toast.makeText(getApplicationContext(), "Enter valid name", Toast.LENGTH_SHORT).show();
}
else
if (fname.matches(".*\\s+.*"))
{
invalid = true;
Toast.makeText(getApplicationContext(), "Spaces are not allowed in Firstname", Toast.LENGTH_SHORT).show();
}
else
if(lname.equals(""))
{
invalid = true;
Toast.makeText(getApplicationContext(), "Please enter your Lastname", Toast.LENGTH_SHORT).show();
}
else
if (fname.matches(".*\\s+.*"))
{
invalid = true;
Toast.makeText(getApplicationContext(), "Spaces are not allowed in Lastname", Toast.LENGTH_SHORT).show();
}
else
if(uname.equals(""))
{
invalid = true;
Toast.makeText(getApplicationContext(), "Please enter your Username", Toast.LENGTH_SHORT).show();
}
else
if (uname.matches(".*\\s+.*"))
{
invalid = true;
Toast.makeText(getApplicationContext(), "Spaces are not allowed in Username", Toast.LENGTH_SHORT).show();
}
else
if(pass.equals(""))
{
invalid = true;
Toast.makeText(getApplicationContext(), "Please enter your Password", Toast.LENGTH_SHORT).show();
}
else
if (pass.matches(".*\\s+.*"))
{
invalid = true;
Toast.makeText(getApplicationContext(), "Spaces are not allowed in Password", Toast.LENGTH_SHORT).show();
}
else
if(email.equals(""))
{
invalid = true;
Toast.makeText(getApplicationContext(), "Please enter your Email ID", Toast.LENGTH_SHORT).show();
}
else if(checkEmail==false)
{
invalid = true;
Toast.makeText(getApplicationContext(), "Please enter valid Email ID", Toast.LENGTH_SHORT).show();
}
else
DB.open();
if (DB.checkusername(uname)) { invalid = true; Toast.makeText(getApplicationContext(), "username alredy in use enter another", Toast.LENGTH_SHORT).show(); }
else
if(invalid == false)
{
addEntry(fname, lname, Gen, uname, pass, email);
login( uname, pass, status);
Intent i_register = new Intent(Registration.this, Login.class);
startActivity(i_register);
//finish();
}
break;
}
}
public void onDestroy()
{
super.onDestroy();
DB.close();
}
private void login(String username,String password,String status)
{
SQLiteDatabase db = DB.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("username", username);
values.put("password", password);
values.put("status", status);
try
{
db.insert(DBHelper.Login_Table, null, values);
}
catch(Exception e)
{
e.printStackTrace();
}
}
private void addEntry(String fname, String lname, String Gen, String uname, String pass, String email)
{
SQLiteDatabase db = DB.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("firstname", fname);
values.put("lastname", lname);
values.put("gender", Gen);
values.put("username", uname);
values.put("password", pass);
values.put("email", email);
try
{
db.insert(DBHelper.DATABASE_TABLE_NAME, null, values);
Toast.makeText(getApplicationContext(), "your details submitted Successfully...", Toast.LENGTH_SHORT).show();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
这里是错误logcat
07-07 02:02:48.623: E/AndroidRuntime(8297): FATAL EXCEPTION: main
07-07 02:02:48.623: E/AndroidRuntime(8297): java.lang.NullPointerException
07-07 02:02:48.623: E/AndroidRuntime(8297): at com.google.data.DBHelper.checkusername(DBHelper.java:164)
07-07 02:02:48.623: E/AndroidRuntime(8297): at com.google.data.Registration.onClick(Registration.java:209)
07-07 02:02:48.623: E/AndroidRuntime(8297): at android.view.View.performClick(View.java:2408)
07-07 02:02:48.623: E/AndroidRuntime(8297): at android.view.View$PerformClick.run(View.java:8816)
07-07 02:02:48.623: E/AndroidRuntime(8297): at android.os.Handler.handleCallback(Handler.java:587)
07-07 02:02:48.623: E/AndroidRuntime(8297): at android.os.Handler.dispatchMessage(Handler.java:92)
07-07 02:02:48.623: E/AndroidRuntime(8297): at android.os.Looper.loop(Looper.java:123)
07-07 02:02:48.623: E/AndroidRuntime(8297): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-07 02:02:48.623: E/AndroidRuntime(8297): at java.lang.reflect.Method.invokeNative(Native Method)
07-07 02:02:48.623: E/AndroidRuntime(8297): at java.lang.reflect.Method.invoke(Method.java:521)
07-07 02:02:48.623: E/AndroidRuntime(8297): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-07 02:02:48.623: E/AndroidRuntime(8297): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-07 02:02:48.623: E/AndroidRuntime(8297): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
似乎没有open
您正在执行的数据库query
。在执行任何操作之前,您需要先打开数据库。
示例:db.open()