【问题标题】:How to enter information in SQLite database? (App Crashes) [duplicate]如何在 SQLite 数据库中输入信息? (应用程序崩溃)[重复]
【发布时间】:2016-06-28 19:43:59
【问题描述】:

我一直在尝试使用注册活动将医生详细信息存储在 SQLite 数据库中,但每次单击注册按钮时我的应用程序都会崩溃。它没有显示任何错误。我关注了许多在线视频,但它对我的应用程序不起作用 错误是什么?应用为什么会崩溃?

dsignup.java

DatabaseHelper1 helper1 = new DatabaseHelper1(this);
public void OnButton_regclick(View v)
{
    if(v.getId()== R.id.button_reg)
    {
        EditText dname = (EditText)findViewById(R.id.dname);
        EditText username = (EditText)findViewById(R.id.username);
        EditText docemail = (EditText)findViewById(R.id.docemail);
        EditText password = (EditText)findViewById(R.id.password);
        EditText reg_num = (EditText)findViewById(R.id.reg_num);
        EditText dcontact = (EditText)findViewById(R.id.dcontact);
        EditText wcontact = (EditText)findViewById(R.id.wcontact);
        RadioGroup gender = (RadioGroup) findViewById(R.id.gender);
        int selectedid = gender.getCheckedRadioButtonId();
        EditText address = (EditText)findViewById(R.id.address);
        EditText pincode = (EditText)findViewById(R.id.pincode);
        EditText specialization =(EditText)findViewById(R.id.specialization);
        EditText experience = (EditText)findViewById(R.id.experience);
        EditText category = (EditText)findViewById(R.id.category);

        RadioButton rb = (RadioButton)findViewById(selectedid);

        String dnamestr = dname.getText().toString();
        String docemailstr = docemail.getText().toString();
        String usernamestr = username.getText().toString();
        String passwordstr = password.getText().toString();
        String  dcontactstr = dcontact.getText().toString();
        String  reg_numstr = reg_num.getText().toString();
        String  specializationstr = specialization.getText().toString();
        String  experiencestr = experience.getText().toString();
        String  categorystr = category.getText().toString();
        String  genderstr = rb.getText().toString();
        String  pincodestr = pincode.getText().toString();
        String  addressstr = address.getText().toString();
        String  wcontactstr = wcontact.getText().toString();
        Contact c = new Contact();
        c.setDname(dnamestr);
        c.setDocemail(docemailstr);
        c.setUsername(usernamestr);
        c.setPassword(passwordstr);
        c.setDcontact(dcontactstr);
        c.setReg_num(reg_numstr);
        c.setSpecialization(specializationstr);
        c.setExperience(experiencestr);
        c.setCategory(categorystr);
        c.setGender(genderstr);
        c.setPincode(pincodestr);
        c.setAddress(addressstr);
        c.setWcontact(wcontactstr);
        helper1.insertContact(c);

            }

DatabaseHelper1.java

public class DatabaseHelper1 extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "contacts.db";
private static final String TABLE_NAME1 = "doctors";
private static final String COLUMN_ID1 = "id1";
private static final String COLUMN_DNAME ="dname";
private static final String COLUMN_DCONTACT ="dcontact";
private static final String COLUMN_REG_NUM ="reg_num";
private static final String COLUMN_SPECIALIZATION ="specialization";
private static final String COLUMN_EXPERIENCE ="experience";
private static final String COLUMN_CATEGORY ="category";
private static final String COLUMN_AVAILABLEFROM ="availablefrom";
private static final String COLUMN_PASSWORD ="password";
private static final String COLUMN_USERNAME ="username";
private static final String COLUMN_AVAILABLETO ="availableto";
private static final String COLUMN_GENDER ="gender";
private static final String COLUMN_DOCEMAIL ="docemail";
private static final String COLUMN_PINCODE ="pincode";
private static final String COLUMN_ADDRESS ="address";
private static final String COLUMN_WCONTACT ="wcontact";
private static final String COLUMN_TIMETO ="timeto";
private static final String COLUMN_TIMEFROM ="timefrom";
private static final String COLUMN_LATITUDE ="latitude";
private static final String COLUMN_LONGITUDE ="longitude";
SQLiteDatabase db1;

private static final String TABLE_CREATE1 = "create table doctors (id integer primary key not null, dname text not null,reg_num integer not null , specialization text not null, experience text not null, category text not null," +
        "available from text not null, username text not null, availableto text not null, gender text not null, email text not null, pincode integer not null, address text not null " +
        " wcontact integer not null, timeto time, timefrom time, latitude float(10,6), longitude float(10,6));";


public DatabaseHelper1(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db1) {
    db1.execSQL(TABLE_CREATE1);
    this.db1 = db1;


}



public void insertContact(Contact c) {
    db1 = this.getWritableDatabase();
    ContentValues values = new ContentValues();

    String query = "select * from doctors";
    Cursor cursor = db1.rawQuery(query , null);
    int count = cursor.getCount();

    values.put(COLUMN_ID1 , count);
    values.put(COLUMN_DNAME, c.getDname());
    values.put(COLUMN_DCONTACT, c.getDcontact());
    values.put(COLUMN_REG_NUM, c.getReg_num());
    values.put(COLUMN_SPECIALIZATION, c.getSpecialization());
    values.put(COLUMN_EXPERIENCE, c.getExperience());
    values.put(COLUMN_CATEGORY, c.getCategory());
    values.put(COLUMN_AVAILABLEFROM, c.getAvailablefrom());
    values.put(COLUMN_PASSWORD, c.getPassword());
    values.put(COLUMN_USERNAME, c.getUsername());
    values.put(COLUMN_AVAILABLETO, c.getAvailableto());
    values.put(COLUMN_GENDER, c.getGender());
    values.put(COLUMN_DOCEMAIL, c.getDocemail());
    values.put(COLUMN_PINCODE, c.getPincode());
    values.put(COLUMN_ADDRESS, c.getAddress());
    values.put(COLUMN_WCONTACT, c.getWcontact());
    //values.put(COLUMN_TIMETO, c.getTimeto());
   // values.put(COLUMN_TIMEFROM, c.getTimefrom());
    // values.put(COLUMN_LATITUDE, c.getLatitude());
    //values.put(COLUMN_LONGITUDE, c.getLongitude());

    db1.insert(TABLE_NAME1, null, values);
    db1.close();
}


public String searchPass(String username)
{
    db1 = this.getReadableDatabase();
    String query = "select uname, pass from "+TABLE_NAME1;
    Cursor cursor = db1.rawQuery(query , null);
    String a, b;
    b = "not found";
    if(cursor.moveToFirst())
    {
        do{
            a = cursor.getString(0);

            if(a.equals(username))
            {
                b = cursor.getString(1);
                break;
            }
        }
        while(cursor.moveToNext());
    }

    return b;
}


@Override
public void onUpgrade(SQLiteDatabase db1, int oldVersion, int newVersion) {
    String query = "DROP TABLE IF EXISTS "+TABLE_NAME1;
    db1.execSQL(query);
    this.onCreate(db1);
       }
    }

