简体   繁体   English

“ INSERT”附近的SQLiteLog错误(1):语法错误

[英]SQLiteLog Error (1) near “INSERT”: syntax error

I'm trying to read and execute an SQL script from a file. 我正在尝试从文件读取和执行SQL脚本。 CREATE commands work but my first INSERT command results in a Logcat error with tag "SQLiteLog" and text "(1) near "INSERT": syntax error". CREATE命令有效,但是我的第一个INSERT命令导致Logcat错误,其标签为“ SQLiteLog”,文本为“(1)在“ INSERT”附近:语法错误”。 The error is thrown on the database.execSQL() line. 该错误引发在database.execSQL()行上。

String[] createScript = outputStream.toString().split(";");
            for (int i = 0; i < createScript.length; i++) {
                String sqlStatement = createScript[i].trim();
                // TODO You may want to parse out comments here
                if (sqlStatement.length() > 0) {
                    database.execSQL(sqlStatement + ";");
                }
            }

The SQL line is as follows SQL行如下

INSERT INTO [EntityAttributeSet] ([Id],[Code]) VALUES (1,NULL);

The CREATE statement is as follows CREATE语句如下

CREATE TABLE [EntityAttributeSet] ([Id] int IDENTITY (636,1) NOT NULL, [Code]nvarchar(50) NULL);

Hex dump of a few INSERT statements: 一些INSERT语句的十六进制转储:

0000-0c00:  20 4e 4f 54-20 4e 55 4c-4c 0d 0a 29-3b 0d 0a 49  .NOT.NUL L..);..I
0000-0c10:  4e 53 45 52-54 20 49 4e-54 4f 20 5b-45 6e 74 69  NSERT.IN TO.[Enti
0000-0c20:  74 79 41 74-74 72 69 62-75 74 65 53-65 74 5d 20  tyAttrib uteSet].
0000-0c30:  28 5b 49 64-5d 2c 5b 43-6f 64 65 5d-29 20 56 41  ([Id],[C ode]).VA
0000-0c40:  4c 55 45 53-20 28 31 2c-4e 55 4c 4c-29 3b 0d 0a  LUES.(1, NULL);..
0000-0c50:  0d 0a 49 4e-53 45 52 54-20 49 4e 54-4f 20 5b 45  ..INSERT .INTO.[E
0000-0c60:  6e 74 69 74-79 41 74 74-72 69 62 75-74 65 53 65  ntityAtt ributeSe
0000-0c70:  74 5d 20 28-5b 49 64 5d-2c 5b 43 6f-64 65 5d 29  t].([Id] ,[Code])
0000-0c80:  20 56 41 4c-55 45 53 20-28 32 2c 4e-55 4c 4c 29  .VALUES. (2,NULL)
0000-0c90:  3b 0d 0a 0d-0a 49 4e 53-45 52 54 20-49 4e 54 4f  ;....INS ERT.INTO

I resolved the problem myself -- there was a command from a different flavor of SQL later in the script. 我自己解决了这个问题-稍后在脚本中有一条来自另一种SQL的命令。 Removing it fixed the error. 删除它可以修复错误。

if your first parameter is really an Integer do this: 如果您的第一个参数确实是整数,请执行以下操作:

  INSERT INTO EntityAttributeSet (Id,Code) VALUES (1,NULL)

If it's a varchar (string), do this: 如果是varchar (字符串),请执行以下操作:

  INSERT INTO EntityAttributeSet (Id,Code) VALUES ("1",NULL)

more info on: http://www.w3schools.com/sql/sql_insert.asp 有关更多信息: http : //www.w3schools.com/sql/sql_insert.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM