我无法插入数据

I can't insert data

我正在制作一个插入数据的应用程序。但是当我通过提供所有详细信息单击添加按钮时。 App return 我到上一页 这是我创建插入的方式class

    public class InsertStudent extends AppCompatActivity {

            Button instudent;
            DBHelper dbHelper;
            EditText sName,sDOB,sAddress;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_insert_student);

                instudent = findViewById(R.id.btninsert);
                sName = findViewById(R.id.insertname);
                sDOB = findViewById(R.id.insertdob)

;
            sAddress = findViewById(R.id.insertaddress);

下面是我编码插入数据的方式

instudent.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    String userName = sName.getText().toString();
                    String dateB = sDOB.getText().toString();
                    String addr = sAddress.getText().toString();

                    boolean count = dbHelper.addInfo(userName,dateB,addr );

                    if(count =true){
                        Toast.makeText(InsertStudent.this, "Inserted!", Toast.LENGTH_SHORT).show();
                    }
                    else{
                        Toast.makeText(InsertStudent.this, "Something went wrong!", Toast.LENGTH_SHORT).show();
                    }
                }
            });

这是DBHelper中的addinfo方法class

public boolean addInfo(String stdName, String stdDOB, String stdAddress){

        SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();

        ContentValues contentValues = new ContentValues();
        contentValues.put(UserProfile.Users.COLUMN_STDNAME, stdName);
        contentValues.put(UserProfile.Users.COLUMN_DATEOFBIRTH, stdDOB);
        contentValues.put(UserProfile.Users.TABLE_ADDRESS, stdAddress);


        long result = sqLiteDatabase.insert(UserProfile.Users.TABLE_NAME, null, contentValues);
        if(result==1)
            return false;
        else
            return  true;

    }
}
  1. "SQLiteDatabase"class的插入方法没有return 计数,它是 returns 插入行的 id。所以你正在检查 如果 return 结果为 1,这是一个真正的过程,但它不是一种方法 检查插入方法。这意味着你需要检查是否有 return 结果,您的插入操作成功执行,但如果 有问题,应用程序会崩溃。

  2. 确保您创建了要在其中插入数据的 table。