2014-11-23 55 views
-1
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等于我的表与第一列:sqlite getPosition()和getCount()崩溃应用程序

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

为getPosition()是不是唯一的功能崩溃,其他我试过也崩溃的应用程序包括:getString(0)和getCount()。虽然if块中的getCount()在检查表中是否存在某行时工作得很好。

它也没有帮助,logcat只告诉我,这个我的应用程序崩溃只在这一行。任何帮助,为什么logcat不是非常描述或为什么我的应用程序崩溃在所有将不胜感激。

+0

试试我的答案并更新 – 2014-11-23 06:42:30

+0

是的,现在有用,非常感谢!我没有意识到rawQuery在第一个入口之前返回Cursor对象,所以我可能要求一个负向索引。非常感谢你! – incyc70 2014-11-23 06:57:36

回答

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

您的inputstreamreader(是)已编码。在您执行 "lineRead = is.readLine();"之后,执行logcat操作并检查输入流读取器。

-1

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

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