代码之家  ›  专栏  ›  技术社区  ›  rainer

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

  •  1
  • rainer  · 技术社区  · 9 年前

    我习惯用Java开发桌面应用程序。现在,我正在尝试代号一开发我的第一个移动应用程序。

    尝试复制我使用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 回复  |  直到 9 年前
        1
  •  3
  •   ItamarG3    9 年前

    您没有将下一行提取到 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

    tutorial .

    推荐文章