#java #android
#java #Android
Вопрос:
Пожалуйста, помогите мне в этом коде. База данных создана успешно, таблица также создана успешно, но запись не попадает в таблицу. Он показывает успешную регистрацию, но не показывает никаких записей в таблице. здесь ниже я прикрепил код другого класса.
registrationalumini.java
public class RegistrationAlumini extends AppCompatActivity {
EditText first, last, email, mobile, currentjob, currentcity, currentcomp, dob,poy,feedback;
Button save, cancel;
DatabaseHandler db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration_alumini);
first = (EditText) findViewById(R.id.editfirstname);
last = (EditText) findViewById(R.id.editlastname);
email = (EditText) findViewById(R.id.editemail);
mobile = (EditText) findViewById(R.id.editmobileno);
currentjob = (EditText) findViewById(R.id.editcurrentjob);
currentcity = (EditText) findViewById(R.id.editcurrentcity);
currentcomp = (EditText) findViewById(R.id.editcurrentcompany);
dob = (EditText) findViewById(R.id.editdob);
poy = (EditText) findViewById(R.id.editpoy);
feedback = (EditText) findViewById(R.id.editfb);
save = (Button) findViewById(R.id.btnsave);
cancel = (Button) findViewById(R.id.btncancel);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String edfirst = first.getText().toString();
String edlast = last.getText().toString();
String edemail = email.getText().toString();
String edmobile = mobile.getText().toString();
String edcurrentjob = currentjob.getText().toString();
String edcurrentcity = currentcity.getText().toString();
String edcurrentcomp = currentcomp.getText().toString();
String eddob = dob.getText().toString();
String edpoy = poy.getText().toString();
String edfeedback = feedback.getText().toString();
if(first.getText().toString().length()==0 ){
first.setError("First name not entered");
first.requestFocus();
}
if(last.getText().toString().length()==0){
last.setError("Last name not entered");
last.requestFocus();
}
if(email.getText().toString().length()==0){
email.setError("Email is required");
email.requestFocus();
}
if(mobile.getText().toString().length()<10 ){
mobile.setError("Mobile number shall be 10 digit");
mobile.requestFocus();
}
if(currentjob.getText().toString().length()==0){
currentjob.setError("Current job is required");
currentjob.requestFocus();
}
if(currentcity.getText().toString().length()==0){
currentcity.setError("Current city is required");
currentcity.requestFocus();
}
if(currentcomp.getText().toString().length()==0){
currentcomp.setError("current company or organization is required");
currentcomp.requestFocus();
}
if(dob.getText().toString().length()==0){
dob.setError("date of birth is required");
dob.requestFocus();
}
if(poy.getText().toString().length()==0 ){
poy.setError("pass out year is required ");
poy.requestFocus();
}
if(feedback.getText().toString().length()==0){
feedback.setError("Feedback is required");
feedback.requestFocus();
}
db = new DatabaseHandler(RegistrationAlumini.this, null, null, 2);
Registerdata reg = new Registerdata();
reg.setfirstName(edfirst);
reg.setlastName(edlast);
reg.setEmailId(edemail);
reg.setmobileno(edmobile);
reg.setcurrentjob(edcurrentjob);
reg.setcurrentcity(edcurrentcity);
reg.setcurrentcomp(edcurrentcomp);
reg.setdob(eddob);
reg.setpoy(edpoy);
reg.setfeedback(edfeedback);
db.addregister(reg);
Toast.makeText(getApplicationContext(), "Registered", Toast.LENGTH_LONG).show();
startActivity(new Intent(getApplicationContext(),RegistrationAlumini.class));
}
});
}
DatabaseHandler.java
public class DatabaseHandler extends SQLiteOpenHelper {
int id;
public DatabaseHandler(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "Alumini.db";
// Contacts table name
private static final String TABLE_REGISTER= "register";
public static final String KEY_ID = "id";
public static final String KEY_FIRST_NAME = " first_name";
public static final String KEY_lAST_NAME = "last_name";
public static final String KEY_EMAIL_ID="email_id";
public static final String KEY_MOB_NO = "mobile_number";
public static final String KEY_CURRENT_JOB = "current_job";
public static final String KEY_CURRENT_CITY = "current_city";
public static final String KEY_CURRENT_COMP = "current_comp";
public static final String KEY_DATE_OF_BIRTH = "d_o_b";
public static final String KEY_PASS_OUT_YEAR = "p_o_y";
public static final String KEY_FEED_BACK = "feed_back";
//public static final String KEY_PASSWORD = "password";
public static final String CREATE_TABLE="CREATE TABLE " TABLE_REGISTER "("
KEY_ID " INTEGER PRIMARY KEY," KEY_FIRST_NAME " TEXT," KEY_lAST_NAME " TEXT," KEY_EMAIL_ID " TEXT,"
KEY_MOB_NO " TEXT," KEY_CURRENT_JOB " TEXT ," KEY_CURRENT_CITY "TEXT," KEY_CURRENT_COMP "TEXT,"
KEY_DATE_OF_BIRTH "TEXT," KEY_PASS_OUT_YEAR "TEXT," KEY_FEED_BACK "TEXT" ")";
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " TABLE_REGISTER);
onCreate(sqLiteDatabase);
}
void addregister(Registerdata registerdata)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_FIRST_NAME,registerdata.getFirst_name()); // register first Name
values.put(KEY_lAST_NAME, registerdata. getLast_name() ); // register last name
values.put(KEY_EMAIL_ID, registerdata.getEmail_id());//register email id
values.put(KEY_MOB_NO, registerdata.getMobile_number());//register mobile no
values.put(KEY_CURRENT_JOB,registerdata.getCurrent_job()); // register current job
values.put(KEY_CURRENT_CITY, registerdata. getCurrent_city() ); // register current city
values.put(KEY_CURRENT_COMP, registerdata.getCurrent_comp());//register current company
values.put(KEY_DATE_OF_BIRTH, registerdata.getD_o_b());//register date of year
values.put(KEY_PASS_OUT_YEAR, registerdata.getP_o_y());//register pass out year
values.put(KEY_FEED_BACK, registerdata.getFeed_back());//register feedback
//values.put(KEY_PASSWORD, registerdata.getPassword());
// Inserting Row
db.insert(TABLE_REGISTER, null, values);
db.close(); // Closing database connection
}
RegisterData.java
public class Registerdata {
int _id;
String first_name;
String last_name;
String email_id;
String mobile_number;
String current_job;
String current_city;
String current_comp;
String d_o_b;
String p_o_y;
String feed_back;
public Registerdata(){
}
public Registerdata(int id, String first_name, String last_name,String email_id,String mobile_number, String current_job,
String current_city, String current_comp, String d_o_b, String p_o_y, String feed_back){
this._id = id;
this.first_name = first_name;
this.last_name = last_name;
this.email_id=email_id;
this.mobile_number=mobile_number;
this.current_job = current_job;
this.current_city = current_city;
this.current_comp = current_comp;
this.d_o_b = d_o_b;
this.p_o_y = p_o_y;
this.feed_back = feed_back;
}
public int getID(){ return this._id; }
// setting id
public void setID(int id){
this._id = id;
}
public String getFirst_name() {
// TODO Auto-generated method stub
return first_name;
}
// setting first name
public void setfirstName(String first_name){
this.first_name = first_name;
}
public String getLast_name(){
return last_name;
}
public void setlastName(String last_name){
this.last_name = last_name;
}
public String getEmail_id(){
return email_id;
}
public void setEmailId(String email_id){
this.email_id = email_id;
}
public String getMobile_number(){
return mobile_number;
}
public void setmobileno(String mobile_number){
this.mobile_number = mobile_number;
}
public String getCurrent_job(){
return current_job;
}
public void setcurrentjob(String current_job){
this.current_job = current_job;
}
public String getCurrent_city(){
return current_city;
}
public void setcurrentcity(String current_city){
this.current_city = current_city;
}
public String getCurrent_comp(){
return current_comp;
}
public void setcurrentcomp(String current_comp){
this.current_comp = current_comp;
}
public String getD_o_b(){
return d_o_b;
}
public void setdob(String d_o_b){
this.d_o_b = d_o_b;
}
public String getP_o_y(){
return p_o_y;
}
public void setpoy(String p_o_y){
this.p_o_y = p_o_y;
}
public String getFeed_back(){
return feed_back;
}
public void setfeedback(String feed_back){
this.feed_back = feed_back;
}
}
here is log
2020-10-08 20:30:43.326 13701-13701/com.lcit.lcitnewdemo E/SQLiteLog: (1) table register has no column named feed_back in "INSERT INTO register( first_name,feed_back,id,mobile_number,d_o_b,p_o_y,current_job,email_id,current_city,current_comp,last_name) VALUES (?,?,?,?,?,?,?,?,?,?,?)
2020-10-08 20:30:43.328 13701-13701/com.lcit.lcitnewdemo E/SQLiteDatabase: Error inserting first_name=dfdsfdf feed_back=vcxvxcv id=0 mobile_number=1232322344 d_o_b=12-09-1986 p_o_y=2009 current_job=xvcvcv email_id=zczc@ghgh.com current_city=xcvxvx current_comp=xcvxcv last_name=xxcvxv
android.database.sqlite.SQLiteException: table register has no column named feed_back (code 1 SQLITE_ERROR): , while compiling: INSERT INTO register( first_name,feed_back,id,mobile_number,d_o_b,p_o_y,current_job,email_id,current_city,current_comp,last_name) VALUES (?,?,?,?,?,?,?,?,?,?,?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1045)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:652)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:590)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:61)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:33)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1699)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1570)
at com.lcit.lcitnewdemo.DatabaseHandler.addregister(DatabaseHandler.java:74)
at com.lcit.lcitnewdemo.RegistrationAlumini$1.onClick(RegistrationAlumini.java:108)
at android.view.View.performClick(View.java:7438)
at android.view.View.performClickInternal(View.java:7415)
at android.view.View.access$3600(View.java:810)
at android.view.View$PerformClick.run(View.java:28286)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:941)
2020-10-08 20:30:43.329 13701-13701/com.lcit.lcitnewdemo D/CompatibilityChangeReporter: Compat change id reported: 147798919; UID 10163; state: ENABLED
Ответ №1:
При определении таблицы в CREATE_TABLE
вы забыли добавить несколько пробелов "TEXT"
. Вы должны были получить исключение при попытке создать таблицу. Я исправил это здесь:
public static final String CREATE_TABLE="CREATE TABLE " TABLE_REGISTER "("
KEY_ID " INTEGER PRIMARY KEY," KEY_FIRST_NAME " TEXT," KEY_lAST_NAME " TEXT," KEY_EMAIL_ID " TEXT,"
KEY_MOB_NO " TEXT," KEY_CURRENT_JOB " TEXT ," KEY_CURRENT_CITY " TEXT," KEY_CURRENT_COMP " TEXT,"
KEY_DATE_OF_BIRTH " TEXT," KEY_PASS_OUT_YEAR " TEXT," KEY_FEED_BACK " TEXT" ")";