Mayank Aggarwal
Mayank Aggarwal

Reputation: 139

Error while submitting email address to SQLite Database

I am storing the info entered by the user when he registers on the app in a SQLite Database but the app crashes with an error which seems to occur when I submit the email address. If that is the reason, can you tell me how to rectify it? If not, please tell me the correct reason and the solution.

My java file:

public class MainActivity extends AppCompatActivity {

AutoCompleteTextView txtmobileno, txtemail, txtname,txtuname;
EditText txtpass;
private SQLiteDatabase sqLiteDatabase;

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

    constraintLayout = (ConstraintLayout)findViewById(R.id.constraintlayout);

    txtemail = (AutoCompleteTextView) findViewById(R.id.txtemail);
    txtmobileno = (AutoCompleteTextView) findViewById(R.id.txtmobileno);
    txtname = (AutoCompleteTextView) findViewById(R.id.txtname);
    txtuname = (AutoCompleteTextView)findViewById(R.id.txtuname);
    txtpass = (EditText)findViewById(R.id.txtpass);

    btnSignUp = (ImageButton) findViewById(R.id.btnSignUp);
    btnClear = (ImageButton) findViewById(R.id.btnClear);

    createDatabase();

    btnSignUp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String email = txtemail.getText().toString();
            String mobileno = txtmobileno.getText().toString();
            String name = txtemail.getText().toString();
            if (name.trim().equals(""))
                txtname.setError("Please enter your name");
            else if (email.trim().equals(""))
                txtemail.setError("Please enter email Address");
            else if (!email.contains("@") || !email.contains("."))
                txtemail.setError("Please enter a valid email Address");
            else if (mobileno.trim().length() < 10)
                txtmobileno.setError("Not a valid mobile no"); // I'm finding solution to validate mobile no
            else {
                insertIntoDB();
                Snackbar snackbar = Snackbar
                        .make(constraintLayout, "Success", Snackbar.LENGTH_LONG)
                        .setAction("HIDE", new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                            }
                        });
                snackbar.show();
            }
        }
    });

    btnClear.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            txtemail.setText("");
            txtmobileno.setText("");
            txtname.setText("");
            txtname.setError(null);
            txtemail.setError(null);
            txtmobileno.setError(null);
        }
    });

    txtmobileno.setOnEditorActionListener(new EditText.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

            if(actionId == EditorInfo.IME_ACTION_DONE){
                btnSignUp.performClick();
                return true;
            }
            return false;
        }
    });
}

private void createDatabase(){
    sqLiteDatabase = openOrCreateDatabase("UserDatabase", Context.MODE_PRIVATE,null);
    //sqLiteDatabase.execSQL("IF EXISTS (SELECT * FROM USERS) DROP TABLE USERS");
    sqLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS USERS(ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, NAME VARCHAR(30)," +
            "USERNAME VARCHAR(15), EMAIL TEXT, PASSWORD VARCHAR(15), MOBILE VARCHAR(11))");
}

protected void insertIntoDB(){
    String name, uname, email, password, mobile;
    name = txtname.getText().toString();
    uname = txtuname.getText().toString();
    email = txtemail.getText().toString();
    password = txtpass.getText().toString();
    mobile = txtmobileno.getText().toString();

    String query = "INSERT INTO USERS VALUES(" + name +"," + uname + "," + email + "," + password + "," + mobile + ");";

    sqLiteDatabase.execSQL(query);
}
}

EDIT: Added error log

Error log

Upvotes: 1

Views: 2673

Answers (3)

Prince Patel
Prince Patel

Reputation: 77

String query = "INSERT INTO USERS VALUES(" + name +"," + uname + ",'" + email + "'," + password + "," + mobile + ");";

Try this code. Add '' between single quote.

Hope it helps

Upvotes: 1

Denny
Denny

Reputation: 1783

Your core error is that for the insert query you are not enclosing the values to be inserted, in quotes. Your query, after construction, looks something like this:

insert into TABLE values([email protected])

When it should be something like this:

insert into TABLE values('[email protected]')

The SQL parser chokes while trying to parse your current query, because the syntax is incorrect.

Try to use prepared sql queries, for example:

String sql = "INSERT INTO table_name (column_1, column_2) VALUES (?, ?)";
SQLiteStatement statement = db.compileStatement(sql);

int intValue = 57;
String stringValue = "hello";

statement.bindLong(1, intValue); // These match to the two question marks in the sql string
statement.bindString(2, stringValue); 

long rowId = statement.executeInsert();

Or even better use SQLiteOpenHelper where you can do this for example:

ContentValues insertValues = new ContentValues();
insertValues.put("EMAIL", "[email protected]");
db.insert("CashData", null, insertValues);

Upvotes: 3

Ashish P
Ashish P

Reputation: 3056

Try to insert data to Database by below function and double check is email field really created or not.And below function is a just an example.

public void InsertToOppoStats(ArrayList<OppoStats> bean) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues userValues = new ContentValues();

    for (int i = 0; i < bean.size(); i++) {
         ----------------------------------------
        userValues.put("column name", "value");
         ----------------------------------------

        userValues.put("id", bean.get(i).getID();
        userValues.put("name", bean.get(i).getName());
        userValues.put("std", bean.get(i).getStd());
        userValues.put("city", bean.get(i).getCity());

    }

    db.insert("xyz_table", null, userValues);

}

Upvotes: 1

Related Questions