将数据插入 SQLite 时出现问题-android

Problem with inserting data into SQLite-android

我用它创建一个 table 用于在我的项目中添加帐户类型组

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

我用它来将数据插入 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;
}

在 activity,我有这个。我尝试通过从编辑文本

中获取文本来将数据插入 table
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();
            }
        });


    }

    }

当我单击按钮将数据插入 table 时,没有任何反应。并且没有错误发生。 table 中没有添加任何内容。 有人可以帮助我。请

谢谢

错误的确切原因似乎是 VALUES 子句中两个文字中的第二个没有用单引号引起来。我们可以直接补救这个问题,但更好的长期解决方法是使用准备好的语句:

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