SQLite - no errors, however, no data uploaded to DB

空扰寡人 提交于 2019-12-08 09:41:57

问题


I have developed a mobile application using the CodeName One plugin for Java in the Netbeans IDE.

CodeName One uses the Database API. https://www.codenameone.com/javadoc/com/codename1/db/Database.html

I'm running some tests (there are around 10 values I would like to upload, however, just testing the connection ect by uploading ID, Fname and Lname values.

Database db = null;
        Cursor cur = null;
        String Fname = findTxtFirstn(c).getText();
        String Lname = findTxtLastn(c).getText();

        try{

            Database ARdb = Display.getInstance().openOrCreate("RecordsDB.db");            
            System.out.println("Connection secured to database.");

            ARdb.beginTransaction();

            String createTable = "CREATE TABLE IF NOT EXISTS RecordsTable ("
                    + "ID integer PRIMARY KEY,"
                    + "First_Name text NOT NULL,"
                    + "Last_Name text NOT NULL)";

            String query = "insert into RecordsTable (ID,First_Name,Last_Name) values (3,'Test','Testerton')";       

            ARdb.execute(createTable);
            ARdb.execute(query);

            ARdb.commitTransaction();  

        } catch(Exception e){

            System.out.println("Error! Connection Failed to DB" +e.getMessage());

        } finally {

            Util.cleanup (db);
            Util.cleanup(cur);

        }

I get no errors and everything runs, however, the values are not in the database when I check it. Am I missing something here? I have followed tutorials and looked over the Codename One API. I can't find the solution.

Edit: I need to change the value of the primary number each run (else I get an error: number needs to be unique), This tells me the values are being stored on the database, unfortunately, when I check the database in question there are no records on it, so where it the data going?

I used DB Browser for SQLite.


回答1:


Have you tried performing your modifying change in a transaction? In databases with transactions all modifying operations (CREATE, INSERT, ...) need to be performed in a transaction.

ARdb.beginTransaction();
// your create code 
ARdb.commitTransaction();


来源:https://stackoverflow.com/questions/49239038/sqlite-no-errors-however-no-data-uploaded-to-db

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!