chennaione / sugar

Insanely easy way to work with Android Database.
http://satyan.github.com/sugar/
MIT License
2.62k stars 583 forks source link

Table names aren't quoted #416

Open RobinJ1995 opened 8 years ago

RobinJ1995 commented 8 years ago
11-03 14:50:20.087 2280-2280/be.robinj.restobill E/SQLiteLog: (1) near "TABLE": syntax error
11-03 14:50:20.088 2280-2280/be.robinj.restobill D/AndroidRuntime: Shutting down VM
11-03 14:50:20.090 2280-2280/be.robinj.restobill E/AndroidRuntime: FATAL EXCEPTION: main
11-03 14:50:20.090 2280-2280/be.robinj.restobill E/AndroidRuntime: Process: be.robinj.restobill, PID: 2280
11-03 14:50:20.090 2280-2280/be.robinj.restobill E/AndroidRuntime: android.database.sqlite.SQLiteException: near "TABLE": syntax error (code 1): , while compiling: CREATE TABLE TABLE ( ID INTEGER PRIMARY KEY AUTOINCREMENT , NAME TEXT )
11-03 14:50:20.090 2280-2280/be.robinj.restobill E/AndroidRuntime:     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
11-03 14:50:20.090 2280-2280/be.robinj.restobill E/AndroidRuntime:     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
11-03 14:50:20.090 2280-2280/be.robinj.restobill E/AndroidRuntime:     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
11-03 14:50:20.090 2280-2280/be.robinj.restobill E/AndroidRuntime:     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
11-03 14:50:20.090 2280-2280/be.robinj.restobill E/AndroidRuntime:     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
11-03 14:50:20.090 2280-2280/be.robinj.restobill E/AndroidRuntime:     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
11-03 14:50:20.090 2280-2280/be.robinj.restobill E/AndroidRuntime:     at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1674)
11-03 14:50:20.090 2280-2280/be.robinj.restobill E/AndroidRuntime:     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
11-03 14:50:20.090 2280-2280/be.robinj.restobill E/AndroidRuntime:     at com.orm.SugarDb.createTable(SugarDb.java:133)
11-03 14:50:20.090 2280-2280/be.robinj.restobill E/AndroidRuntime:     at com.orm.SugarDb.createDatabase(SugarDb.java:106)
11-03 14:50:20.090 2280-2280/be.robinj.restobill E/AndroidRuntime:     at com.orm.SugarDb.onCreate(SugarDb.java:100)
11-03 14:50:20.090 2280-2280/be.robinj.restobill E/AndroidRuntime:     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
11-03 14:50:20.090 2280-2280/be.robinj.restobill E/AndroidRuntime:     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
11-03 14:50:20.090 2280-2280/be.robinj.restobill E/AndroidRuntime:     at com.orm.Database.getDB(Database.java:18)
11-03 14:50:20.090 2280-2280/be.robinj.restobill E/AndroidRuntime:     at com.orm.SugarRecord.save(SugarRecord.java:45)
11-03 14:50:20.090 2280-2280/be.robinj.restobill E/AndroidRuntime:     at be.robinj.restobill.listener.TableAddPositiveOnClickListener.onClick(TableAddPositiveOnClickListener.java:59)
11-03 14:50:20.090 2280-2280/be.robinj.restobill E/AndroidRuntime:     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:160)
11-03 14:50:20.090 2280-2280/be.robinj.restobill E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
11-03 14:50:20.090 2280-2280/be.robinj.restobill E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135)
11-03 14:50:20.090 2280-2280/be.robinj.restobill E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5221)
11-03 14:50:20.090 2280-2280/be.robinj.restobill E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
11-03 14:50:20.090 2280-2280/be.robinj.restobill E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
11-03 14:50:20.090 2280-2280/be.robinj.restobill E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
11-03 14:50:20.090 2280-2280/be.robinj.restobill E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

As you can tell from the generated query (CREATE TABLE TABLE ( ID INTEGER PRIMARY KEY AUTOINCREMENT , NAME TEXT )) table names aren't quoted. This means that some table names (like table) will cause problems.

sibelius commented 8 years ago

http://stackoverflow.com/a/695626/2628278

RobinJ1995 commented 8 years ago

Regardless of whether some people would find this good practice or not, I would still consider this a bug.

RobinJ1995 commented 8 years ago

Neither are field names, for that matter.

11-03 17:23:54.155 15943-15943/be.robinj.restobill E/AndroidRuntime: Caused by: android.database.sqlite.SQLiteException: near "TABLE": syntax error (code 1): , while compiling: CREATE TABLE BILL_ENTITY ( ID INTEGER PRIMARY KEY AUTOINCREMENT , TABLE INTEGER, CLOSED INTEGER )

sibelius commented 8 years ago

@RobinJ1995 could you please to a PR to fix this? and add some tests for these cases?