sqlite getPosition()和getCount()崩溃应用程序

时间:2014-11-23 06:24:53

标签: android sqlite

userDB = openOrCreateDatabase("UserDB", Context.MODE_PRIVATE, null);
userDB.execSQL("CREATE TABLE IF NOT EXISTS users(username VARCHAR NOT NULL,password VARCHAR NOT NULL,credential VARCHAR NOT NULL,PRIMARY KEY(username));");
String lineRead;
try {
    lineRead = is.readLine();
    while (lineRead != null) {
        String[] splitLine = lineRead.split(",");
        Cursor c = userDB.rawQuery(
                "SELECT * FROM users WHERE username='" + splitLine[0]
                        + "'", null);
        if (c.getCount() == 0) {
            userDB.execSQL("INSERT INTO users(username, password, credential) VALUES (\'"
                    + splitLine[0]
                    + "\', \'"
                    + splitLine[1]
                    + "\', \'"
                    + splitLine[2] + "\');");
        }
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
Cursor c = userDB.rawQuery(
        "SELECT * FROM users WHERE username='" + username
                + "' AND password='" + password + "'", null);
if (c.getCount() == 0) {
    TextView tv = (TextView) findViewById(R.id.loginResult);
    tv.setText("Invalid User ID/Password.");
    return;
}
TextView tv = (TextView) findViewById(R.id.loginResult);
tv.setText(c.getPosition());

我的应用无论何时到达最后一行都会一直崩溃。我认为我的表格格式正确,我的查询应该是正确的,因为我已经能够将TextView设置为等于表格的第一列:

tv.setText(c.getColumnNames()[0]);

getPosition()不是崩溃的唯一函数,我试过的其他函数也崩溃了应用程序包括:getString(0)和getCount()。虽然if块中的getCount()在检查表中是否存在某个行时完全正常。

logcat只告诉我这个应用程序只在这一行崩溃,这也无济于事。任何关于为什么logcat不具有描述性或为什么我的应用程序崩溃的帮助都将非常感激。

3 个答案:

答案 0 :(得分:0)

   String str= null;
   c.moveToFirst();
    if(c.getCount() > 0){

        str= c.getString(1);  // Here you need to edit string or int. And column no also

    }  else {
       str= "Invalid Login Credentials";
    }
    c.close();
    userDB.close();

  TextView tv = (TextView) findViewById(R.id.loginResult);
  tv.setText(str);

答案 1 :(得分:-1)

您的inputstreamreader(是)已编码。在你做完之后做一个logcat操作 "lineRead = is.readLine();"并检查输入流阅读器。

答案 2 :(得分:-1)

您正在崩溃,因为您正在使用整数调用tv.setText。该整数应该是字符串资源的ID,但是你传入的是-1,这是你调用其中一个set-position函数之前的光标位置,如moveToFirst。

我不确定你的意图是什么;你想把什么文本放入TextView?