Java DB Error?

Discussion in 'Plugin Development' started by Gosintary, Mar 1, 2022.

Thread Status:
Not open for further replies.
  1. Offline

    Gosintary

    I'm working with databases for the first time in a long time and I'm having an issue.

    I'm getting this error: [SQLITE_ERROR] SQL error or missing database (near ")": syntax error)

    whenever I call this method:

    Code:
    public void createNewAlbum(String albumName) {
    
            String url = "jdbc:sqlite:C://sqlite/db/songs.db";
    
            String sql = "CREATE TABLE IF NOT EXISTS [" +albumName.toLowerCase()+ "] (\n" + "    id integer PRIMARY KEY,\n"
                    + " type text NOT NULL,\n" + "    duration text NOT NULL,\n" + "    songName text NOT NULL,\n"
                    + " artist text NOT NULL,\n" + " releaseDate text NOT NULL,\n" + " crs text NOT NULL,\n"
                    + " prors text NOT NULL,\n" + ");";
    
            try (Connection conn = DriverManager.getConnection(url); Statement stmt = conn.createStatement()) {
    
                stmt.execute(sql);
            } catch (SQLException e) {
                System.out.println(e.getMessage());
            }
        }
    spent about an hour on google couldn't figure it out. Any suggestions?
     
  2. Offline

    timtower Administrator Administrator Moderator

  3. Offline

    Gosintary

    Sure!

    Here is the method that is throwing the error

    Code:
    public void createNewAlbum(String albumName) {
    
        String url = "jdbc:sqlite:C://sqlite/db/songs.db";
    
        String sql = "CREATE TABLE IF NOT EXISTS [" +albumName.toLowerCase()+ "] (\n" + "    id integer PRIMARY KEY,\n"
                    + " type text NOT NULL,\n" + "    duration text NOT NULL,\n" + "    songName text NOT NULL,\n"
                    + " artist text NOT NULL,\n" + " releaseDate text NOT NULL,\n" + " crs text NOT NULL,\n"
                    + " prors text NOT NULL,\n" + ");";
    
            try (Connection conn = DriverManager.getConnection(url); Statement stmt = conn.createStatement()) {
    
                stmt.execute(sql);
            } catch (SQLException e) {
                System.out.println(e.getMessage());
            }
        }
    Database Creation

    Code:
    public void createNewDatabase(String fileName) {
    
        String url = "jdbc:sqlite:C:/sqlite/db/" + fileName;
    
        try (Connection conn = DriverManager.getConnection(url)) {
                if (conn != null) {
                    DatabaseMetaData meta = conn.getMetaData();
                    System.out.println("The driver name is " + meta.getDriverName());
                    System.out.println("A new database has been created.");
                }
    
            } catch (SQLException e) {
                System.out.println(e.getMessage());
            }
        }
    
    Connecting to Database
    
        private static Connection connect() {
            String url = "jdbc:sqlite:C:/sqlite/db/songs.db";
            Connection conn = null;
            try {
                conn = DriverManager.getConnection(url);
            } catch (SQLException e) {
                System.out.println(e.getMessage());
            }
            return conn;
        }
    
     
  4. Offline

    timtower Administrator Administrator Moderator

    @Gosintary Can you add a printline with the actual query that is about to get executed?
     
  5. Offline

    Gosintary

    I fixed it.
    Code:
    String sql = "CREATE TABLE IF NOT EXISTS [" +albumName.toLowerCase()+ "] (\n" + "    id integer PRIMARY KEY,\n"
                    + " type text NOT NULL,\n" + "    duration text NOT NULL,\n" + "    songName text NOT NULL,\n"
                    + " artist text NOT NULL,\n" + " releaseDate text NOT NULL,\n" + " crs text NOT NULL,\n"
                    + " prors text NOT NULL,\n" + ");";
    There is a comma after "prors text NOT NULL" that shouldn't have been there because its the end of the statement.
     
Thread Status:
Not open for further replies.

Share This Page