2014-11-23 91 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?