Problem with inserting data into SQLite-android Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Problem with inserting data into SQLite-android without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I use this to create a table for add account type group in my project

db.execSQL("CREATE TABLE tbl_groupAccount ( n" +
        "    ID                 INTEGER PRIMARY KEY AUTOINCREMENTn" +
        "                               NOT NULLn" +
        "                               UNIQUE,n" +
        "    ACCOUNT_GROUP_NAME VARCHAR,n" +
        "    ACCOUNT_GROUP_TYPE  VARCHAR n" +
        ");");

and I use this to insert data into the table

public boolean addAccountGroup(String nameAccountGroup, String groupAccountType) {
    boolean result;
    String sql = "INSERT INTO tbl_groupAccount (ACCOUNT_GROUP_NAME,ACCOUNT_GROUP_TYPE)" +
            " VALUES ('" + nameAccountGroup + "'," + groupAccountType + ")";
    try {
        SQLiteDatabase database = this.getWritableDatabase();
        database.execSQL(sql);
        database = null;
        result = true;
    } catch (Exception ex) {
        result = false;
    }
    return result;
}

at activity, I have this. I try to insert data into the table, by getting a text from edit text

public class AddGroupAccountActivity extends AppCompatActivity {
    DatabaseForAccounting database;
    String accountNameGroup;
    String accountGroupType ;
    int idAccountGroup;

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


        database = new DatabaseForAccounting(this);
        final EditText etAddGroupAccount = findViewById(R.id.etAddGroupAccount);
        Intent intent  = getIntent();
        if (getIntent().getExtras() != null && getIntent().getExtras().containsKey("accountNameGroup")) {
            accountNameGroup =  intent.getStringExtra("accountNameGroup");
            accountGroupType = intent.getStringExtra("accountGroupType");
            idAccountGroup = intent.getIntExtra("idAccountGroup",1);
            etAddGroupAccount.setText(accountNameGroup);

        }

        ImageView img_Ok =  findViewById(R.id.image_OK);
        img_Ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

               database.addAccountGroup(etAddGroupAccount.getText().toString(),"asset");

                finish();

            }
        });

        ImageView img_cancel = findViewById(R.id.image_Cancel);
        img_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


    }

    }

When I click on the button to insert data into the table, nothing happens. And no errors occur. And nothing is added to the table. Someone can help me. Please

Thanks

Answer

The exact cause of the error appears to be that the second of two literals in your VALUES clause is not surrounded in single quotes. We could remedy this directly, but a better long term fix would be to use a prepared statement:

try {
    SQLiteDatabase database = this.getWritableDatabase();
    String sql = "INSERT INTO tbl_groupAccount (ACCOUNT_GROUP_NAME, ACCOUNT_GROUP_TYPE) VALUES (?, ?)";
    SQLiteStatement stmt = database.compileStatement(sql);
    stmt.bindString(1, nameAccountGroup);
    stmt.bindString(2,  groupAccountType);
    stmt.executeInsert();
    result = true;
}
catch (Exception ex) {
    result = false;
}
We are here to answer your question about Problem with inserting data into SQLite-android - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji