如何验证 SQLite 命令?
我不知道如何在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果SQL语句执行失败将会抛出异常。这意味着,如果
db.execSQL(...)
后面的代码正常运行,则 SQL 已正确执行。简单的!如果您担心表可能已存在,请考虑使用
CREATE TABLE IF NOT EXISTS tbl_user (...)
。[编辑]:如果你想检查create是否存在,你需要捕获异常:
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:
如果创建表失败,则会抛出异常。因此,如果创建过程没有抛出异常,您就可以非常确定该表存在。
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.