0

I'm having trouble figuring out what this error means: SQLiteLog: (1) near "s": syntax error Mostly because I can't find the database it's running off.

I'm building a movie database android app and the database file I have in my raw directory remains the same and I can't find the DB the build is running off of to fix this.

When using the OMDB api so movies will add to my local database and some will not.

The command I'm using to add is:

MovieDB db = new MovieDB((Context) this);
SQLiteDatabase movDB = db.openDB();
String insert = "insert into movies values('" + movie.title + "','" + movie.year + "','" + movie.rated + "','" + movie.released + "','" + movie.runtime + "','" + movie.genre + "','" + movie.actors + "','" + movie.plot + "',null);";
movDB.execSQL(insert);

I have a feeling it has to do will the null at the end because in my database I have a self incrementing ID which I figured if I passed null it would just assign a value.

Any insight would be much appreciated!

4
  • Watch here ,null);";. This may be because of semicolon and null. Commented Mar 30, 2016 at 3:58
  • Remove ,null. The id (assuming integer primary key) will set to a unique value. Commented Mar 30, 2016 at 3:58
  • Could it be because of a value with a single quote which is unescaped causing issues with one of your strings? Commented Mar 30, 2016 at 3:59
  • I think you should replace null by '' Commented Mar 30, 2016 at 4:05

1 Answer 1

1

Recommended way to insert a row in SQLITE is to use contentvalues which is better than executing query. Example below,

public void addMovie(String title, int year){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("title", title);
        values.put("year", year);
        db.insert(TABLE_MOVIES, null, values);
        db.close();
    }
Sign up to request clarification or add additional context in comments.

3 Comments

That fixed it, thanks! For own knowledge do you or anyone know why the way I had it worked for some movies and not others but your way worked for all?
The second argument in db.insert() method does that magic. Better explained in the docs here. Check the insert() doc developer.android.com/intl/es/reference/android/database/sqlite/…
Aw makes sense pretty much some values were null, and the middle null value (called the NullColumnHack) will allow for attributes not given to be null. Thus why some inserts worked and some didn't because the ones that were denied must of had null values/unaddressed values. Thanks again @Thahaseen !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.