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 转换为布尔值...

4

2 回答 2

1

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...
}
于 2012-01-10T10:41:59.187 回答
0

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.

于 2012-01-10T10:41:48.137 回答