将第二条记录添加到数据库后,代号为一个 Sqlite 问题

Posted

技术标签:

【中文标题】将第二条记录添加到数据库后,代号为一个 Sqlite 问题【英文标题】:Codename one Sqlite issue after adding a second record to the database 【发布时间】:2017-04-06 09:05:33 【问题描述】:

现在我已经成功检索以及将数据插入数据库,但是当我第二次向数据库插入数据时会抛出错误。因为每次应用程序到达主页时都会触发创建表查询.但我不知道如何纠正这个问题。

下面是我的代码:-

public class Calender 

    private Form current;
    private Resources theme;
    Form calendar, saveEvent, DataEvent;
    Label dateLabel;
    TextField eventArea, descriptionArea;
    Customised cal;
    Database db = null;
    Cursor cur = null;

    public void init(Object context) 
        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);

        // Pro only feature, uncomment if you have a pro subscription
        // Log.bindCrashProtection(true);
        String path = Display.getInstance().getDatabasePath("Events.db");
        FileSystemStorage fs = FileSystemStorage.getInstance();
        if (!fs.exists(path)) 
            try (InputStream is = Display.getInstance().getResourceAsStream(getClass(), "/Events.db");
                    OutputStream os = fs.openOutputStream(path)) 
                Util.copy(is, os);
             catch (IOException err) 
                Log.e(err);
            
        
        try 
            db = Display.getInstance().openOrCreate("Events.db");
            db.execute("CREATE TABLE Personsse (Date date NOT NULL,Event varchar(255) NOT NULL, Description varchar(255) NOT NULL)");
         catch (IOException e) 
            e.printStackTrace();
        
    

    public void start() 
        if (current != null) 
            current.show();
            return;
        
        try 
            SplashScreen spl = new SplashScreen();
            spl.show();

            new java.util.Timer().schedule(
                    new java.util.TimerTask() 
                @Override
                public void run() 
                    calendar.show();
                
            , 4000);
         catch (IOException e) 
            e.printStackTrace();
        
        DataEvent = new Form("Data Explorer", new BorderLayout());
        Command back1 = new Command("Back") 
            public void actionPerformed(ActionEvent ev) 
                calendar.show();
            
        ;
        DataEvent.setBackCommand(back1);
        calendar = new Form("Events", new BoxLayout(BoxLayout.Y_AXIS));
        cal = new Customised();

        calendar.add(cal);

        Label date = new Label("Event Date:- ");
        dateLabel = new Label();
        Label event = new Label("Event:- ");
        eventArea = new TextField();
        Label description = new Label("Event Description:- ");
        descriptionArea = new TextField();

        Container container = TableLayout.encloseIn(2, date, dateLabel, event, eventArea, description, descriptionArea);
        container.setUIID("container");
        calendar.add(container);
        cal.addActionListener(new ActionListener() 
            @Override
            public void actionPerformed(ActionEvent evt) 
                try 
                    SimpleDateFormat sdf = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy");
                    String selectedDate = new Date(cal.getSelectedDay()).toString();
                    Date date1 = sdf.parse(selectedDate);
                    SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
                    dateLabel.setText(df.format(date1));
                 catch (ParseException ex) 
                    ex.printStackTrace();
                

                cal.createDay();
            

        );

        Toolbar tb = new Toolbar();
        calendar.setToolBar(tb);

        tb.addCommandToRightBar("", FontImage.createMaterial(FontImage.MATERIAL_SAVE, UIManager.getInstance().getComponentStyle("TitleCommand")), (evt) -> 
            saveEvent.show();
        );
        tb.addCommandToLeftBar("", FontImage.createMaterial(FontImage.MATERIAL_EVENT, UIManager.getInstance().getComponentStyle("TitleCommand")), (evt) -> 
            DataEvent.show();
        );

        Button add = new Button("Add Event");
        Button clear = new Button("Clear Event");
        calendar.add(add);
        calendar.add(clear);

        //Saved Events Form Starts  ====================================================================================
        saveEvent = new Form("Saved Event", new BoxLayout(BoxLayout.Y_AXIS));
        Command back = new Command("Back") 
            public void actionPerformed(ActionEvent ev) 
                calendar.show();
            
        ;
        saveEvent.setBackCommand(back);
        Toolbar tb1 = new Toolbar();
        saveEvent.setToolBar(tb1);
        tb1.addCommandToLeftBar("", FontImage.createMaterial(FontImage.MATERIAL_ARROW_BACK, UIManager.getInstance().getComponentStyle("TitleCommand")), back);
        //Save Events Form Ends =======================================================================================

        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 

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

                        db.execute("INSERT INTO Personsse (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 Personsse");
                                    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();
                                

                                DataEvent.show();
                            
                        );

                     catch (IOException e) 
                        e.printStackTrace();
                    

                
            
        );


