如何验证 SQLite 命令?

发布于 2024-12-26 02:01:37 字数 419 浏览 0 评论 0原文

我不知道如何在 Android 中验证 SQLite 命令...... 当我有该代码时:

final String CREATE_TABLE_USER =
                    "CREATE TABLE tbl_user ("
                    + "id INTEGER PRIMARY KEY AUTOINCREMENT,"
                    + "username VARCHAR(20));";
db.execSQL(CREATE_TABLE_USER);

我喜欢检查“已创建”:

if (tbl_user has been created)

我该如何编写? Eclipse 说我无法将 void 转换为布尔值...

I don't know how to verifiy SQLite commands in Android...
When I have that code:

final String CREATE_TABLE_USER =
                    "CREATE TABLE tbl_user ("
                    + "id INTEGER PRIMARY KEY AUTOINCREMENT,"
                    + "username VARCHAR(20));";
db.execSQL(CREATE_TABLE_USER);

I like to check "has been created":

if (tbl_user has been created)

How can I write that?
Eclipse says I can't convert a void to a boolean...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤单情人 2025-01-02 02:01:37

如果SQL语句执行失败将会抛出异常。这意味着,如果 db.execSQL(...) 后面的代码正常运行,则 SQL 已正确执行。简单的!

如果您担心表可能已存在,请考虑使用CREATE TABLE IF NOT EXISTS tbl_user (...)


[编辑]:如果你想检查create是否存在,你需要捕获异常:

try {
    db.execSQL(CREATE_TABLE_USER);
    doMessage("made the tbl_user table");
} catch (SQLException exn) {
    doMessage("did not make the tbl_user table: " + exn.getMessage());
    throw exn; // Let the exception propagate as we're just reporting...
}

An exception will be thrown if the SQL statement fails to execute. That means that if the code normally following the db.execSQL(...) runs, the SQL was executed correctly. Easy!

Consider using CREATE TABLE IF NOT EXISTS tbl_user (...) if you're worried about the potential for the table to already exist.


[EDIT]: If you want to check whether the create exists, you need to trap the exception:

try {
    db.execSQL(CREATE_TABLE_USER);
    doMessage("made the tbl_user table");
} catch (SQLException exn) {
    doMessage("did not make the tbl_user table: " + exn.getMessage());
    throw exn; // Let the exception propagate as we're just reporting...
}
泅渡 2025-01-02 02:01:37

如果创建表失败,则会抛出异常。因此,如果创建过程没有抛出异常,您就可以非常确定该表存在。

If creating the table fails, an Exception will be thrown. So if the creation doesn't throw an exception you can be pretty darn sure the table exists.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文