Codename一个存储错误值的SQL数据库

时间:2017-07-04 00:57:18

标签: java mysql database codenameone

我习惯使用Java开发桌面应用程序。现在我正在尝试使用Codename One来开发我的第一个移动应用程序。

尝试复制我对SQL数据库的体验我遇到了一个非常奇怪的存储行为,我无法解释。

创建了数据库,但是当我更改表输入值时,新值将被忽略,只添加旧值。要保存新值,我必须删除数据库。

我喜欢这个界面,任何帮助都会受到赞赏。

Database db = Display.getInstance().openOrCreate("MyDB.db");

db.execute("CREATE TABLE IF NOT EXISTS Persons (Date NOT NULL,Event NOT NULL)");




String sql = "INSERT INTO Persons (DATE , Event) " + "VALUES ( 'John', '10000.00' );"; 
db.execute (sql);

// adds "John" to the database every time I click the button 
// then I change the from "John" to "James"
// I am not adding the lines twice, just change the input



String sql = "INSERT INTO Persons (DATE , Event) " + "VALUES ( 'James', '10000.00' );"; 
db.execute (sql);

//keeps adding "John" to the database, even though value has been changed to "James" 




Cursor cur = db.executeQuery("select * from Persons;");
Row currentRow= cur.getRow();
String dataText = currentRow.getString(0);

while (cur.next()) {
             System.out.println(dataText);
        }

1 个答案:

答案 0 :(得分:3)

您未在dataText循环中将下一行提取到while(),因此您只需重复打印第一行中的文字。

应该是:

Cursor cur = db.executeQuery("select * from Persons;");
while (cur.next()) {
    Row currentRow = cur.getRow();
    String dataText = currentRow.getString("Date");
    System.out.println(dataText);
}

如果您使用单独的查询工具(例如PhpMyAdmin)检查表格,您应该会看到它包含两行。

我希望我的语法正确。我不是Java程序员,而是从tutorial得到的。