【发布时间】:2021-01-19 14:34:44
【问题描述】:
您好,我正在尝试将一些数据放入我的数据库表中。我首先尝试使用助手类。但类似的错误再次发生。所以我尝试了这个简单的方法,但是我这个时候报错了。
我尝试重新安装应用,但没有成功。
注意:这是一个时间表应用程序的添加 shadulle 片段。 SQL插入代码在最后。
错误:
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)
代码:
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)")
}
}
}
【问题讨论】:
-
问题是您将“schedule_title”逐字传递,而不是作为变量传递。最可能的最短方法是动态构建查询字符串,但是您会错过一堆功能。你考虑过准备好的陈述吗? sqlite.org/c3ref/….
-
谢谢。你是对的,我现在使用查询字符串,但我要研究准备好的语句。
标签: android sqlite android-studio kotlin android-sqlite