问题描述:

I am trying to create a database with the name of a string stored in a local variable with the following syntax:

mDb.execSQL("CREATE TABLE " + FLASH_TABLE + " (" + KEY_CARD_ROWID

+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_QUESTION

+ "TEXT NOT NULL," + KEY_ANSWER + "TEXT NOT NULL);");

which produces the following error in log cat:

05-14 04:40:05.892: ERROR/Database(372): Failure 1 (near "15": syntax error) on 0x272140 when preparing 'CREATE TABLE 15 (_id INTEGER PRIMARY KEY AUTOINCREMENT, questionTEXT NOT NULL,answerTEXT NOT NULL);'.

with FLASH_TABLE being the local variable with the value "15"

KEY_QUESTION and KEY_ANSWER are also local variables

I have looked at every example online that I could find and I can not find the reason for the syntax error. I appreciate any advice you can offer.

网友答案:

You cannot create a table name (or a field name, for that matter) with a string that begins with a number unless you double-quote it. But even though it's technically possible, it's not considered a best practice. If you do, you will also have to double-quote the table name every time you use it in a query so you will quickly find that it's more trouble than it's worth.

相关阅读:
Top