嵌入式firebird不接受create table语句

本文关键字:table 语句 create 不接受 firebird 嵌入式 | 更新日期: 2023-09-27 18:21:25

下面的SQL代码在MYSQL上运行得很好,并且它包含有效的SQL查询语言。然而,这在嵌入式Firebird服务器上不起作用。

SQL代码:

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;

使用查询的C#代码是:

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是第一个代码列表中的sql代码

该错误是在lasterror = exx.Message;捕获的,值为:"Dynamic SQL Error'nSQL error code = -104'nToken unknown - line 2, char 13",这意味着嵌入的firebird(即line 2, char 13)标记了(

当我删除了定义的数据值类型的所有大小(例如,将id int(11) NOT NULL更改为id int NOT NULL)时,它将标记NOT

如何让Firebird接受此查询并正常执行?

嵌入式firebird不接受create table语句

据我所见,存在各种问题

  1. 为什么你用Statemen来改变表格,而你已经可以在创建时这样做了:

    id int not null primary key,

    filename varchar(500) not null unique,

    lastmodified timestamp default CURRENT_TIMESTAMP

  2. Autoincrement不存在,您需要构建一个触发器,请参阅此处:http://www.firebirdfaq.org/faq29/

  3. 更改时自动更新时间戳不存在,您也需要构建触发器,请参阅此处:http://www.firebirdfaq.org/faq77/

简单地说:创建/更改不在批处理中工作。您必须在单独的命令中使用sql

如果要在批处理中使用命令,则必须使用EXECUTE BLOCK AS BEGIN。。。但是创建表不起作用。。。看见低于

          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();
        }