错误如下:-

应用程序启动时出现以下错误:-

java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database (table Personsse already exists)
    at org.sqlite.DB.newSQLException(DB.java:886)
    at org.sqlite.DB.newSQLException(DB.java:897)
    at org.sqlite.DB.throwex(DB.java:864)
    at org.sqlite.NativeDB.prepare(Native Method)
    at org.sqlite.DB.prepare(DB.java:207)
    at org.sqlite.PrepStmt.<init>(PrepStmt.java:50)
    at org.sqlite.SQLiteConnection.prepareStatement(SQLiteConnection.java:616)
    at org.sqlite.SQLiteConnection.prepareStatement(SQLiteConnection.java:606)
    at org.sqlite.SQLiteConnection.prepareStatement(SQLiteConnection.java:578)
    at com.codename1.impl.javase.SEDatabase.execute(SEDatabase.java:92)
    at com.pivotal.calendar.Calender.init(Calender.java:76)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.codename1.impl.javase.Executor$1$1.run(Executor.java:117)
    at com.codename1.ui.Display.processSerialCalls(Display.java:1154)
    at com.codename1.ui.Display.mainEDTLoop(Display.java:971)
    at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120)
    at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)
java.io.IOException: [SQLITE_ERROR] SQL error or missing database (table Personsse already exists)
    at com.codename1.impl.javase.SEDatabase.execute(SEDatabase.java:96)
    at com.pivotal.calendar.Calender.init(Calender.java:76)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.codename1.impl.javase.Executor$1$1.run(Executor.java:117)
    at com.codename1.ui.Display.processSerialCalls(Display.java:1154)
    at com.codename1.ui.Display.mainEDTLoop(Display.java:971)
    at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120)
    at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)
[EDT] 0:0:0,1 - Codename One revisions: 16f9f308f6ef10ac0e25a8afbd37b8066f5a391d
2348

当向数据库添加第二条记录时,应用程序抛出以下错误:-

[EDT] 0:0:0,46 - 26/04/2017
**** WARNING! DB Cursor was released by the GC without being closed first! This might cause crashes on ios *****
[EDT] 0:0:15,345 - 26/04/2017
java.lang.NullPointerException
[EDT] 0:0:16,278 - Exception: java.lang.NullPointerException - null
    at com.codename1.impl.javase.SEDatabase.execute(SEDatabase.java:103)
    at com.pivotal.calendar.Calender$5.actionPerformed(Calender.java:212)
    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)

而且我还有一些与 Codename one SQLite 相关的查询,例如:-如何按升序将表格数据打印到标签,如何在 SpanLabel 中一次打印一行。

【问题讨论】:

【参考方案1】:

您的表已经存在,因此您会收到此错误,为避免这种情况,您可以使用IF NOT EXISTS

CREATE TABLE IF NOT EXISTS Personsse ...

所以,发生这种情况,当你执行程序不止一次,所以第一次它会创建表,当你再次执行它时,表已经退出,所以繁荣,这个错误发生了,表已经存在。

【讨论】:

我会在你的解决方案的帮助下解决这个问题,所以谢谢你,但是当我向数据库添加第二条记录时,它会抛出一个错误:- EDT] 0:0:23,685 -异常:java.lang.NullPointerException - null 嗯很好,这意味着某些东西没有初始化或为空,你能告诉我你的代码中是否发生了这种情况@GRV_Droid 第二次调用插入查询时会抛出错误。当应用程序第一次在主页上时,我们可以将多条记录添加到数据库中,但是当应用程序导航到应用程序显示的另一个页面表并再次导航到主页以添加记录,它会抛出一个错误,说 JavaNullPointerException。 @YCF_L 24 @GRV_Droid 是一个著名的错误,您可以在这里查看,***.com/questions/218384/… 尝试逐步调试您的程序,并检查它在哪一行抛出此错误,好的,告诉我,也许我可以帮助你

以上是关于将第二条记录添加到数据库后,代号为一个 Sqlite 问题的主要内容,如果未能解决你的问题,请参考以下文章

将第二个输入语句添加到代码后出错 [关闭]

如何使用 SQL Server 更新触发器中的原始值更改第二条记录

按组中第二条记录的数据对记录组进行排序

从数据库中检索每个用户的倒数第二条记录

将第二个表中的第二个(条件)结果添加到 SQL 查询

处理文件行流迭代器