【问题标题】:SQLite insert statement: SQL error or missing database (near "(": syntax error)SQLite 插入语句:SQL 错误或缺少数据库(“(”附近:语法错误)
【发布时间】:2017-10-18 08:25:19
【问题描述】:

我看不到我搞砸了什么,还有其他人看到问题吗?

我似乎无法弄清楚这是怎么回事,我已经在所有可能的地方添加和删除了括号,并使用我添加的每个新括号运行

当前代码

    package application;

import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.PreparedStatement;

import java.sql.SQLException;
import java.util.ResourceBundle;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class RegistrationController implements Initializable {

    Connection connection;

    public RegistrationController() {
        connection = SQLConnection.Connector();
        if (connection == null)
            System.exit(1);
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }



    @FXML
    private TextField txtFName;

    @FXML
    private TextField txtSName;

    @FXML
    private TextField txtUName;

    @FXML
    private TextField txtPasswd;

    @FXML
    private TextField txtAge;

    @FXML
    private Label lblTest;

    public void Submit(ActionEvent event) {

        try {
            String query = ("INSERT INTO member(name,surname,age,username,password) VALUES(?,?,?,?,?)");
            PreparedStatement pst;
            pst = connection.prepareStatement(query);
            pst.setString(1, txtFName.getText());
            pst.setString(2, txtSName.getText());
            pst.setString(3, txtUName.getText());
            pst.setString(4, txtPasswd.getText());
            pst.setString(5, txtAge.getText());

            pst.executeUpdate();

            pst.close();

            lblTest.setText("Connected");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void Back(ActionEvent event) {
        try {
            ((Node) event.getSource()).getScene().getWindow().hide();
            Stage primaryStage = new Stage();
            FXMLLoader loader = new FXMLLoader();
            Pane root = loader.load(getClass().getResource("/application/Login.fxml").openStream());
            // ProfileController profileController =
            // (ProfileController)loader.getController();
            // profileController.GetUser(txtUsername.getText());
            primaryStage.close();
            Scene scene = new Scene(root);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


}

【问题讨论】:

  • 再次投票者可以提供反馈,这样我就可以解决我的问题,而不是投票和消失
  • 应该是insert into.. values .. 。 SQL 无效,因此出现错误
  • 试试...) VALUES (...而不是...) (...?
  • 我的坏人,添加了值,在发布后 30 秒注意到,没有错误,标签显示为“已连接”,就像代码中一样,但数据库中没有更新 @VeeraKannadiga
  • 也许你可以发一个minimal reproducible example。

标签: java eclipse sqlite


【解决方案1】:

你的行应该是:-

String query = ("INSERT INTO member(name,surname,age,username,password) VALUES (?,?,?,?,?)");

注意VALUES 关键字。

【讨论】:

  • 注意到帖子后30秒,将其编辑到代码中,没有错误但数据库没有更新:/
  • 我会尝试硬编码一些值,然后运行它。我看不出为什么不会将任何内容插入数据库的明显原因。它肯定是运行那段代码吗?
  • 真是个傻瓜,我的数据库没有更新,因为我放错了我的数据库文件,抱歉,你的回答帮助我注意到了,我会在可能的时候接受:)
猜你喜欢
  • 2021-04-28
  • 2018-04-10
  • 2020-12-11
  • 1970-01-01
  • 2017-02-11
  • 1970-01-01
  • 2021-07-31
  • 1970-01-01
  • 2018-01-19
相关资源
最近更新 更多