【问题标题】:SQLite - Select text and get value to Textfield in Codename One for JavaSQLite - 在 Codename One for Java 中选择文本并获取文本字段的值
【发布时间】:2018-08-25 20:39:43
【问题描述】:

我提出了一个关于存储在数据库中的移动应用程序数据的问题,但不是我指定的数据库:

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

我创建了另一个 GUI 元素来搜索数据库以获取特定信息以进行测试。

CodeName One 使用数据库 API。 https://www.codenameone.com/javadoc/com/codename1/db/Database.html

不幸的是,网上有很多很好的例子,但是关于代号一号的却没有。

Database db = null;
Cursor cur = null;

        try{

            Database ARdb = Display.getInstance().openOrCreate("RecordsDB.db"); 

            System.out.println("Connection secured to database.");

            ARdb.beginTransaction();

            String SelectRecords = "SELECT Last_Name, Phone, Email, Postcode 
            FROM TestRecord";

            cur = ARdb.executeQuery(SelectRecords);

            //This is where I'm trying to set the text of col Last_Name
            findTxtLastnSearch(c).setText(cur.getText("Last_Name"));

            ARdb.commitTransaction(); 

        } catch(Exception e){

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

        } finally {

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

        }
    }

我已选择要测试的记录。我现在想将结果上传到文本字段,但是,我不知道该怎么做。任何帮助将不胜感激。

【问题讨论】:

    标签: java sqlite codenameone


    【解决方案1】:

    您从执行的查询中得到的不是单行,而是包含多行的游标,因此您需要遍历它并处理每一行。

    查看第二个链接中的示例。像这样的

    int indexForLastName = cur.getColumnIndex("Last_Name");
    while(cur.next()) {
      Row row = cur.getRow()
      String lastname = row.getString(indexForLastName);
      //more stuff
    }
    

    【讨论】:

    • 同行:String lastname = row.getString("Last_Name"); - 我无法转换为 int?顺便说一句,感谢您的帮助,
    • 是的,getString 接受一个 int 而不是一个字符串,我的错。只需使用它。
    • 老实说,这是我使用该代码时一直遇到的错误。我会尝试其他一些事情。
    • 我想一种方法是从游标方法getColumnIndex中获取索引。我更新了我的答案以使用这个
    • 在问题的第二个链接中,选择顶部的“Frames”,然后在“All Classes”下方的左列中找到指向 Cursor 和 Row 文档的链接
    猜你喜欢
    • 2018-08-07
    • 1970-01-01
    • 1970-01-01
    • 2019-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多