在 Codename One 应用程序中使用 SQLite

Using SQLite in Codename One Application

我有一个工作的 sqlite 数据库,我把它放在我的 /src 文件夹中。 然后我进入代号一网站并按照他们的文档示例

    Database db = null;
    Cursor cur = null;
    try {
        db = Display.getInstance().openOrCreate("MyDb.db");
        if(query.getText().startsWith("select")) {
            cur = db.executeQuery(query.getText());
            int columns = cur.getColumnCount();
            frmMain.removeAll();
            if(columns > 0) {
                boolean next = cur.next();
                if(next) {
                    ArrayList<String[]> data = new ArrayList<>();
                    String[] columnNames = new String[columns];
                    for(int iter = 0 ; iter < columns ; iter++) {
                        columnNames[iter] = cur.getColumnName(iter);
                    }
                    while(next) {
                        Row currentRow = cur.getRow();
                        String[] currentRowArray = new String[columns];
                        for(int iter = 0 ; iter < columns ; iter++) {
                            currentRowArray[iter] = currentRow.getString(iter);
                        }
                        data.add(currentRowArray);
                        next = cur.next();
                    }
                    Object[][] arr = new Object[data.size()][];
                    data.toArray(arr);
                    frmMain.add(BorderLayout.CENTER, new Table(new DefaultTableModel(columnNames, arr)));
                } else {
                    frmMain.add(BorderLayout.CENTER, "Query returned no results");
                }
            } else {
                frmMain.add(BorderLayout.CENTER, "Query returned no results");
            }
        } else {
            db.execute(query.getText());
            frmMain.add(BorderLayout.CENTER, "Query completed successfully");
        }
        frmMain.revalidate();
    } catch(IOException err) {

        frmMain.removeAll();
        frmMain.add(BorderLayout.CENTER, "Error: " + err);
        frmMain.revalidate();
    } finally {
        Util.cleanup(db);
        Util.cleanup(cur);
    }

然而,当我 运行 这个例子并尝试执行一个简单的 select 查询时,我得到了这个错误...

java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database (no such table: MyTable)

我是不是漏了一步?

谢谢

您确定当前执行时的工作目录是 ./src 吗?

尝试

db = Display.getInstance().openOrCreate("./src/MyDb.db");

或用绝对文件名打开:

db = Display.getInstance().openOrCreate("/path/to/src/MyDb.db");

你可以试试这个 cn1lib https://github.com/shannah/cn1-data-access-lib

我用过它,它很有用,除了对同一查询中的 2 个表不起作用并且不能执行删除操作。

干杯

感谢所有提供意见的人。

不幸的是 none 的建议对我有用。 不过我最后还是解决了。

原来我的主目录中有一个名为“.cn1/database”的文件夹。一旦我将数据库放入此文件夹中,它就可以工作了。

两件事: 1] 如果数据库不存在,那么它将创建它并将其放入此目录 2] 数据库没有出现在 Netbeans 的任何地方(无论如何我都看不到)

再次感谢

来自developer guide

Some SQLite apps ship with a "ready made" database. We allow you to replace the DB file by using the code:

String path = Display.getInstance().getDatabasePath(“databaseName”);

You can then use the FileSystemStorage class to write the content of your DB file into the path. Notice that it must be a valid SQLite file!

Important: getDatabasePath() is not supported in the Javascript port. It will always return null.

This is very useful for applications that need to synchronize with a central server or applications that ship with a large database as part of their core product.

您依赖在模拟器中有意义的路径,在设备中您需要将资源复制到位置。查看实现此功能的 SQL 演示:https://www.codenameone.com/blog/sql-demo-revisited.html

将数据库文件放入src文件夹后,使用以下代码将数据库复制到Codenameone存储,运行

后数据库将复制到目录“.cn1/database”
String DB_NAME = "DBNAME.db";
Database temp_db = Database.openOrCreate(DB_NAME); //To create an empty file before copy, otherwise Android will throw file not found exception
temp_db.close();
String p = Database.getDatabasePath(DB_NAME);
OutputStream o = FileSystemStorage.getInstance().openOutputStream(p);
InputStream i = Display.getInstance().getResourceAsStream(getClass(), "/" + DB_NAME);
Util.copy(i, o);

这似乎不是一个完整的答案。

我正在浏览演示,似乎缺少某些内容:
构建任务不应该将此数据库资源复制到正确的目标文件夹吗?否则部署如何工作? (如果数据库没有打包,那么它将如何部署?)没有它,应用程序就无法 运行