我是android studio和sq lite数据库的新手,我正在为像gmail中的登录这样的应用程序开发一个登录页面。在这里,我将用户电话号码作为主键。当用户输入他的电话号码时,如果已经存在,它将重定向到密码屏幕。我不知道如何检查电话号码是否已经存在。 这是我的代码。
PhoneNumber.java
package com.hernaezchristophergmail.projectitrack;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class PhoneNumber extends AppCompatActivity {
EditText Pnum;
Button Con;
UserDBHelper userDBHelper;
SQLiteDatabase sqLiteDatabase;
Context context = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_phone_number);
Pnum = (EditText) findViewById(R.id.tbPnumber);
Con = (Button) findViewById(R.id.btbCon);
Con.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
String pnumber = Pnum.getText().toString().trim();
String storedPassword = UserDBHelper.getSingleEntry(pnumber,sqLiteDatabase);
/* if(pnumber.equals("")||pnumber1.equals("")){
Toast.makeText(PhoneNumber.this, "Please", Toast.LENGTH_LONG).show();
return;
}*/
if(!pnumber.equals(storedPassword)){
Toast.makeText(PhoneNumber.this, "Invalid Login details", Toast.LENGTH_LONG).show();
return;
}
else{
Intent intent = new Intent(PhoneNumber.this,PasswordScreen.class);
startActivity(intent);
}
}
});
}
/* public void add(View view){
String pnumber = Pnum.getText().toString();
Intent intent = new Intent(PhoneNumber.this, PasswordScreen.class);
startActivity(intent);
}
*/
public void create(View view) {
Intent intent = new Intent(PhoneNumber.this, SignUp.class);
startActivity(intent);
}
@Override
public void onBackPressed(){
}
}
UserDBHelper.java
package com.hernaezchristophergmail.projectitrack;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
/**
* Created by Christopher on 4/26/2016.
*/
public class UserDBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "ITRACK_USER.DB";
private static final int DATABASE_VERSION = 1;
private static final String CREATEINFO_QUERY=
"CREATE TABLE "+ UserContact.UserInformation.TABLE_NAME+"("+ UserContact.UserInformation.User_Password+" TEXT," +
UserContact.UserInformation.User_PhoneNumber+" TEXT," + UserContact.UserInformation.User_Firstame+" TEXT," + UserContact.UserInformation.User_LastName+" TEXT);";
public UserDBHelper(Context context){
super(context,DATABASE_NAME,null,DATABASE_VERSION);
Log.e("DATABASE OPERATIONS","DATABASE CREATED / OPENED ...");
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATEINFO_QUERY);
Log.e("DATABASE OPERATION", "TABLE CREATED ...");
}
public void saveInfo(String first,String last,String pnum, String pass, SQLiteDatabase db){
ContentValues contentvalues = new ContentValues();
contentvalues.put(UserContact.UserInformation.User_Firstame,first);
contentvalues.put(UserContact.UserInformation.User_LastName,last);
contentvalues.put(UserContact.UserInformation.User_PhoneNumber,pnum);
contentvalues.put(UserContact.UserInformation.User_Password,pass);
db.insert(UserContact.UserInformation.TABLE_NAME, null, contentvalues);
Log.e ("Database Operation", "one row inserted . . .");
}
public static String getSingleEntry(String pnum, SQLiteDatabase sqLiteDatabase) {
Cursor cursor = sqLiteDatabase.query(UserContact.UserInformation.TABLE_NAME, null, " User_PhoneNumber=?",
new String[] { pnum }, null, null, null);
if (cursor.getCount() < 1) {
cursor.close();
return "NOT EXIST";
}
cursor.moveToFirst();
String phonenum = cursor.getString(cursor.getColumnIndex("password"));
cursor.close();
return phonenum;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
UserContact.java
public class UserContact {
public static abstract class UserInformation{
public static final String TABLE_NAME = "info";
public static final String User_Password= "password";
public static final String User_LastName = "last_name";
public static final String User_Firstame = "first_name";
public static final String User_PhoneNumber = "phone_number";
public static final String PHOTOID = "photo_id";
}
}
答案 0 :(得分:0)
错误是什么? 另外,请看一下。
public static String getSingleEntry(String pnum, SQLiteDatabase sqLiteDatabase) {
Cursor cursor = sqLiteDatabase.query(UserContact.UserInformation.TABLE_NAME, null, " User_PhoneNumber=?",
new String[] { pnum }, null, null, null);
if (cursor.getCount() < 1) {
cursor.close();
return "NOT EXIST";
}
cursor.moveToFirst();
String phonenum = cursor.getString(cursor.getColumnIndex("password"));
cursor.close();
return phonenum; //RETURN YES / NO if exist not the actual password for security purpose.
}
if(!pnumber.equals(storedPassword)){ //ALSO HERE, return YES/NO if exist or new, not the actual password.
Toast.makeText(PhoneNumber.this, "Invalid Login details", Toast.LENGTH_LONG).show();
return;
}
答案 1 :(得分:0)
查询错误
的 1。您的表中从不存在错误的列名 User_PhoneNumber
。写下查询
Cursor cursor = sqLiteDatabase.query(UserContact.UserInformation.TABLE_NAME, null, UserContact.UserInformation.User_PhoneNumber+"=?",
new String[] { pnum }, null, null, null);
和 的 2。错误的列名:)您正在读取密码而不是电话号码..写
String phonenum = cursor.getString(cursor.getColumnIndex(UserContact.UserInformation.User_PhoneNumber));
而不是
String phonenum = cursor.getString(cursor.getColumnIndex("password"));
答案 2 :(得分:0)
以下方法将返回给定数字的名称。通过这种方式,您可以触发该号码是否可用。如果数字为其他,则返回true。
public Boolean numberExists(Context context, String phoneNumber) {
String contactName = null;
ContentResolver resolver = context.getContentResolver();
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
Cursor cursor = resolver.query(uri, new String[]{ContactsContract.Contacts.DISPLAY_NAME}, null, null, null);
try {
if (cursor != null) {
if (cursor.moveToFirst()) {
return true;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
}
return false;
}