简体   繁体   中英

Can't insert data into sqlite in android

I try to insert data from EditView into Sqlite but it can't insert, then close the program. My condition code is working if I don't enter any value all the edittext. After I fill all the fields and click button insert, it doesn't work. Any mistakes in my insert code?

Below is my java code

public class FormPage extends AppCompatActivity implements View.OnClickListener {

public EditText KhId, Fname, Lname, sex, dob, address;
private Button btnadd, btnview, btnupdate;
SQLiteDatabase db;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.form_page);

    KhId = (EditText)findViewById(R.id.editText);
    Fname = (EditText)findViewById(R.id.editText2);
    Lname = (EditText)findViewById(R.id.editText3);
    sex = (EditText)findViewById(R.id.editText4);
    dob = (EditText)findViewById(R.id.editText5);
    address = (EditText)findViewById(R.id.editText6);
    btnadd = (Button)findViewById(R.id.button5);
    btnview = (Button)findViewById(R.id.button6);
    btnupdate = (Button)findViewById(R.id.button7);

    btnadd.setOnClickListener(this);
    btnview.setOnClickListener(this);
    btnupdate.setOnClickListener(this);

    db = openOrCreateDatabase("UserInfo", Context.MODE_PRIVATE, null);
    db.execSQL("CREATE TABLE IF NOT EXISTS user(kh_id int primary key, fname varchar, lname varchar, sex varchar, dob date, address varchar)");
}

public void onClick(View view){
    if (view == btnadd) {
        if (KhId.getText().toString().equals("0")) {
            showMessage("Error", "Zero is invalid for" + " input to database");
        } else {
            if (KhId.getText().toString().equals("") || Fname.getText().toString().equals("") || Lname.getText().toString().equals("") ||
                    sex.getText().toString().equals("") || dob.getText().toString().equals("") || address.getText().toString().equals("")) {
                showMessage("Error", "Please enter all values");
                return;
            }
            Cursor c = db.rawQuery("Select kh_id From user " + "where kh_id='" +
                    KhId.getText().toString() + "'", null);

            if (c.moveToFirst()) {
                showMessage("Error", "Record already exists");
                clearText();
            } else {
                db.execSQL("INSERT INTO user VALUES(" + KhId.getText() + ",'" + Fname.getText() + "','" + Lname.getText()+"','"+ sex.getText()+"','"+
                        dob.getText() + "'," + address.getText() + ");");
                showMessage("Successful", "Record added");
                clearText();
            }
        }
    }
}
 private void showMessage(String title, String msg) {
  // code
 } 
 public void Clear() {
  // code
 } 

You can use ContentValue class to insert data into table. This will also prevent from SQL injection .

Also you need to use insert() in SQLiteDatabase .

ContentValues values = new ContentValues();
values.put(COL_2, KhId.getText().toString());
values.put(COL_3, Fname.getText().toString());
values.put(COL_4, Lname.getText().toString());
values.put(COL_5, sex.getText().toString());
values.put(COL_6, dob.getText().toString());
values.put(COL_7, address.getText().toString());

long res = db.insert(TABLE_NAME, null, values);

Lastly, keep remember to use proper naming conventions .

db.execSQL("INSERT INTO user VALUES(" + KhId.getText().tostring()+ ",'" + Fname.getText().tostring() + "','" + Lname.getText().tostring()+"','"+ sex.getText().tostring()+"','"+
                        dob.getText().tostring() + "'," + address.getText().tostring() + ");");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM