我该如何解决这些问题?
进程:com.example.burhanozen.sqlitedb,PID:4658
android.database.sqlite.SQLiteException:在“ INTEGER”附近:语法错误(代码1):,在编译时:CREATE TABLE
students_table(ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,SURNAME
文字,标记整数
-at com.example.burhanozen.sqlitedb.dbHelper.onCreate(dbHelper.kt:16)
-at com.example.burhanozen.sqlitedb.dbHelper.insertData(dbHelper.kt:28)
-at com.example.burhanozen.sqlitedb.MainActivity $ addData $ 1.onClick(MainActivity.kt:62)
这是主要活动:
class MainActivity : AppCompatActivity() {
internal val helper = dbHelper(this)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
addData()
viewAll()
}
fun viewAll(){
kayitGoster.setOnClickListener{
val res = helper.allData
if(res.count == 0){
showMessage("Hata","Veri bulunamadı")
return@setOnClickListener
}
val buffer = StringBuffer()
while(res.moveToNext()){
buffer.append("Id : " + res.getString(0) + "\n")
buffer.append("Name : " + res.getString(1) + "\n")
buffer.append("Surname : " + res.getString(2) + "\n")
buffer.append("Mark : " + res.getString(3) + "\n")
}
}
}
fun showMessage(title:String, message:String){
val builder = AlertDialog.Builder(this)
builder.setCancelable(true)
builder.setTitle(title)
builder.setMessage(message)
builder.show()
}
fun addData(){
gonderButon.setOnClickListener {
helper.insertData(
editisim.text.toString(),
soyisim.text.toString(),
not.text.toString())
}
}
}
这是dbHelper.kt
class dbHelper (context : Context) : SQLiteOpenHelper(context,DATABASE_NAME,null,1) {
override fun onCreate(db: SQLiteDatabase) {
db.execSQL("CREATE TABLE $TABLE_NAME (ID INTEGER PRIMARY KEY AUTOINCREMENT," +
"NAME TEXT," +
"SURNAME TEXT," +
"MARK INTEGER")
}
override fun onUpgrade(db:SQLiteDatabase,oldVersion: Int, newVersion: Int){
db.execSQL("DROP TABLE IF EXISTS $TABLE_NAME")
onCreate(db)
}
fun insertData(name:String, surname:String, marks:String){
val db=this.writableDatabase
val contentValues = ContentValues()
contentValues.put(COL_NAME,name)
contentValues.put(COL_SURN,surname)
contentValues.put(COL_NOT,marks)
db.insert(TABLE_NAME,null,contentValues)
}
val allData: Cursor
get(){
val db=this.writableDatabase
val res=db.rawQuery("select * from $TABLE_NAME",null)
return res
}
companion object{
internal val DATABASE_NAME = "students.db"
internal val TABLE_NAME = "students_table"
internal val COL_ID = "ID"
internal val COL_NAME = "NAME"
internal val COL_SURN = "SURNAME"
internal val COL_NOT = "MARKS"
}
}
最佳答案
您在SQL语句中缺少右括号。您现在拥有的是:
Create(db: SQLiteDatabase) {
db.execSQL("CREATE TABLE $TABLE_NAME (ID INTEGER PRIMARY KEY AUTOINCREMENT," +
"NAME TEXT," +
"SURNAME TEXT," +
"MARK INTEGER")
必须是:
Create(db: SQLiteDatabase) {
db.execSQL("CREATE TABLE $TABLE_NAME (ID INTEGER PRIMARY KEY AUTOINCREMENT," +
"NAME TEXT," +
"SURNAME TEXT," +
"MARK INTEGER)")