我该如何解决这些问题?
处理:com.example.burhanozen.sqlitedb,PID:4658 android.database.sqlite.SQLiteException:near“INTEGER”:语法错误(代码1):,编译时:CREATE TABLE students_table(ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,SURNAME TEXT,MARK INTEGER
-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"
}
}
答案 0 :(得分:1)
您缺少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)")