1

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

...我不确定我做错了什么,有人可以检查并告诉我我的错误吗?

继承人的代码:

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 ****
4

5 回答 5

4

订单是sqllite 保留的 workd/keyword。我建议将表名从OrdertoOrders或类似的名称更改并尝试。

于 2013-08-30T04:40:14.950 回答
2

你这里有一个错字:

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

应该

+ "custid text references Category(id) not null,"
于 2013-08-30T04:41:30.690 回答
0

您正在使用保留关键字,而且您忘记在查询中提及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,"
        + ")";
于 2013-08-30T04:48:01.147 回答
0

我修复了我所有的 lil 错误,结果是这样的:

在我修复之前

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"
    + ")";
于 2013-08-30T04:51:40.787 回答
0

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

1.

而不是 'not null' 你使用了 'no null'

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

更正为

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

2.

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

+ "state text not null,"

去掉逗号

+ "state text not null"
于 2013-08-30T05:32:06.300 回答