SQLite查询的逻辑错误

时间:2012-06-24 04:37:35

标签: android sqlite android-contentprovider

所以我正在为我的班级创建一个应用程序,用户提交表单并将数据记录在数据库中。现在我正在尝试执行检查,检查用户试图选择的用户名是否存在于数据库中。这是我现在的代码:

//Username duplicate check                                                                                                                       
String mSelection = MyContentProvider.COLUMN_USERNAME + "= ?";
String[] mSelectionArgs = new String[] { "Username" };
String[] mProjection = new String[]{MyContentProvider.COLUMN_USERNAME};
mCursor = getContentResolver().query(MyContentProvider.CONTENT_URI,
                                     mProjection,
                                     mSelection,
                                     mSelectionArgs, 
                                     null);
if (mCursor.moveToFirst()){
if (userName.getText().toString().equals(mCursor.getString(mCursor.getColumnIndex(MyContentProvider.COLUMN_USERNAME)))){
    submitCheck = false;
    userName.setHint("Username taken");
}   

}     mCursor.close();

运行此命令时,if语句会出错。这是logcat:

06-24 00:56:55.125: E/AndroidRuntime(907): FATAL EXCEPTION: main
06-24 00:56:55.125: E/AndroidRuntime(907): android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0
06-24 00:56:55.125: E/AndroidRuntime(907):  at android.database.AbstractCursor.checkPosition(AbstractCursor.java:400)
06-24 00:56:55.125: E/AndroidRuntime(907):  at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
06-24 00:56:55.125: E/AndroidRuntime(907):  at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
06-24 00:56:55.125: E/AndroidRuntime(907):  at android.database.CursorWrapper.getString(CursorWrapper.java:114)
06-24 00:56:55.125: E/AndroidRuntime(907):  at com.connor.black.HomeWork2Activity$1.onClick(HomeWork2Activity.java:132)
06-24 00:56:55.125: E/AndroidRuntime(907):  at android.view.View.performClick(View.java:3511)
06-24 00:56:55.125: E/AndroidRuntime(907):  at android.view.View$PerformClick.run(View.java:14105)
06-24 00:56:55.125: E/AndroidRuntime(907):  at android.os.Handler.handleCallback(Handler.java:605)
06-24 00:56:55.125: E/AndroidRuntime(907):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-24 00:56:55.125: E/AndroidRuntime(907):  at android.os.Looper.loop(Looper.java:137)
06-24 00:56:55.125: E/AndroidRuntime(907):  at android.app.ActivityThread.main(ActivityThread.java:4424)
06-24 00:56:55.125: E/AndroidRuntime(907):  at java.lang.reflect.Method.invokeNative(Native Method)
06-24 00:56:55.125: E/AndroidRuntime(907):  at java.lang.reflect.Method.invoke(Method.java:511)
06-24 00:56:55.125: E/AndroidRuntime(907):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-24 00:56:55.125: E/AndroidRuntime(907):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-24 00:56:55.125: E/AndroidRuntime(907):  at dalvik.system.NativeStart.main(Native Method)

以下是我的内容提供商的一些内容:

public class MyContentProvider extends ContentProvider{
public final static String DBNAME = "FormStorage";
public static final String AUTHORITY = "com.connor.black.provider";
public final static String TABLE_NAMESTABLE = "formstable";
private static UriMatcher sUriMatcher;

public final static String COLUMN_FIRSTNAME = "FirstName";
public final static String COLUMN_LASTNAME = "LastName";
public final static String COLUMN_PHONE = "PhoneNumber";
public final static String COLUMN_EMAIL = "Email";
public final static String COLUMN_USERNAME = "Username";
public final static String COLUMN_PASSWORD = "Password";
public final static String COLUMN_GENDER = "Gender";
public final static String COLUMN_COUNTRY = "Country";
public static final Uri CONTENT_URI = Uri.parse("content://com.connor.black.provider/" + TABLE_NAMESTABLE);


private static final String SQL_CREATE_MAIN = "CREATE TABLE " + TABLE_NAMESTABLE + "("+"_ID INTEGER PRIMARY KEY, "+
                                                                    "FirstName TEXT, " +
                                                                    "LastName TEXT, " +
                                                                    "PhoneNumber TEXT, " +
                                                                    "Email TEXT, " +
                                                                    "Username TEXT, " +
                                                                    "Password TEXT, " +
                                                                    "Gender TEXT, " +
                                                                    "Country TEXT)";

2 个答案:

答案 0 :(得分:0)

您应将列索引值直接硬编码到代码中。这是因为,

  1. 设计很糟糕。

  2. 它使您的项目难以维护(即如果您稍后向表中添加列,则列的整数可能会更改,从而破坏您的代码)。

  3. 无法确定0是否与用户名列对应。整数值由管理SQLite数据库的内部库分配......您应该从不依赖内部库,因为Android团队可以随时更改实施。

  4. 我建议您将代码更改为以下内容:

    int userCol = mCursor.getColumnIndex(MyContentProvider.USERNAME_COLUMN);
    mCursor.getString(userCol);
    

答案 1 :(得分:0)

看起来第0个是你的表中的_ID用第5个索引来试试..........