Answer the question
In order to leave comments, you need to log in
How to fix error when starting Sqlite database?
Good afternoon! I want to make my first simple sqlite program for android. But when adding an element, an error occurs:
E/SQLiteLog: (1) table tbl_student has no column named id in "INSERT INTO tbl_student(id,name,email) VALUES (?,?,?)"
E/SQLiteDatabase: Error inserting id= 1 name=jjj email=jjj
android.database.sqlite.SQLiteException: table tbl_student has no column named id (code 1 SQLITE_ERROR): , while compiling: INSERT INTO tbl_student(id,name,email) VALUES (?,?,?)
class MainActivity : AppCompatActivity() {
private lateinit var edName: EditText
private lateinit var edEmail: EditText
private lateinit var btnAdd: Button
private lateinit var btnView: Button
private lateinit var sqLiteHelper: SQLiteHelper
private lateinit var d1: SQLiteHelper
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
d1 = SQLiteHelper(this)
d1.insertStudent(StudentModel(1,"jjj", "jjj"))
initView()
//sqLiteHelper = SQLiteHelper(this)
btnAdd.setOnClickListener{ addStudent()}
btnView.setOnClickListener{ getStudent()}
}
private fun getStudent() {
val stdList = sqLiteHelper.getAllStudent()
Log.e("pppp", "${stdList.size}")
}
private fun addStudent() {
val name = edName.text.toString()
val email = edEmail.text.toString()
if(name.isEmpty() || email.isEmpty()){
Toast.makeText(this, "Please enter required field ", Toast.LENGTH_SHORT).show()
}else{
val std = StudentModel(name = name, email = email)
val status = sqLiteHelper.insertStudent(std)
if(status > -1){
Toast.makeText(this, "Student Added...", Toast.LENGTH_SHORT).show()
clearEdiText()
}else{
Toast.makeText(this, "Record not saved", Toast.LENGTH_SHORT).show()
}
}
}
private fun clearEdiText() {
edName.setText("")
edEmail.setText("")
edName.requestFocus()
}
private fun initView(){
edName = findViewById(R.id.edName)
edEmail = findViewById(R.id.adEmail)
btnAdd = findViewById(R.id.bntAdd)
btnView = findViewById(R.id.btnView)
}
}
class SQLiteHelper(context: Context) : SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION){
companion object{
private const val DATABASE_VERSION = 1
private const val DATABASE_NAME = "student.db"
private const val TBL_STUDENT = "tbl_student"
private const val ID = "id"
private const val NAME = "name"
private const val EMAIL = "email"
}
override fun onCreate(db: SQLiteDatabase?) {
val createTblStudent = ("CREATE TABLE " + TBL_STUDENT + "(" + ID
+ " INTEGER PRIMARY KEY, " + NAME + " TEXT," + EMAIL + " TEXT" + ")")
db?.execSQL(createTblStudent)
}
override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
db!!.execSQL("DROP TABLE IF EXISTS $TBL_STUDENT")
onCreate(db)
}
fun insertStudent(std: StudentModel): Long{
val db = this.writableDatabase
val contextValue = ContentValues()
contextValue.put(ID, std.id)
contextValue.put(NAME, std.name)
contextValue.put(EMAIL, std.email)
val success = db.insert(TBL_STUDENT, null, contextValue)
db.close()
return success
}
fun getAllStudent(): ArrayList<StudentModel>{
val stdList: ArrayList<StudentModel> = ArrayList()
val selectQuery = "SELECT * FROM $TBL_STUDENT"
val db = this.readableDatabase
val cursor: Cursor?
try{
cursor = db.rawQuery(selectQuery, null)
}catch (e: Exception){
e.printStackTrace()
db.execSQL(selectQuery)
return ArrayList()
}
var id: Int
var name:String
var email: String
if(cursor.moveToFirst()){
do{
id = cursor.getInt(cursor.getColumnIndex("id"))
name = cursor.getString(cursor.getColumnIndex("name"))
email = cursor.getString(cursor.getColumnIndex("email"))
val std = StudentModel(id = id, name = name, email = email)
stdList.add(std)
}while (cursor.moveToNext())
}
return stdList
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question