Well… I don’t know where to start with this… I’m doing a course where the teacher has not taught us anything about databases and now he want us to do an app with kotlin and sqlite where we make an Activity with a button "New table" where user can create a database table with a name and dynamic fields. I’ve been searching all day about it and I got nothing.
I thought about create a CRUD but I’m with the trouble that I don’t know how to make any of this dynamically.
For now I have this SQLiteHelper that I saw it was necessary to make and I put const values to test it because I don’t have any clue how to pass the values from the user view.
class AdminSQLiteOpenHelper(context: Context, name: String, factory: CursorFactory?, version: Int) : SQLiteOpenHelper(context, name, factory, version) {
companion object{
const val DATABASE_NAME = "test.db"
var TABLE_NAME = "Testing_Table"
const val COL_1 = "ID"
var COL_2 = "NAME"
var COL_3 = "SURNAME"
var COL_4 = "YEARS"
}
override fun onCreate(db: SQLiteDatabase) {
db.execSQL("create table TABLE_NAME(COL_1 primary key autoincrement, COL_2 text, COL_3 text, COL_4 int)")
}
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
db.execSQL("drop table if exists TABLE_NAME")
onCreate(db)
}
}
And I have this activity
class MainActivity2Crear : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main_activity2_crear)
val buttonCrear = findViewById<Button>(R.id.buttonCrear)
buttonCrear.setOnClickListener{
val register = ContentValues()
register.put(COL_1, textView.getText().toString())
TABLE_NAME = register.toString()
val admin = AdminSQLiteOpenHelper(this, TABLE_NAME, null, 1)
val bd = admin.writableDatabase
bd.insert(TABLE_NAME, null, register)
bd.close()
textView.setText("")
Toast.makeText(this, "Table created", Toast.LENGTH_SHORT).show()
val intent = Intent(this, MainActivity2::class.java)
startActivity(intent)
}
val button = findViewById<Button>(R.id.button)
button.setOnClickListener{
val intent = Intent(this, MainActivity2::class.java)
startActivity(intent)
}
}
}
I just did create button for now because I don’t know what I’m doing…
I know probably this question will have downvotes but I just want some help or a tutorial or something that help me to understand how to make this exercise…
Source: Android Studio Questions