每当我尝试运行我的应用程序时,它都会关闭,并且没有错误的表格待办事项。.
package com.vineet.tododatabase
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.ArrayAdapter
import com.vineet.tododatabase.db.MyDbHelper
import com.vineet.tododatabase.db.TodoTable
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
val todos = ArrayList<String>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val todosAdapter = ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
todos
)
val db = MyDbHelper(this).writableDatabase
lvItems.adapter = todosAdapter
fun refreshTodoList(){
val todoList = TodoTable.getAllTodos(db)
Log.d("TODOS", todoList.toString())
}
btnAdd.setOnClickListener {
val newTodo= Todo(
etTask.text.toString(),
false
)
TodoTable.insertTodos(db, newTodo)
refreshTodoList()
}
}
}
package com.vineet.tododatabase.db
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
class MyDbHelper(context: Context): SQLiteOpenHelper(
context,
"todos.db",
null,
1
){
override fun onCreate(p0: SQLiteDatabase?) {
}
override fun onUpgrade(p0: SQLiteDatabase?, p1: Int, p2: Int) {
}
}
package com.vineet.tododatabase.db
import android.content.ContentValues
import android.database.sqlite.SQLiteDatabase
import com.vineet.tododatabase.Todo
object TodoTable{
val TABLE_NAME = "todos"
object Columns{
val ID = "id"
val TASK = "task"
val DONE = "done"
}
val CMD_CREATE_TABLE = """
CREATE TABLE IF NOT EXIST $TABLE_NAME
(
${Columns.ID} INTEGER PRIMARY KEY AUTOINCREMENT,
${Columns.TASK} TEXT,
${Columns.DONE} BOOLEAN
);
""".trimIndent()
fun insertTodos(db: SQLiteDatabase, todo: Todo){
val row = ContentValues()
row.put(Columns.TASK, todo.task)
row.put(Columns.DONE, todo.done)
db.insert(TABLE_NAME,null, row)
}
fun getAllTodos(db: SQLiteDatabase): ArrayList<Todo>{
val todos = ArrayList<Todo>()
var c = db.query(
TABLE_NAME,
arrayOf(Columns.ID, Columns.TASK, Columns.DONE),
null, null,
null, null,
null
)
while (c.moveToNext()){
var todo = Todo(c.getString(1), c.getInt(2) == 1)
todos.add(todo)
}
return todos
}
}
package com.vineet.tododatabase
data class Todo(var task: String, var done: Boolean)
我不知道为什么会收到此错误
E / SQLiteLog:(1)没有这样的表:todos E / SQLiteDatabase:错误 插入task = gg done = false android.database.sqlite.SQLiteException:没有这样的表:todos(代码1):,而在编译时:INSERT INTO todos(任务,完成) 值(?,?) 在android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native 方法) 在android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:890) 在android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:501) 在android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) 在android.database.sqlite.SQLiteProgram。(SQLiteProgram.java:58) 在android.database.sqlite.SQLiteStatement。(SQLiteStatement.java:31) 在android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1546) 在android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1417) 在com.vineet.tododatabase.db.TodoTable.insertTodos(TodoTable.kt:30) 在com.vineet.tododatabase.MainActivity $ onCreate $ 2.onClick(MainActivity.kt:40) 在android.view.View.performClick(View.java:6294) 在android.view.View $ PerformClick.run(View.java:24770) 在android.os.Handler.handleCallback(Handler.java:790) 在android.os.Handler.dispatchMessage(Handler.java:99) 在android.os.Looper.loop(Looper.java:164) 在android.app.ActivityThread.main(ActivityThread.java:6494) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:438) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) E / SQLiteLog:(1)没有这样的表:todos
答案 0 :(得分:0)
在dbHelper的onCreate方法中执行创建表查询以创建表...然后它将尝试从表中获取数据
p0.execSQL("Create table todos (blah,blah,blah)")