1

I am learning android programming and sqlite at the same time. I am creating a table like:

    private static final String DATABASE_CREATE = "CREATE TABLE " + BOOK_TABLE + " ("
        + BookContract._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
        + BookContract.TITLE + " TEXT NOT NULL, "
        + BookContract.AUTHORS + " TEXT NOT NULL, "
        + BookContract.ISBN + " TEXT NOT NULL, "
        + BookContract.PRICE + " TEXT NOT NULL );";

In my function that fetches all books in the database, my query is:

String query =  "SELECT " +
            BOOK_TABLE + "." + BookContract._ID + ", " +
            BookContract.TITLE + ", " +
            BookContract.PRICE + ", " +
            BookContract.ISBN + ", " +
            "GROUP_CONCAT(" + AuthorContract.NAME + ", ',') as " + BookContract.AUTHORS +
            " FROM " + BOOK_TABLE + " LEFT OUTER JOIN " + AUTHOR_TABLE + " ON " +
            BOOK_TABLE + "." + BookContract._ID + "=" +
            AUTHOR_TABLE + "." + AuthorContract.BOOK_FOREIGN_KEY +
            " GROUP BY " + BOOK_TABLE + "." + BookContract._ID + ", " +
            BookContract.TITLE + ", " + BookContract.PRICE + ", " + BookContract.ISBN;

Here is how the string actually looks like for the table creation:

CREATE TABLE bookTable (_id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL, authors TEXT NOT NULL, isbn TEXT NOT NULL, price TEXT NOT NULL );

and for the query:

 SELECT bookTable._id, title, price, isbn, GROUP_CONCAT(name, ',') as authors FROM bookTable LEFT OUTER JOIN authorTable ON bookTable._id = authorTable.book_fk GROUP BY bookTable._id, title, price, isbn

I swear I am going insane on this because similar stackOverflow posts asking the similar problem have minor spelling mistakes or missing spaces, however mine seems to work correctly on an online sql simulator!

When i do a database.rawQuery(query, null), the error saying that there is no such column: price appears in the log.

EDIT:

There is another Author table that has the missing name column that was mentioned in the comments, here it is for clarity:

 String AUTHOR_TABLE_CREATE = "CREATE TABLE " + AUTHOR_TABLE + " ("
        + AuthorContract._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
        + AuthorContract.BOOK_FOREIGN_KEY + " INTEGER NOT NULL, "
        + AuthorContract.NAME  + " TEXT NOT NULL, "
        + "FOREIGN KEY ("+ AuthorContract.BOOK_FOREIGN_KEY+") " +
        "REFERENCES "+BOOK_TABLE+"("+BookContract._ID+") ON DELETE CASCADE);";

which gives the string:

CREATE TABLE authorTable (_id INTEGER PRIMARY KEY AUTOINCREMENT, book_fk INTEGER NOT NULL, name TEXT NOT NULL, FOREIGN KEY (book_fk) REFERENCES bookTable(_id) ON DELETE CASCADE);
user3261941
  • 103
  • 9

1 Answers1

2

Just uninstall your app from mobile and then re-run. i hope it works if there is no problem in code.

Ravindra Kushwaha
  • 7,846
  • 14
  • 53
  • 103