我正在创建具有不同角色的管理员表。 当用户注册时,数据,例如:姓名,电子邮件,密码,角色,地址将保存在我的ADMIN_TBL中。此保存的注册数据没有问题。
但是当我尝试获取相同的数据时,通过提供sql查询子句(email =?和pass =?),我的管理员模型对象收到空。
编辑:即使表格和数据有效,也无法从数据库中接收数据。这正是我面临的问题。
这是DBHelper类
package com.thebitshoes.uocn.DAO;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import com.thebitshoes.uocn.Model.Administrator;
import com.thebitshoes.uocn.Model.Student;
import java.util.ArrayList;
import java.util.List;
/**
* Created by thebitshoes on 11/2/17.
*/
public class DBHelper extends SQLiteOpenHelper{
static final String DBNAME="UOCNDB";
static final int VERSION=3;
public static final String TBLName="student_table";
public static final String ID="ID";
public static final String NAME="name";
public static final String AGE="age";
public static final String GENDER="gender";
public static final String COURSE="course";
public static final String STD_EMAIL="email";
public static final String STUDENT_CONTACT="student_contact";
public static final String PARENTS_CONTACT="parents_contact";
public static final String PARENTS_FULLNAME="parents_fullname";
public static final String ADMIN_TBL="admin_table";
public static final String ROLE="role";
public static final String TEACHING_STATUS="status";
public static final String ADMIN_MAIL="emailid";
public static final String ADMIN_CONTACT="contact";
public static final String ADMIN_PASS="password";
public static final String ADMIN_ADDRESS="address";
public static final String COURSE_TBL="course_table";
public static final String COURSE_TITLE="course_title";
public static final String COURSE_DURATION="course_duration";
public static final String FEEDBACK_TBL="feedback_table";
public static final String FEEDBACK="feedback";
public static final String FEED_STATUS="feed_status";
public static final String FEED_DATE="feed_date";
public static final String ATTENDANCE_TBL="attendance_tbl";
public static final String START_DATE="start_date";
public static final String END_DATE="end_date";
public static final String LEAVE="leave";
public static final String ABSENT="absent";
public static final String ROUTINE_TBL="routine_table";
public static final String SUBJECT="subject";
public static final String ROUTINE_DATE_TIME="routine_date_time";
public static final String REPLY_MASTER="reply_master_table";
public static final String USER_EMAIL="emailid";
public static final String REPLY_MESSAGE="reply_msg";
public static final String REPLY_STATUS="reply_status";
public static final String REPLY_DATE="reply_date";
public DBHelper(Context context) {
super(context, DBNAME, null, VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String query="create table "+TBLName+" ( "+ID+" INTEGER PRIMARY KEY, "+
NAME+" TEXT, "+AGE+" TEXT, "+GENDER+" TEXT, "+
COURSE+" TEXT, "+STD_EMAIL+" TEXT, "+STUDENT_CONTACT+" TEXT, "+
PARENTS_CONTACT+" TEXT, "+PARENTS_FULLNAME+" TEXT)";
String admin_query="create table "+ADMIN_TBL+" ( "+ID+" INTEGER PRIMARY KEY, "+NAME+
" TEXT, "+ADMIN_MAIL+
" TEXT, "+ADMIN_CONTACT+" TEXT, "+
ADMIN_PASS+" TEXT, "+ADMIN_ADDRESS+" TEXT, "+
ROLE+" TEXT, "+
TEACHING_STATUS+" TEXT)";
String routine_query="create table "+ROUTINE_TBL+" ( "+ID+" INTEGER PRIMARY KEY, "+
COURSE+" TEXT, "+SUBJECT+" TEXT, "+ROUTINE_DATE_TIME+
" TEXT)";
String reply_query="create table "+REPLY_MASTER+" ( "+ID+" INTEGER PRIMARY KEY, "+
USER_EMAIL+" TEXT, "+REPLY_MESSAGE+" TEXT, "+
REPLY_STATUS+" TEXT, "+REPLY_DATE+" TEXT)";
String attendance_query="create table "+ATTENDANCE_TBL+" ( "+ID+" INTEGER PRIMARY KEY, "+ START_DATE+" TEXT, "+END_DATE+ " TEXT, "+LEAVE+" TEXT, "+ABSENT+" TEXT)";
String feedback_query="creeate table "+FEEDBACK_TBL+" ( "+ID+" INTEGER PRIMARY KEY, "+ USER_EMAIL+" TEXT, "+FEEDBACK+" TEXT, "+FEED_STATUS+" TEXT, "+FEED_DATE+" TEXT)";
String course_query="create table "+COURSE_TBL+" ( "+ID+" INTEGER PRIMARY KEY, "+
COURSE_TITLE+" TEXT, "+COURSE_DURATION+
" TEXT)";
sqLiteDatabase.execSQL(query);
sqLiteDatabase.execSQL(admin_query);
sqLiteDatabase.execSQL(routine_query);
sqLiteDatabase.execSQL(reply_query);
sqLiteDatabase.execSQL(attendance_query);
sqLiteDatabase.execSQL(feedback_query);
sqLiteDatabase.execSQL(course_query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
//onUpgrade remove the existing table, and recreate and populate new data
db.execSQL("DROP TABLE IF EXISTS "+TBLName);
onCreate(db);
}
/* public static final String ADMIN_TBL="admin_table";
public static final String ROLE="role";
public static final String TEACHING_STATUS="status";
public static final String ADMIN_MAIL="emailid";
public static final String ADMIN_CONTACT="contact";
public static final String ADMIN_PASS="password";
public static final String ADMIN_ADDRESS="address";*/
// ADDING DATA TO DATABASE
public long addAdminUsers(Administrator obj){
SQLiteDatabase db=getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(NAME,obj.getName());
cv.put(ADMIN_MAIL,obj.getPassword());
cv.put(ADMIN_CONTACT,obj.getContact());
cv.put(ADMIN_PASS,obj.getPassword());
cv.put(ADMIN_ADDRESS,obj.getAddress());
cv.put(ROLE,obj.getRole());
cv.put(TEACHING_STATUS,obj.getTeachingStatus());
long l2=db.insert(ADMIN_TBL,null,cv);
return l2;
}
public long addStudents(Student obj){
SQLiteDatabase db=getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(NAME,obj.getName());
cv.put(AGE,obj.getAge());
cv.put(GENDER,obj.getGender());
cv.put(COURSE,obj.getCourseOpted());
cv.put(STD_EMAIL,obj.getStudentEmail());
cv.put(STUDENT_CONTACT,obj.getStudentContact());
cv.put(PARENTS_CONTACT,obj.getParentsContact());
cv.put(PARENTS_FULLNAME,obj.getParentsFullName());
long insert=db.insert(TBLName,null,cv);
return insert;
}
//Recieveing data based on EMAIL and PASSWORD
public Administrator getAdminByEmailPassRole(String email,String pass){
SQLiteDatabase db=getReadableDatabase();
String q="select * from "+ADMIN_TBL+" where "+ADMIN_MAIL+" =? and "+ADMIN_PASS+" =?";
Cursor cursor=db.rawQuery(q,new String[]{email,pass});
Administrator obj=null;
if(cursor.moveToFirst()){
obj=new Administrator();
obj.setName(cursor.getString(1));
obj.setEmailId(cursor.getString(2));
obj.setContact(cursor.getString(3));
obj.setPassword(cursor.getString(4));
obj.setAddress(cursor.getString(5));
obj.setRole(cursor.getString(6));
obj.setTeachingStatus(cursor.getString(7));
}
return obj;
}
}
这里是用户可以从中登录/签名的活动类(LandingPage)
package com.thebitshoes.uocn;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.thebitshoes.uocn.DAO.DBHelper;
import com.thebitshoes.uocn.Model.Administrator;
public class LandingPage extends AppCompatActivity {
DBHelper db;
String role;
String[]roles={
"--select role--","Student","Teacher","Parent","Admin",
};
Spinner role_spinner;
EditText lib_email,lib_pass;
TextView lib_signUp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_landing_page);
lib_email=(EditText)findViewById(R.id.lib_emailId);
lib_pass=(EditText)findViewById(R.id.lib_pass);
db=new DBHelper(this);
lib_signUp=(TextView)findViewById(R.id.lib_signup_tv);
role_spinner=(Spinner)findViewById(R.id.role_spinner_widget);
ArrayAdapter<String>adapter=new ArrayAdapter<String>(this,R.layout.spinner_layout,roles);
adapter.setDropDownViewResource(R.layout.spinner_layout);
role_spinner.setAdapter(adapter);
role_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
switch(i){
case 0:
break;
case 1: role="Student";
break;
case 2: role="Teacher";
break;
case 3: role="Parent";
break;
case 4: role="Admin";
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
lib_signUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(LandingPage.this,AdminSignUp.class));
}
});
}
public void loginNow(View v){
String email=lib_email.getText().toString();
String pass=lib_pass.getText().toString();
//Toast.makeText(getApplicationContext()," E :"+email+" P: "+pass,Toast.LENGTH_SHORT).show();
Administrator obj=db.getAdminByEmailPassRole(email,pass);
Toast.makeText(getApplicationContext(),""+obj.getName()+" "+obj.getRole()+obj.getContact(),Toast.LENGTH_SHORT).show();
}
}
将用户信息添加到数据库的注册页面(AdminSignup)
package com.thebitshoes.uocn;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.thebitshoes.uocn.DAO.DBHelper;
import com.thebitshoes.uocn.Model.Administrator;
public class AdminSignUp extends AppCompatActivity {
String role;
LinearLayout child_detail_layout,basic_detail_layout;
Button signup;
String[]roles={
"--select a role--","Teacher","Parent","Admin",
};
Spinner role_spinner;
EditText nameofChild,name_tv,email_tv,password_tv,contact_tv,address_tv;
Administrator admin_obj;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin_sign_up);
setTitle("User Sign Up");
child_detail_layout=(LinearLayout)findViewById(R.id.child_detail_layout);
basic_detail_layout=(LinearLayout)findViewById(R.id.basic_detail_layout);
signup=(Button)findViewById(R.id.admin_signup);
role="";
name_tv=(EditText) findViewById(R.id.admin_name_tv);
email_tv=(EditText)findViewById(R.id.admin_mail_tv);
password_tv=(EditText)findViewById(R.id.admin_pass_tv);
contact_tv=(EditText)findViewById(R.id.admin_contact_tv);
address_tv=(EditText)findViewById(R.id.address_tv);
role_spinner=(Spinner)findViewById(R.id.admin_role_spin);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,R.layout.spinner_layout,roles);
adapter.setDropDownViewResource(R.layout.spinner_layout);
role_spinner.setAdapter(adapter);
role_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
switch(i){
case 0:
role="";
break;
case 1: role="Teacher";
child_detail_layout.setVisibility(View.GONE);
basic_detail_layout.setVisibility(View.VISIBLE);
signup.setVisibility(View.VISIBLE);
break;
case 2: role="Parent";
basic_detail_layout.setVisibility(View.GONE);
child_detail_layout.setVisibility(View.VISIBLE);
signup.setVisibility(View.GONE);
break;
case 3: role="Admin";
child_detail_layout.setVisibility(View.GONE);
basic_detail_layout.setVisibility(View.VISIBLE);
signup.setVisibility(View.VISIBLE);
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
public void signupAdmin(View v){
long check=0;
admin_obj=new Administrator();
DBHelper db=new DBHelper(this);
String n,e,p,c,ad;
n=name_tv.getText().toString();
e=email_tv.getText().toString();
p=password_tv.getText().toString();
c=contact_tv.getText().toString();
ad=address_tv.getText().toString();
if(n.equalsIgnoreCase("")||e.equalsIgnoreCase("")||p.equalsIgnoreCase("")||c.equalsIgnoreCase("")||ad.equalsIgnoreCase(""))
{
Toast.makeText(getApplicationContext(),"All fields are mandatory",Toast.LENGTH_SHORT).show();
}
else {
admin_obj.setName(n);
admin_obj.setEmailId(e);
admin_obj.setPassword(p);
admin_obj.setContact(c);
admin_obj.setRole(role);
admin_obj.setAddress(ad);
admin_obj.setTeachingStatus("Active");
check=db.addAdminUsers(admin_obj);
if(check>0) {
Toast.makeText(getApplicationContext(),"Successfully Created",Toast.LENGTH_SHORT).show();
startActivity(new Intent(this,LandingPage.class));
}
else Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_SHORT).show();
}
}
}
这里出现错误LOG
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.thebitshoes.uocn, PID: 26277
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21153)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21153)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.thebitshoes.uocn.Model.Administrator.getName()' on a null object reference
at com.thebitshoes.uocn.LandingPage.loginNow(LandingPage.java:84)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5204)
at android.view.View$PerformClick.run(View.java:21153)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)