[英]Sqlite-JDBC create table syntax error
我正在尝试使用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 ****
顺序是sqllite保留的workd / keyword 。 我建议将表名从Order
更改为Orders
或类似的方法,然后尝试。
您在这里有错别字:
+ "custid text references Category(id) no null,"
应该
+ "custid text references Category(id) not null,"
您使用的是保留关键字,而且您忘记在查询中提及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,"
+ ")";
我修复了我所有的小错误,结果是这样的:
在我修复之前
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"
+ ")";
在您的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"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.