org.sqlite.SQLiteException:[SQLITE_ERROR] SQL 错误或缺少数据库(“as”附近:语法错误)

Posted

技术标签:

【中文标题】org.sqlite.SQLiteException:[SQLITE_ERROR] SQL 错误或缺少数据库(“as”附近:语法错误)【英文标题】:org.sqlite.SQLiteException: [SQLITE_ERROR] SQL error or missing database (near "as": syntax error) 【发布时间】:2021-07-31 00:56:01 【问题描述】:

我正在使用 Spring Boot 开发 JavaFX 应用程序,当我尝试从复杂的 SQLite 数据库表中列出记录时遇到标题异常。

之前我已经能够从另一个简单的表中进行选择,但在这种情况下,是一个具有多个外键的表:

这里是创建表脚本:

CREATE TABLE citas (
    id_cita                   INTEGER    PRIMARY KEY AUTOINCREMENT
                                         NOT NULL,
    dni_paciente              TEXT (20)  CONSTRAINT dni_paciente REFERENCES pacientes (dni_paciente) MATCH SIMPLE
                                         NOT NULL,
    dni_sanitario             TEXT (20)  NOT NULL
                                         CONSTRAINT dni_sanitario REFERENCES sanitarios (dni_sanitario),
    id_tratamiento            INTEGER    CONSTRAINT identificador_tratamiento REFERENCES tratamientos (id_tratamiento) 
                                         NOT NULL,
    fecha                     DATE       NOT NULL,
    hora_inicio               TIME       NOT NULL,
    hora_fin                  TIME       NOT NULL,
    observaciones             TEXT (100),
    paciente_dni              VARCHAR,
    sanitario_dni             VARCHAR,
    tratamiento_identificador INTEGER,
    dni                       VARCHAR
);

CREATE TABLE pacientes (
    dni_paciente TEXT (20)  PRIMARY KEY
                            NOT NULL,
    nombre       TEXT (50),
    apellidos    TEXT (50),
    direccion    TEXT (100),
    telefono     TEXT (20),
    adjunto      BLOB,
    dni          INTEGER
);

CREATE TABLE sanitarios (
    dni_sanitario TEXT (20) PRIMARY KEY,
    nombre        TEXT (50),
    apellidos     TEXT (50),
    especialidad  TEXT (50) 
);

CREATE TABLE tratamientos (
    id_tratamiento INTEGER    PRIMARY KEY ASC AUTOINCREMENT,
    nombre         TEXT (50),
    descripcion    TEXT (100),
    identificador  INTEGER
);

这是我的 Java 实体:

@Entity
@Table(name = "citas")
public class Citas 

    @Id
    @Column(name = "identificador", nullable = false, unique = true)
    private Integer identificador;

    @OneToOne
    @JoinColumn(name = "dni_paciente")
    private Pacientes paciente;

    @OneToOne
    @JoinColumn(name = "dni_sanitario")
    private Sanitarios sanitario;

    @OneToOne
    @JoinColumn(name = "id_tratamiento")
    private Tratamientos tratamiento;

    @Column(name = "fecha,", nullable = true, unique = false)
    private LocalDate fecha;

    @Column(name = "hora_inicio", nullable = true, unique = false)
    private LocalTime horaDesde;

    @Column(name = "hora_fin", nullable = true, unique = false)
    private LocalTime horaHasta;

    @Column(name = "observaciones,", length = 100, nullable = true, unique = false)
    private String observaciones;
// GETTERS AND SETTERS

@Entity
    @Table(name = "pacientes")
    public class Pacientes 

    @Id
    @Column(name = "dni_paciente", length = 20, nullable = false, unique = true)
    private String dniPaciente;

    @Column(name = "nombre", length = 50, nullable = true, unique = false)
    private String nombre;

    @Column(name = "apellidos", length = 50, nullable = true, unique = false)
    private String apellidos;

    @Column(name = "direccion", length = 100, nullable = true, unique = false)
    private String direccion;

    @Column(name = "telefono", length = 20, nullable = true, unique = false)
    private String telefono;

    @Lob
    private byte[] adjunto;

    @OneToOne
    @JoinColumn(name="dni")
    private Citas citas;
// GETTERS AND SETTERS

@Entity
@Table(name = "sanitarios")
public class Sanitarios 

    @Id
    @Column(name = "dni_sanitario", length = 20, nullable = false, unique = true)
    private String dniSanitario;

    @Column(name = "nombre", length = 50, nullable = true, unique = false)
    private String nombre;

    @Column(name = "apellidos", length = 50, nullable = true, unique = false)
    private String apellidos;

    @Column(name = "especialidad", length = 50, nullable = true, unique = false)
    private String especialidad;

    @OneToOne
    @JoinColumn(name="dni_sanitario")
    private Citas citas;
// GETTERS AND SETTERS

@Entity
@Table(name = "tratamientos")
public class Tratamientos 

    @Id
    @Column(name = "id_tratamiento", nullable = false, unique = true)
    private Integer idTratamiento;

    @Column(name = "nombre", length = 50, nullable = true, unique = false)
    private String nombre;

    @Column(name = "descripcion", length = 100, nullable = true, unique = false)
    private String descripcion;

    @OneToOne
    @JoinColumn(name="identificador")
    private Citas citas;
// GETTERS AND SETTERS

这是我的 Spring 数据存储库:

public interface CitasRepository extends PagingAndSortingRepository<Citas, Integer>, JpaRepository<Citas, Integer> 


服务实现:

@Service()
public class CitasServiceImpl implements CitasService 
    
    @Autowired
    private CitasRepository citasRepository;

    @Override
    public List<Citas> listarCitas() 
    return citasRepository.findAll();
    


