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不是非常描述或为什么我的应用程序崩溃在所有将不胜感激。
试试我的答案并更新 – 2014-11-23 06:42:30
是的,现在有用,非常感谢!我没有意识到rawQuery在第一个入口之前返回Cursor对象,所以我可能要求一个负向索引。非常感谢你! – incyc70 2014-11-23 06:57:36