estoy realizando una app de tareas con SQLite para aprender acerca de Kotlin y Android Studio. El problema que tengo es a la hora de añadir las tareas a la base de datos, ya que recibo este error:
E/SQLiteLog: (1) near "androidx": syntax error in "INSERT INTO tareas (titulo, descripcion, fecha) VALUES androidx.appcompat.widget.AppCompatEditText{aec1dc9 VFED..CL. .F...... 184,550-896,693 #7f0801b8 app:id/descripcion_aña D/AndroidRuntime: Shutting down VM E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.listadetareas, PID: 25201 android.database.sqlite.SQLiteException: near "androidx": syntax error (code 1 SQLITE_ERROR): , while compiling: INSERT INTO tareas (titulo, descripcion, fecha) VALUES androidx.appcompat.widget.AppCompatEditText{aec1dc9 VFED..CL. .F...... 184,550-896,693 #7f0801b8 app:id/descripcion_añadir_tarea aid=1073741825}, androidx.appcompat.widget.AppCompatEditText{aec1dc9 VFED..CL. .F...... 184,550-896,693 #7f0801b8 app:id/descripcion_añadir_tarea aid=1073741825}, 30/5/2021 at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1045) at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:652) at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:590) at android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:61) at android.database.sqlite.SQLiteStatement.(SQLiteStatement.java:33) at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1919) at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1841) at com.example.listadetareas.FeedReaderDbHelper.añadirTarea(DB.kt:45) at com.example.listadetareas.AnadirUsuarios.onCreate$lambda-0(AnadirUsuarios.kt:32) at com.example.listadetareas.AnadirUsuarios.lambda$fysUFCr2_t7Ey5OcDMCqD4gKBRk(Unknown Source:0) at com.example.listadetareas.-$$Lambda$AnadirUsuarios$fysUFCr2_t7Ey5OcDMCqD4gKBRk.onClick(Unknown Source:4) at android.view.View.performClick(View.java:7448) at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119) at android.view.View.performClickInternal(View.java:7425) at android.view.View.access$3600(View.java:810) at android.view.View$PerformClick.run(View.java:28305) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:223) at android.app.ActivityThread.main(ActivityThread.java:7656) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Este es el archivo Anadirusuarios.kt
class AnadirUsuarios : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.anadir_usuarios);
val recuperar_year = intent.getIntExtra("year", 2021).toString()
val recuperar_month = intent.getIntExtra("month", 1).toString()
val recuperar_day= intent.getIntExtra("day", 1).toString()
val fecha = findViewById<TextView>(R.id.fecha_tarea);
val btn_añadir_tarea = findViewById<Button>(R.id.btn_añadir_tarea)
val date = "$recuperar_day/$recuperar_month/$recuperar_year";
fecha.text = date;
btn_añadir_tarea.setOnClickListener {
val titulo_añadir_tarea = findViewById<EditText>(R.id.titulo_añadir_tarea).toString()
val descripcion_añadir_tarea = findViewById<EditText>(R.id.descripcion_añadir_tarea).toString()
if (titulo_añadir_tarea != null && descripcion_añadir_tarea != null){
var dbHelper = FeedReaderDbHelper(applicationContext);
dbHelper.añadirTarea(descripcion_añadir_tarea, descripcion_añadir_tarea, date)
Toast.makeText(applicationContext, "La tarea $descripcion_añadir_tarea ha sido añadida correctamente", Toast.LENGTH_LONG).show()
}
}
}
}
El archivo de las consultas de base de datos(DB.kt):
class FeedReaderDbHelper(context: Context) : SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION) {
private val SQL_CREATE_ENTRIES =
"CREATE TABLE " + DB_settings.TABLE_NAME +
"(id INTEGER PRIMARY KEY AUTOINCREMENT," +
"${DB_settings.COLUMN_NAME_TITULO} VARCHAR," +
"${DB_settings.COLUMN_NAME_DESCRIPCION} VARCHAR)" +
"${DB_settings.COLUMN_NAME_FECHA} VARCHAR)"
private val SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS ${DB_settings.TABLE_NAME}"
override fun onCreate(db: SQLiteDatabase) {
db.execSQL(SQL_CREATE_ENTRIES)
}
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
// This database is only a cache for online data, so its upgrade policy is
// to simply to discard the data and start over
db.execSQL(SQL_DELETE_ENTRIES)
onCreate(db)
}
override fun onDowngrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
onUpgrade(db, oldVersion, newVersion)
}
companion object {
// If you change the database schema, you must increment the database version.
const val DATABASE_VERSION = 1
const val DATABASE_NAME = "FeedReader.db"
}
object DB_settings{
const val TABLE_NAME = "tareas"
const val COLUMN_NAME_TITULO = "titulo"
const val COLUMN_NAME_DESCRIPCION = "descripcion"
const val COLUMN_NAME_FECHA = "fecha"
}
fun añadirTarea(titulo: String, description: String, fecha: String ){
val db = writableDatabase
db.execSQL("INSERT INTO ${DB_settings.TABLE_NAME} (${DB_settings.COLUMN_NAME_TITULO}, ${DB_settings.COLUMN_NAME_DESCRIPCION}, ${DB_settings.COLUMN_NAME_FECHA}) VALUES ${titulo}, ${description}, ${fecha}")
db.close();
}
}
Si necesitan algún tipo de información extra no duden en pedirmela.