Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JDBC SQLite not enforcing unique primary key constraint

Can anyone suggest to me why my SQLite JDBC database is not enforcing the unique primary key constraint?

The create table method is:

public static void TableCars()
      {
        Connection c = null;
        Statement stmt = null;
        try {
          Class.forName("org.sqlite.JDBC");
          c = DriverManager.getConnection("jdbc:sqlite:WalkerTechCars.db");
          System.out.println("Opened database successfully");

          stmt = c.createStatement();
          String sql = "CREATE TABLE IF NOT EXISTS CARS3 " +
                       "(REGISTRATION TEXT PRIMRY KEY   NOT NULL, " + 
                       " PHONE               TEXT       NOT NULL," +
                       " MAKE                TEXT       NOT NULL, " + 
                       " MODEL               TEXT, " + 
                       " COLOUR              TEXT)";
          stmt.executeUpdate(sql);
          stmt.close();
          c.close();
        } catch ( Exception e ) {
          System.err.println( e.getClass().getName() + ": " + e.getMessage() );
          System.exit(0);
        }
        System.out.println("Cars3 created successfully");
      }

The insert method is:

 public static void InsertCars(String table, String phone, String registration, String make, String model, String colour)
  {
    Connection c = null;
    PreparedStatement pstmt = null;
    try {
      Class.forName("org.sqlite.JDBC");
      c = DriverManager.getConnection("jdbc:sqlite:WalkerTechCars.db");
      c.setAutoCommit(false);
      System.out.println("Opened database successfully");

      String query="INSERT INTO "+table+" (PHONE,REGISTRATION,MAKE, MODEL,COLOUR) VALUES (?,?,?,?,?)";

      pstmt = c.prepareStatement(query);

      pstmt = c.prepareStatement
              ("insert into CARS3 values(?,?,?,?,?)");
        pstmt.setString(1,registration);
        pstmt.setString(2,phone);
        pstmt.setString(3, make);
        pstmt.setString(4, model);
        pstmt.setString(5, colour);         
        pstmt.executeUpdate();

        pstmt.close();
      c.commit();
      c.close();
    } catch ( Exception e ) {
      System.err.println( e.getClass().getName() + ": " + e.getMessage() );
      System.exit(0);
    }
    System.out.println("Car Records created successfully");
  }

and the line of code that should not be allowed, but can be executed repeatedly is:

DbHandler.InsertCars("Cars3", "1","BN51 MZY", "Mini", "C220", "BLACK");

1 Answers

Your creation SQL states PRIMRY KEY instead PRIMARY KEY. Your table was thus, initially created without the PRIMARY KEY constraint and since you added the 'IF NOT EXISTS' condition, new creations won't wake effect until the table is previously dropped.

The reason your table was created without PRIMARY KEY constraint can be found within the column-constraint diagram:

enter image description here

The constraint deffinition is wrong (PRMARY is not PRIMARY), (as remarked in red) that makes the column constraint sub-grammar not to triggered.

like image 185
Pablo Francisco Pérez Hidalgo Avatar answered Mar 19 '26 00:03

Pablo Francisco Pérez Hidalgo