Skip to content
Advertisement

SQL Insert query not storing upon button press

I am creating a project in which a database can be read and modified through an app. The tutorial for the insert section has lost me though. Using lowercase to begin a word causes a syntax error, fine. But, when capitalising the first letter it is accepted. The page then closes and takes you back to the main menu without indicating any errors. However, unlike my tutors tutorial, my added record does not show in the list view. So if anyone could help me understand where I’ve gone wrong it would be greatly appreciated!

The Insert Activity:

package com.example.filmapp;

import androidx.appcompat.app.AppCompatActivity;

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class InsertRecordActivity extends AppCompatActivity
{

    OpenDatabase sqh;
    SQLiteDatabase sqdb;

    EditText filmTitleEditTextInsert;
    EditText genreEditTextInsert;
    EditText roleEditTextInsert;
    EditText yearEditTextInsert;
    EditText ratingEditTextInsert;

    Button insertButtonInsert;
    Button cancelButtonInsert;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_insert_record);

        InitDatabase();

        setupControls();

    } // protected void onCreate(Bundle savedInstanceState)

    public void InitDatabase()
    {
        //Initialize SQLite Helper Class
        sqh = new OpenDatabase(this);

        //Retrieve read/writeable database
        sqdb = sqh.getWritableDatabase();

    } //  public void InitDatabase()

    protected void setupControls()
    {
       filmTitleEditTextInsert = findViewById(R.id.fimTitleEditTextInsert);
        genreEditTextInsert = findViewById(R.id.genreEditTextInsert);
        roleEditTextInsert = findViewById(R.id.roleEditTextInsert);
        yearEditTextInsert = findViewById(R.id.yearEditTextInsert);
        ratingEditTextInsert = findViewById(R.id.ratingEditTextInsert);

        insertButtonInsert = findViewById(R.id.insertButtonInsert);
        insertButtonInsert.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                sqh.insertRecordIntoFilmography( sqdb, filmTitleEditTextInsert.getText().toString(),
                        genreEditTextInsert.getText().toString(),
                        roleEditTextInsert.getText().toString(),
                        yearEditTextInsert.getText().toString(),
                        ratingEditTextInsert.getText().toString());
                finish();;

            }
        });

        cancelButtonInsert = findViewById(R.id.cancelButtonInsert);
        cancelButtonInsert.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                finish();

            }
        });

    } // protected void setupControls()

} // public class InsertRecordActivity extends AppCompatActivity 

Insert Function within OpenDatabase class.

public void insertRecordIntoFilmography(SQLiteDatabase sqdb, String filmTitle, String genre, String role,
                                            String year, String imdbRating)
    {
        String insertQuery = "INSERT INTO FilmTable(filmTitle, genre, role, year, imdbRating) ";
        insertQuery = insertQuery + "VALUES ('" + filmTitle + "',";
        insertQuery = insertQuery + " '" + genre + " '";
        insertQuery = insertQuery + " '" + role + " '";
        insertQuery = insertQuery + " '" + year + " '";
        insertQuery = insertQuery + " '" + imdbRating + " ');";

        sqdb.execSQL(insertQuery);

    } //  public void insertRecordIntoFilmography(SQLiteDatabase sqdb, String filmTitle, String genre, String role, String year, String imdbRating)

Advertisement

Answer

Missing , after every value. (‘Film’, ‘Genre ‘, ‘Role ‘, ‘Year ‘, ‘Ratiung ‘) More info

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement