1 year ago

#319862

test-img

Christopher Chua

Passing NULL value into the SQL server with Android Studio

How do I accept null values into the SQL Server database with Kotlin? I am trying to make that if the user does not input anything into the column, it will return null instead in the SQL Server. But I failed to do so and a bunch of numbers is saved instead, may I know how can I do it? Below are my codes and the input I got from SQL Server. For example: 2131231267 and 2131231270 is saved instead of NULL.

fun createGoals()
 {
     val sqlCon = SQLCon()
     checkGoal()
     checkTargetAmount()
     checkSavedAmount()
     checkNote()
     if (checkGoal() == true && checkTargetAmount() == true){
         Toast.makeText(this, "Creating Goals...", Toast.LENGTH_LONG).show()
         connection = sqlCon.connectionClass()!!
         if(connection == null)
         {
             Toast.makeText(this, "Failed to make connection", Toast.LENGTH_LONG).show()

         }
         else {
             try{
                 val sql :String  =
                     "INSERT INTO Budget(gName,sAmount,Note,tAmount) VALUES ('" + binding.txtGoal.text.toString() + "'" +
                             ",'" + Integer.valueOf(R.id.txtSavedAmount.toString()) + "'" +
                             ",'" + R.id.txtNote.toString()+ "'" +
                             ",'" + Integer.valueOf(binding.txtTargetAmount.text.toString().trim()) + "')"
                 statement = connection!!.createStatement()
                 statement!!.executeUpdate(sql)
                 Toast.makeText(this, "Going In ", Toast.LENGTH_LONG).show()
             }
             catch (e : Exception){
                 Log.e("Error", e.message!!)
             }
             status = true
         }
     }
}

private fun checkNote() : String?
{
    var Note : String? = R.id.txtNote.toString()
    if(Note.isNullOrEmpty())
    {
        Note = " "
    }
    return Note
}

private fun checkSavedAmount() : String?
{
    var savedAmount: String? = R.id.txtSavedAmount.toString()
    if(savedAmount.isNullOrEmpty())
    {
        savedAmount = " "
    }
    return savedAmount
}

sql-server

kotlin

sql-null

0 Answers

Your Answer

Accepted video resources