无法在android中将数据插入sqlite

时间:2017-07-29 05:39:17

标签: java android sqlite

我尝试将EditView中的数据插入到Sqlite中,但它无法插入,然后关闭程序。如果我没有输入任何值,则我的条件代码正常工作。填写所有字段并单击按钮插入后,它不起作用。我的插入代码有错误吗?

以下是我的java代码

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
 } 

2 个答案:

答案 0 :(得分:3)

您可以使用ContentValue类将数据插入表中。这也会阻止SQL injection

您还需要在SQLiteDatabase中使用insert()

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);

最后,请记住使用正确的naming conventions

答案 1 :(得分:-2)

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