dbf表上的插入中的语法错误

时间:2013-07-18 20:58:38

标签: java jdbc foxpro dbf visual-foxpro

我正在尝试使用jdbc在dbf表上进行插入,但每次执行系统时都会给出错误:

Exception in thread "main" java.sql.SQLException: [Microsoft][ODBC Visual FoxPro Driver]Syntax error.
at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcStatement.execute(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcStatement.executeQuery(Unknown Source)
at addProduct.test.main(test.java:29)

这是代码:

public static void main(String[] args) throws ClassNotFoundException, SQLException {
    // TODO Auto-generated method stub
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    String connString="jdbc:odbc:Driver={Microsoft Visual FoxPro Driver};SourceDB=C:\\RestBar\\DBC;SourceType=DBF";//DeafultDir indicates the location of the db
    Connection connection=DriverManager.getConnection(connString);
    Statement query = connection.createStatement();

    ResultSet result;
    String sql="INSERT INTO CUENTAS (NUM_CTA, NUB_CTA, CAJ_CTA, FEC_CTA, HOR_CTA, " +
            "OPE_CTA, MES_CTA, NOM_CTA, UBI_CTA, PER_CTA, ACU_CTA, USU_CTA, KDI_CTA) " +
            "VALUES('0000001', SELECT DES_MEZ FROM MESAS WHERE COD_MEZ = 'T01', '01', " +
            "'2013/07/18', '16:50:33', 0,'002', SELECT DES_MES FROM MESEROS WHERE COD_MES = '002'," +
            " 'T01', '2',0.00, 01, 0.00)";
    query.execute(sql);


}

2 个答案:

答案 0 :(得分:1)

如果要使用子查询,则需要将它们用作SELECT的一部分,而不是VALUES中列出的标量值的一部分。并且FoxPro不允许SELECT没有FROM

您的简化查询如下(请注意,当您将字符串发送到VFP时,您需要删除换行符或插入分号以包装字符串):

INSERT INTO CUENTAS (
    NUM_CTA, 
    NUB_CTA, 
    CAJ_CTA, FEC_CTA, HOR_CTA, OPE_CTA, MES_CTA, 
    NOM_CTA, 
    UBI_CTA, PER_CTA, ACU_CTA, USU_CTA, KDI_CTA
) 
SELECT
    '0000001', 
    DES_MEZ , 
    '01', '2013/07/18', '16:50:33', 0,'002', 
    (SELECT DES_MES FROM MESEROS WHERE COD_MES = '002'), 
    'T01', '2',0.00, 01, 0.00
FROM MESAS WHERE COD_MEZ = 'T01'

请注意,在编写INSERT...SELECT语句时,向SELECT添加正确的列名是有效且良好的做法。

答案 1 :(得分:0)

至少,在使用子查询时,您应该将它们嵌入另一组括号中,如下所示:

VALUES('0000001', (SELECT DES_MEZ FROM MESAS WHERE COD_MEZ = 'T01'), '01', " +

我不确切知道VFP是否支持此语法,但您必须验证更改后语法错误是否仍然存在。如果没有,您可以通过事先发出第二个查询来获取子查询结果来规避限制,然后将该数据作为附加参数用于原始查询。