在创建表

时间:2017-04-06 07:35:30

标签: java android mysql sqlite codenameone

我正在开发一个将用户事件存储在数据库中的应用程序。为此,我使用SQLite进行存储,但是当我执行查询时,它会抛出错误。 以下是我点击添加按钮时触发操作的代码:

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();
         }
        }
       });

应用程序抛出的错误如下:

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)

2 个答案:

答案 0 :(得分:5)

您需要拨打execute(),而不是为db.executeQuery()语句调用CREATE TABLE

答案 1 :(得分:2)

CREATE TABLE不是查询;它不会返回任何数据。

要执行查询,请使用$qb->select(IDENTITY(lsc.serviceCompany)) (...) ,它返回一个光标以访问返回的数据 要执行任何其他SQL语句,请改用executeQuery()