Search code examples
androidsqliteandroid-studiokotlinandroid-sqlite

Kotlin SQLite "(code 1 SQLITE_ERROR): , while compiling: INSERT INTO"


Hi i'm trying to put some Data to my database table. I tried as first to make with helper class. But similar errors happened again. So I tried this simple method, but I taken this error at this time.

I tried to reinstall app but it didn't work.

Note : This is a add shadulle fragment of a Timetable app. The SQL insert code on the end.

ERROR:

    android.database.sqlite.SQLiteException: no such column: schedule_title (code 1 SQLITE_ERROR): , while compiling: INSERT INTO schedules (schedule_name, schedule_info, schedule_day, schedule_start_time, schedule_end_time, schedule_color) VALUES (schedule_title, schedule_info, schedule_save_day, start_time, end_time, schedule_color)
    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:986)
    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:593)
    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:590)

CODES:

class add_schedule : Fragment() {

private lateinit var alertDialog: AlertDialog
private lateinit var sqlite_database :SQLiteDatabase
var start_time = "08:00"
var end_time = "09:00"
var schedule_display_day = "Monday"
var schedule_save_day = ScheduleDay.MONDAY
var schedule_title = "Title"
var schedule_color = "#ffffff"
var schedule_info = "Info"
lateinit var new_schedule: ScheduleEntity
private var selectedColor: Int = ColorSheet.NO_COLOR
private var noColorOption = false
lateinit var colors: IntArray

companion object {
    private const val COLOR_SELECTED = "selectedColor"
    private const val NO_COLOR_OPTION = "noColorOption"
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    arguments?.let {

    }
}

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    return inflater.inflate(R.layout.fragment_add_schedule, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    sqlite_database = this.requireContext().openOrCreateDatabase("main_database", MODE_PRIVATE, null)
    sqlite_database.execSQL("CREATE TABLE IF NOT EXISTS schedules ( schedule_id INTEGER PRIMARY KEY, schedule_name VARCHAR(256), schedule_info VARCHAR(256), schedule_day INTEGER, schedule_start_time VARCHAR(256), schedule_end_time VARCHAR(256), schedule_color VARCHAR(256))")


    colors = resources.getIntArray(R.array.colors)
    selectedColor = savedInstanceState?.getInt(COLOR_SELECTED) ?: colors.first()
    noColorOption = savedInstanceState?.getBoolean(NO_COLOR_OPTION) ?: false

    select_day_button.setOnClickListener {
        show_day_picker()
    }
    schedule_start_time_text.setOnClickListener {
        show_start_time_picker()
    }
    schedule_end_time_text.setOnClickListener {
        show_end_time_picker()
    }
    schedule_color_layout.setOnClickListener {
        show_color_picker()
    }
    schedule_add_button.setOnClickListener {
        add_schedule_fun()
    }

}

    //Other Funcs
    private fun show_start_time_picker () ....

    private fun show_end_time_picker () ....

    private fun show_day_picker() ....

    private fun show_color_picker() ....



    //save schedule to database.
    private fun add_schedule_fun(){

    schedule_title = schedule_title_edit_text.text.toString()
    schedule_info = schedule_info_edit_text.text.toString()

    if (schedule_title.isNotEmpty() &&
        schedule_info.isNotEmpty()
    ) {
        sqlite_database.execSQL("INSERT INTO schedules (schedule_name, schedule_info, schedule_day, schedule_start_time, schedule_end_time, schedule_color) VALUES (schedule_title, schedule_info, schedule_save_day, start_time, end_time, schedule_color)")

    }

}

}


Solution

  • I solved the issue by add query string. Thanks for help to g_bor.

            sqlite_database = this.requireContext().openOrCreateDatabase("main_database", MODE_PRIVATE, null)
    
            val create_table_if_not_exist_query = "CREATE TABLE IF NOT EXISTS schedules " +
                    "('schedule_id' INTEGER PRIMARY KEY, " +
                    "'schedule_name' VARCHAR, " +
                    "'schedule_info' VARCHAR, " +
                    "'schedule_day' INTEGER, " +
                    "'schedule_start_time' VARCHAR, " +
                    "'schedule_end_time' VARCHAR, " +
                    "'schedule_color' VARCHAR)"
            sqlite_database.execSQL(create_table_if_not_exist_query)
    
            val insert_into_query = "INSERT INTO schedules " +
                    "( 'schedule_name', 'schedule_info', 'schedule_day', 'schedule_start_time', 'schedule_end_time', 'schedule_color')" +
                    " VALUES " +
                    "( '$schedule_title', '$schedule_info', '$schedule_save_day', '$start_time', '$end_time', '$schedule_color')"
            sqlite_database.execSQL(insert_into_query)