我已在记录器中激活 SQL:

    select
        citas0_.identificador as identifi1_1_,
        citas0_.fecha,
        as fecha2_1_,
        citas0_.hora_inicio as hora_ini3_1_,
        citas0_.hora_fin as hora_fin4_1_,
        citas0_.observaciones,
        as observac5_1_,
        citas0_.dni_paciente as dni_paci6_1_,
        citas0_.dni_sanitario as dni_sani7_1_,
        citas0_.id_tratamiento as id_trata8_1_ 
    from
        citas citas0_
Hibernate: 
    select
        citas0_.identificador as identifi1_1_,
        citas0_.fecha,
        as fecha2_1_,
        citas0_.hora_inicio as hora_ini3_1_,
        citas0_.hora_fin as hora_fin4_1_,
        citas0_.observaciones,
        as observac5_1_,
        citas0_.dni_paciente as dni_paci6_1_,
        citas0_.dni_sanitario as dni_sani7_1_,
        citas0_.id_tratamiento as id_trata8_1_ 
    from
        citas citas0_

Hibernate 创建了错误的选择

完整的堆栈跟踪:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1787)
    at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1670)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Node.fireEvent(Node.java:8885)
    at javafx.scene.control.Button.fire(Button.java:203)
    at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:206)
    at com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
    at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.event.Event.fireEvent(Event.java:198)
    at javafx.scene.Scene$MouseHandler.process(Scene.java:3890)
    at javafx.scene.Scene.processMouseEvent(Scene.java:1885)
    at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2618)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:409)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:299)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:447)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:412)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:446)
    at com.sun.glass.ui.View.handleMouseEvent(View.java:556)
    at com.sun.glass.ui.View.notifyMouse(View.java:942)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
    at com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1784)
    ... 46 more
Caused by: org.springframework.orm.jpa.JpaSystemException: could not prepare statement; nested exception is org.hibernate.exception.GenericJDBCException: could not prepare statement
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:331)
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:233)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:551)
    at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)
    at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:152)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:174)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215)
    at com.sun.proxy.$Proxy78.findAll(Unknown Source)
    at es.clinica.podologia.servicios.impl.CitasServiceImpl.listarCitas(CitasServiceImpl.java:26)
    at es.clinica.podologia.controladores.AccesoController.autenticarUsuario(AccesoController.java:63)
    ... 58 more
Caused by: org.hibernate.exception.GenericJDBCException: could not prepare statement
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:47)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:113)
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:186)
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareQueryStatement(StatementPreparerImpl.java:151)
    at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:2103)
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:2040)
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:2018)
    at org.hibernate.loader.Loader.doQuery(Loader.java:948)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:349)
    at org.hibernate.loader.Loader.doList(Loader.java:2849)
    at org.hibernate.loader.Loader.doList(Loader.java:2831)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2663)
    at org.hibernate.loader.Loader.list(Loader.java:2658)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:506)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:400)
    at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:219)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1414)
    at org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1625)
    at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1593)
    at org.hibernate.query.Query.getResultList(Query.java:165)
    at org.hibernate.query.criteria.internal.compile.CriteriaQueryTypeQueryAdapter.getResultList(CriteriaQueryTypeQueryAdapter.java:76)
    at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:356)
    at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:78)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.springframework.data.repository.core.support.RepositoryMethodInvoker$RepositoryFragmentMethodInvoker.lambda$new$0(RepositoryMethodInvoker.java:289)
    at org.springframework.data.repository.core.support.RepositoryMethodInvoker.doInvoke(RepositoryMethodInvoker.java:137)
    at org.springframework.data.repository.core.support.RepositoryMethodInvoker.invoke(RepositoryMethodInvoker.java:121)
    at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:524)
    at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:285)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:531)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:156)
    at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:131)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:80)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:123)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:388)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:137)
    ... 67 more
Caused by: org.sqlite.SQLiteException: [SQLITE_ERROR] SQL error or missing database (near "as": syntax error)
    at org.sqlite.core.DB.newSQLException(DB.java:1012)
    at org.sqlite.core.DB.newSQLException(DB.java:1024)
    at org.sqlite.core.DB.throwex(DB.java:989)
    at org.sqlite.core.NativeDB.prepare_utf8(Native Method)
    at org.sqlite.core.NativeDB.prepare(NativeDB.java:130)
    at org.sqlite.core.DB.prepare(DB.java:257)
    at org.sqlite.core.CorePreparedStatement.<init>(CorePreparedStatement.java:45)
    at org.sqlite.jdbc3.JDBC3PreparedStatement.<init>(JDBC3PreparedStatement.java:30)
    at org.sqlite.jdbc4.JDBC4PreparedStatement.<init>(JDBC4PreparedStatement.java:19)
    at org.sqlite.jdbc4.JDBC4Connection.prepareStatement(JDBC4Connection.java:35)
    at org.sqlite.jdbc3.JDBC3Connection.prepareStatement(JDBC3Connection.java:241)
    at org.sqlite.jdbc3.JDBC3Connection.prepareStatement(JDBC3Connection.java:205)
    at com.zaxxer.hikari.pool.ProxyConnection.prepareStatement(ProxyConnection.java:337)
    at com.zaxxer.hikari.pool.HikariProxyConnection.prepareStatement(HikariProxyConnection.java)
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$5.doPrepare(StatementPreparerImpl.java:149)
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:176)
    ... 108 more

【问题讨论】:

【参考方案1】:

@Column(name = "observaciones,", length = 100, nullable = true, unique = false) 在名称末尾有一个逗号,这对 SQL 造成了严重破坏。去掉逗号! fecha, 也一样。

【讨论】:

以上是关于org.sqlite.SQLiteException:[SQLITE_ERROR] SQL 错误或缺少数据库(“as”附近:语法错误)的主要内容,如果未能解决你的问题,请参考以下文章