【问题标题】:Sqlite-JDBC create table syntax errorSqlite-JDBC 创建表语法错误
【发布时间】:2013-08-30 04:38:08
【问题描述】:

我正在尝试使用 Java 在我的 sqlite 数据库中创建表。但在最后一个表 ORDER 上不断出现语法错误 >_

...我不确定我做错了什么,谁能检查并告诉我我的错误吗?

代码如下:

public boolean CreateTables() {
    Connection cn = getConnected();
    String createCategory = "Create table Category("
            + "id text primary key not null,"
            + "name text not null,"
            + "deliverday int not null,"
            + "isdelete bit not null"
            + ")";

    String createCustomer = "Create table Customer("
            + "id text primary key not null,"
            + "name text not null,"
            + "email text not null,"
            + "phone text not null,"
            + "address text not null,"
            + "isdelete boolean not null"
            + ")";

    String createOrder = "Create table Order("
            + "id text primary key not null,"
            + "custid text references Category(id) no null,"
            + "catid text references Customer(id) not null,"
            + "orderdate date not null,"
            + "delieverdate date not null,"
            + "description text not null,"
            + "requirement text not null,"
            + "price int not null,"
            + "image text not null,"
            + "product text not null,"
            + "state text not null,"
            + ")";
    try {
        PreparedStatement pst;
        try {
            System.out.println("*** Create table Category");

            pst = cn.prepareStatement(createCategory);
            pst.executeUpdate();
            pst.close();

            System.out.println("*** Category created Successfully");
        } catch (Exception e) {
            System.out.println("*** Failed to create Category");
            System.out.println(e.getMessage());
        }

        try {
            System.out.println("*** Create table Customer");

            pst = cn.prepareStatement(createCustomer);
            pst.executeUpdate();
            pst.close();

            System.out.println("*** Customer created Successfully");
        } catch (Exception e) {
            System.out.println("*** Failed to create Customer");
            System.out.println(e.getMessage());
        }


        try {
            System.out.println("*** Create table Order");

            pst = cn.prepareStatement(createOrder);
            pst.executeUpdate();
            pst.close();

            System.out.println("*** Order created successsfully");
        } catch (Exception e) {
            System.out.println("*** Failed to create Order");
            System.out.println(e.getMessage());
        }

        System.out.println("*** COMPLETE CREATING TABLE PROCESS ****");


        cn.close();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

这是错误。

*** Start to connection Sqlite
*** Connected to Sqlite successfully
*** Create table Category
*** Category created Successfully
*** Create table Customer
*** Customer created Successfully
*** Create table Order
*** Failed to create Order
[SQLITE_ERROR] SQL error or missing database (near "Order": syntax error)
*** COMPLETE CREATING TABLE PROCESS ****

【问题讨论】:

    标签: java sql sqlite jdbc


    【解决方案1】:

    订单是sqllite reserved workd/keyword。我建议将表名从 Order 更改为 Orders 或类似的名称并尝试。

    【讨论】:

    • @zBaoAnhLe:不客气。祝你好运!如果回答对你有帮助,别忘了接受。
    【解决方案2】:

    你这里有一个错字:

    + "custid text references Category(id) no null,"
    

    应该是

    + "custid text references Category(id) not null,"
    

    【讨论】:

      【解决方案3】:

      您正在使用保留关键字,而且您忘记在查询中提及 foreign 关键字。应该是这样的:

      String createOrder = "Create table Cus_Order("
              + "id text primary key not null,"
              + "custid text FOREIGN KEY references Category(id) not null,"
              + "catid text FOREIGN KEY references Customer(id) not null,"
              + "orderdate date not null,"
              + "delieverdate date not null,"
              + "description text not null,"
              + "requirement text not null,"
              + "price int not null,"
              + "image text not null,"
              + "product text not null,"
              + "state text not null,"
              + ")";
      

      【讨论】:

        【解决方案4】:

        我修正了我所有的小错误,结果是这样的:

        在我修复之前

        String createOrder = "Create table Order("
                + "id text primary key not null,"
                + "custid text references Category(id) no null,"
                + "catid text references Customer(id) not null,"
                + "orderdate date not null,"
                + "delieverdate date not null,"
                + "description text not null,"
                + "requirement text not null,"
                + "price int not null,"
                + "image text not null,"
                + "product text not null,"
                + "state text not null,"
                + ")";
        

        修复后:

        String createOrder = "Create table Orders("
            + "id text primary key not null,"
            + "custid text references Category(id),"
            + "catid text FOREIGN KEY references Customer(id),"
            + "orderdate date not null,"
            + "delieverdate date not null,"
            + "description text not null,"
            + "requirement text not null,"
            + "price int not null,"
            + "image text not null,"
            + "product text not null,"
            + "state text not null"
            + ")";
        

        【讨论】:

        • 你忘了修正第二列custid
        【解决方案5】:

        在您的 String createOrder 中有两个错误:

        1.

        你使用了“no null”而不是“not null”

        +"custid text references Category(id) no null,"
        

        改正为

        +"custid text references Category(id) not null,"
        

        2.

        最后一个字段后面不需要逗号,这里你在最后一个字段后面使用逗号。

        + "state text not null,"
        

        去掉逗号

        + "state text not null"
        

        【讨论】:

          猜你喜欢
          • 2014-03-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-01-21
          • 1970-01-01
          • 2021-11-30
          • 2021-10-15
          • 1970-01-01
          相关资源
          最近更新 更多