检查如果列存在于Android的应用程序数据库应用程序、数据库、Android

2023-09-05 05:37:28 作者:老衲法號帥哥

有没有在Android的一个很好的方法,如果一列存在于表中的应用程序数据库? (我知道有一个与此相似的问题已经,但似乎并没有任何是安卓专用。)

Is there a nice way in Android to see if a column exists in a table in the application database? (I know there are questions similar to this one already, but there don't seem to be any that are Android specific.)

推荐答案

cursor.getColumnIndex(字符串COLUMNNAME)返回-1如果列不存在。所以,我基本上执行像SELECT * FROM XXX极限0,1,并使用光标以确定是否列,你正在寻找一个简单的查询,存在

cursor.getColumnIndex(String columnName) returns -1 if, the column doesn't exist. So I would basically perform a simple query like "SELECT * FROM xxx LIMIT 0,1" and use the cursor to determine if the column, you are looking for, exists

您可以尝试查询栏选择theCol从xxx,并捕获异常

you can try to query the column "SELECT theCol FROM xxx" and catch an exception