Estoy desarrollando una aplicación que almacena eventos de usuarios en la base de datos, y para ese propósito estoy usando SQLite para propósitos de almacenamiento, pero cuando estoy ejecutando una consulta arrojará un error. A continuación se muestra mi código que activó la acción cuando se hace clic en el botón Agregar: -

add.addActionListener(new ActionListener() {
   @Override
   public void actionPerformed(ActionEvent evt) {
     Log.p(dateLabel.getText().toString());
     //if((dateLabel.getText() == null) && (eventArea.getText() == null) && (descriptionArea.getText() == null)){
     if ((dateLabel.getText() == "") || (eventArea.getText() == "") || (descriptionArea.getText() == "")) {
      Dialog.show("Required field", "Please fill all the fields", "OK", "");
     }
     //}
     else {

      Label date1 = new Label("Event Date:- ");
      Label dateLabel1 = new Label(dateLabel.getText());
      Label event1 = new Label("Event:- ");
      Label eventArea1 = new Label(eventArea.getText());
      Label description1 = new Label("Event Description:- ");
      Label descriptionArea1 = new Label(descriptionArea.getText());

      Container container1 = TableLayout.encloseIn(2, date1, dateLabel1, event1, eventArea1, description1, descriptionArea1);
      container1.setUIID("container");

      saveEvent.add(container1);

      ToastBar.showMessage("Event successfull saved", FontImage.MATERIAL_SAVE, 4000);

      //Databse Working


      try {
       db = Display.getInstance().openOrCreate("Events.db");
       cur = db.executeQuery("CREATE TABLE 'calendar' ('Date' DATE NOT NULL,'Event' VARCHAR(250) NOT NULL,'Description' VARCHAR(500) NOT NULL);");

       String[] values = new String[] {
        dateLabel.getText(), eventArea.getText(), descriptionArea.getText()
       };

       db.execute("INSERT INTO data('Date','Event','Description') VALUES(?,?,?)", values);

       Button database = new Button("Database");
       calendar.add(database);
       database.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent evt) {
         try {
          cur = db.executeQuery("SELECT * FROM data");
          int columns = cur.getColumnCount();
          DataEvent.removeAll();

          if (columns > 0) {
           boolean next = cur.next();
           if (next) {
            ArrayList < String[] > data = new ArrayList < > ();
            String[] columnNames = new String[columns];
            for (int iter = 0; iter < columns; iter++) {
             columnNames[iter] = cur.getColumnName(iter);
            }
            while (next) {
             Row currentRow = cur.getRow();
             String[] currentRowArray = new String[columns];
             for (int iter = 0; iter < columns; iter++) {
              currentRowArray[iter] = currentRow.getString(iter);
             }
             data.add(currentRowArray);
             next = cur.next();
            }
            Object[][] arr = new Object[data.size()][];
            data.toArray(arr);
            DataEvent.add(BorderLayout.CENTER, new Table(new DefaultTableModel(columnNames, arr)));
           } else {
            DataEvent.add(BorderLayout.CENTER, "Query returned no results");
           }
          } else {
           DataEvent.add(BorderLayout.CENTER, "Query returned no results");
          }
          DataEvent.revalidate();
          Util.cleanup(db);
          Util.cleanup(cur);
         } catch (IOException e) {
          e.printStackTrace();
         }
        }
       });

Y el error arrojado por la aplicación se muestra a continuación: -

java.sql.SQLException: Query does not return results
    at org.sqlite.PrepStmt.executeQuery(PrepStmt.java:104)
    at com.codename1.impl.javase.SEDatabase.executeQuery(SEDatabase.java:172)
    at com.pivotal.calendar.Calender$4.actionPerformed(Calender.java:211)
    at com.codename1.ui.util.EventDispatcher.fireActionEvent(EventDispatcher.java:349)
    at com.codename1.ui.Button.fireActionEvent(Button.java:435)
    at com.codename1.ui.Button.released(Button.java:467)
    at com.codename1.ui.Button.pointerReleased(Button.java:566)
    at com.codename1.ui.Form.pointerReleased(Form.java:2642)
    at com.codename1.ui.Form.pointerReleased(Form.java:2578)
    at com.codename1.ui.Component.pointerReleased(Component.java:3266)
    at com.codename1.ui.Display.handleEvent(Display.java:2025)
    at com.codename1.ui.Display.edtLoopImpl(Display.java:1070)
    at com.codename1.ui.Display.mainEDTLoop(Display.java:999)
    at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120)
    at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)
java.io.IOException: Query does not return results
    at com.codename1.impl.javase.SEDatabase.executeQuery(SEDatabase.java:176)
    at com.pivotal.calendar.Calender$4.actionPerformed(Calender.java:211)
    at com.codename1.ui.util.EventDispatcher.fireActionEvent(EventDispatcher.java:349)
    at com.codename1.ui.Button.fireActionEvent(Button.java:435)
    at com.codename1.ui.Button.released(Button.java:467)
    at com.codename1.ui.Button.pointerReleased(Button.java:566)
    at com.codename1.ui.Form.pointerReleased(Form.java:2642)
    at com.codename1.ui.Form.pointerReleased(Form.java:2578)
    at com.codename1.ui.Component.pointerReleased(Component.java:3266)
    at com.codename1.ui.Display.handleEvent(Display.java:2025)
    at com.codename1.ui.Display.edtLoopImpl(Display.java:1070)
    at com.codename1.ui.Display.mainEDTLoop(Display.java:999)
    at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120)
    at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)
1
Gaurav Takte 6 abr. 2017 a las 10:35

2 respuestas

La mejor respuesta

En lugar de llamar a db.executeQuery() para su declaración CREATE TABLE, debe llamar a db.execute().

5
Eric O'Connell 6 abr. 2017 a las 07:40

CREATE TABLE no es una consulta; No devuelve ningún dato.

Para ejecutar una consulta, use executeQuery(), que devuelve un cursor para acceder a los datos devueltos.
Para ejecutar cualquier otra instrucción SQL, use execute() en su lugar.

2
CL. 6 abr. 2017 a las 07:40