1

I have the following execSQL that is throwing a syntax error and causing my app to crash. I can't figure out why, considering I've escaped single and double quotes.

Here is the code:

database.execSQL("insert into " + TABLE_EXERCISES + " (" + COLUMN_NAME + ", " + COLUMN_PRIMARY_MUSCLE + ", " + COLUMN_SECONDARY_MUSCLE + ", " + COLUMN_EQUIPMENT_TYPE + ", " + COLUMN_STEPS + ", " + COLUMN_IMAGES + ") values ('Cable Shrugs', 'Traps', 'None', 'Cable', 'Attach a flat shoulder width bar to the lowest cable pulley. Stand with your feet shoulder width apart. Keep your abs tight and a slight bend in your knees to protect your lower back. Starting with the bar at waist height, raise your shoulders toward your ears as if you\'re saying,\"I don\'t know.\" Hold briefly at the top of the contraction then lower in a slow and controlled manner.', 'cable-shrugs-1.png|cable-shrugs-2.png')");

Anyone have any idea?

Here is the log:

12-29 23:31:54.174: E/AndroidRuntime(1207): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gauvion.gfit/com.gauvion.gfit.RoutinesActivity}: android.database.sqlite.SQLiteException: near "re": syntax error (code 1): , while compiling: insert into exercises(name, primary_muscle, secondary_muscle, equipment_type, steps, images) VALUES ('Cable Shrugs', 'Traps', 'None', 'Cable', 'Attach a flat shoulder width bar to the lowest cable pulley. Stand with your feet shoulder width apart. Keep your abs tight and a slight bend in your knees to protect your lower back. Starting with the bar at waist height, raise your shoulders toward your ears as if you're saying,"I don't know." Hold briefly at the top of the contraction then lower in a slow and controlled manner.', 'cable-shrugs-1.png|cable-shrugs-2.png')
4
  • 1
    Could you post the part of the logcat where the crash happens please. Commented Dec 29, 2012 at 23:45
  • 2
    Also note that escaping single quotes in SQLite is by double-single-quotes (''), not backslashes: sqlite.org/faq.html#q14 You really should consider using positional parameters rather than hand-coding all the escaping. Commented Dec 29, 2012 at 23:46
  • posted the log. maybe youre right, im probably not escaping quotes and double quotes properly. Commented Dec 29, 2012 at 23:48
  • From that error, it definitely looks like SQLite is choking on your first single quote. Commented Dec 29, 2012 at 23:50

1 Answer 1

1

you're

There's your error. You're not escaping the apostrophe. To my knowledge if you use '' this shall work.

Note: for ' use '' and for " use ""

Try this:

database.execSQL("insert into " + TABLE_EXERCISES + " (" + COLUMN_NAME + ", " + COLUMN_PRIMARY_MUSCLE + ", " + COLUMN_SECONDARY_MUSCLE + ", " + COLUMN_EQUIPMENT_TYPE + ", " + COLUMN_STEPS + ", " + COLUMN_IMAGES + ") values ('Cable Shrugs', 'Traps', 'None', 'Cable', 'Attach a flat shoulder width bar to the lowest cable pulley. Stand with your feet shoulder width apart. Keep your abs tight and a slight bend in your knees to protect your lower back. Starting with the bar at waist height, raise your shoulders toward your ears as if you''re saying,""I don''t know."" Hold briefly at the top of the contraction then lower in a slow and controlled manner.', 'cable-shrugs-1.png|cable-shrugs-2.png')");

Sign up to request clarification or add additional context in comments.

Comments

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.