B. Ozen B. Ozen - 6 months ago 17
Android Question

Android SQLite Database CREATE TABLE Error

How can I solve these problems?

Process: com.example.burhanozen.sqlitedb, PID: 4658
android.database.sqlite.SQLiteException: near "INTEGER": syntax error (code 1): , while compiling: 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)

This is Main Activity:

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())

}

}


}

And here is 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"
}


}

Answer Source

You are missing the close parenthesis in your SQL statement. What you have right now is:

Create(db: SQLiteDatabase) {
  db.execSQL("CREATE TABLE $TABLE_NAME (ID INTEGER PRIMARY KEY AUTOINCREMENT," +
        "NAME TEXT," +
        "SURNAME TEXT," +
        "MARK INTEGER")

That needs to be:

Create(db: SQLiteDatabase) {
  db.execSQL("CREATE TABLE $TABLE_NAME (ID INTEGER PRIMARY KEY AUTOINCREMENT," +
        "NAME TEXT," +
        "SURNAME TEXT," +
        "MARK INTEGER)")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download