Contact.java

public class Contact {
String name ,email,uname,pass,gender1,dname,dcontact,reg_num,specialization, experience, category, availablefrom, password, username, availableto,gender,docemail,pincode,address,wcontact;

public void setName(String name)
{
    this.name = name;
}
public String getName()
{
    return this.name;
}
public void setEmail(String email)
{
    this.email = email;
}
public String getEmail()
{
    return this.email;
}
public void setUname(String uname)
{
    this.uname = uname;
}
public String getUname()
{
    return this.uname;
}
public void setPass(String pass)
{
    this.pass = pass;
}
public String getPass()
{
    return this.pass;
}
public void setGender1(String gender)
{
    this.gender1 = gender;
}
public String getGender1()
{
    return this.gender1;
}
public void setDname(String dname)
{
    this.dname = dname;
}
public String getDname()
{
    return this.dname;
}
public void setReg_num(String reg_num)
{
    this.reg_num = reg_num;
}
public String getReg_num()
{
    return this.reg_num;
}
public void setDcontact(String dcontact)
{
    this.name = dcontact;
}
public String getDcontact()
{
    return this.dcontact;
}
public void setSpecialization(String specialization)
{
    this.specialization = specialization;
}
public String getSpecialization()
{
    return this.specialization;
}
public void setExperience(String experience)
{
    this.experience = experience;
}
public String getExperience()
{
    return this.experience;
}
public void setCategory(String category)
{
    this.category = category;
}
public String getCategory()
{
    return this.category;
}
public void setAvailablefrom(String availablefrom)
{
    this.availablefrom = availablefrom;
}
public String getAvailablefrom()
{
    return this.availablefrom;
}
public void setPassword(String password)
{
    this.password = password;
}
public String getPassword()
{
    return this.password;
}
public void setUsername(String username)
{
    this.username = username;
}
public String getUsername()
{
    return this.username;
}
public void setAvailableto(String availableto)
{
    this.availableto = availableto;
}
public String getAvailableto()
{
    return this.availableto;
}
public void setGender(String gender)
{
    this.gender = gender;
}
public String getGender()
{
    return this.gender;
}
public void setDocemail(String docemail)
{
    this.docemail = docemail;
}
public String getDocemail()
{
    return this.docemail;
}
public void setPincode(String pincode) {this.pincode = pincode;}
public String getPincode()
{
    return this.pincode;
}
public void setAddress(String address)
{
    this.address = address;
}
public String getAddress()
{
    return this.address;
}
public void setWcontact(String wcontact)
{
    this.wcontact = wcontact;
}
public String getWcontact()
{
    return this.wcontact;
}
public void setTimeto(String timeto)
{
    this.timeto = timeto;
}
public String getTimeto() {return this.timeto;}
public void setTimefrom(String timefrom)
{
    this.timefrom = timefrom;
}
public String getTimefrom()
{
    return this.timefrom;
}

}

【问题讨论】:

  • 请发布您的 logcat 详细信息
  • 请给出错误日志。

标签: java android database sqlite android-studio


【解决方案1】:

尝试对您的代码进行排序,因为它不容易阅读。另外,创建一个可以初始化小部件的方法,如下所示:

public static void startCom(){
     text1 = (textView) findById....
     .
     .
     .
}

只在onCreate方法上调用startComp,只是为了保存代码的顺序。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-02
    相关资源
    最近更新 更多