Sqlite-JDBC创建表语法错误

时间:2013-08-30 04:38:08

标签: java sql sqlite jdbc

我正在尝试使用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 ****

5 个答案:

答案 0 :(得分:4)

订单为sqllite reserved workd/keyword。我建议将表格名称从Order更改为Orders或类似的内容,然后尝试。

答案 1 :(得分:2)

你这里有一个错字:

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

应该是

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

答案 2 :(得分: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,"
        + ")";

答案 3 :(得分:0)

我修正了所有的错误,结果如下:

在我修复之前

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"
    + ")";

答案 4 :(得分: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"