Embedded firebird not accepting create table statement

两盒软妹~` 提交于 2019-12-11 10:55:29

问题


The following SQL code works very well on MYSQL, and it contains valid SQL query language. However this doesn't work on embedded Firebird server.

The SQL code:

CREATE TABLE publications (
  id int(11) NOT NULL,
  filename varchar(500) NOT NULL,
  title varchar(500) DEFAULT NULL,
  authors varchar(1000) DEFAULT NULL,
  uploader int(7) DEFAULT NULL,
  keywords varchar(500) DEFAULT NULL,
  rawtext text,
  rawbinarydata blob NOT NULL,
  lastmodified timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

ALTER TABLE publications
  ADD PRIMARY KEY (id),
  ADD UNIQUE KEY filename (filename);

ALTER TABLE publications
  MODIFY id int(11) NOT NULL AUTO_INCREMENT;

The C# code that is using the query is:

try
{
    using( cmd.Connection = connect_to_fbserver() )
    {
        cmd.CommandText = fresh_db_creation_statement;
        cmd.Connection.Open();
        cmd.ExecuteNonQuery();
    }

    return true;
}
catch( Exception exx )
{
    lasterror = exx.Message;
    return false;
}

fresh_db_creation_statement is the sql code in the first code listing.

The error was caught at lasterror = exx.Message; with the value: "Dynamic SQL Error\nSQL error code = -104\nToken unknown - line 2, char 13", meaning the ( was flagged by the embedded firebird (that is line 2, char 13).

When I removed all sizes of the defined data value types (e.g. changed id int(11) NOT NULL to id int NOT NULL) it will flag the NOT.

How can I make Firebird accept this query and execute as normal?


回答1:


From what I can see, there are various problems

  1. Why you alter the table with statemens which you could already do on create:

    id int not null primary key,

    filename varchar(500) not null unique,

    lastmodified timestamp default CURRENT_TIMESTAMP

  2. Autoincrement does not exist, you need to build a trigger, see here: http://www.firebirdfaq.org/faq29/

  3. Update timestamp automatically on change does not exist, you need to build a trigger too, see here: http://www.firebirdfaq.org/faq77/




回答2:


Simply : Create/Alter does not work in batch. You have to use sql in a separate Command.

If you want to use the commands in a batch, you must use the EXECUTE BLOCK AS BEGIN ... but then create table doesnt work ... see. below

          string sqlText = "create table pub(id int not null);";//----  OK ----
        //string sqlText = "EXECUTE BLOCK AS BEGIN \ncreate table pub(id int not null);\nalter table pub add primary key (id);\nEND";//----  FAILED  ----
        //string sqlText = "EXECUTE BLOCK AS BEGIN \nupdate jizda set cislovozidla = 99999 where cislovozidla = 999899;\nalter table pub add primary key (id);\nEND";//----  FAILED ----
        //string sqlText = "EXECUTE BLOCK AS BEGIN \nupdate jizda set cislovozidla = 99999 where cislovozidla = 999899;\nupdate jizda set cislovozidla = 99999 where cislovozidla = 99989;\nEND";//----  OK ----
        using (FbConnection dbConnection = new FbConnection(Program.ConnectDBData()))
        {
          dbConnection.Open();
          FbCommand cmd = new FbCommand(sqlText);
          cmd.CommandType = CommandType.Text;
          cmd.Connection = dbConnection;
          cmd.ExecuteNonQuery();
        }


来源:https://stackoverflow.com/questions/33631757/embedded-firebird-not-accepting-create-table-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!