问题
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