Android SQLite用户登录不起作用

知识养成了思想,思想同时又在融化知识。这篇文章主要讲述Android SQLite用户登录不起作用相关的知识,希望能为你提供帮助。
我实际上在我的应用程序中实现了一个SQLite数据库并进行了一个Login活动。因此,当应用程序启动时,我启动我的登录活动,并在那里我访问数据库。因此,用户输入名称和密码并按“anmelden”,然后数据库检查用户是否已经存在,如果是,则密码是否正确,如果正确,则活动结束,用户可以继续。如果密码不正确但用户存在,则不会发生任何事情,并且将显示Toast。如果没有给出Name的用户,则将使用该Password和Name创建一个新用户。
我的问题是,当我创建一个新用户时,一切似乎工作正常,但如果我重新启动应用程序并尝试使用相同的数据登录,它仍然会创建一个新用户。
我遗憾地不知道这在数据库中看起来怎么样因为我没有root电话atm,所以我希望你们能帮助我,因为我没有发现错误
这是我的“Anmeldung”(登录)课程

public class Anmeldung extends Activity { public Datasource datasource = new Datasource(this); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_anmeldung); }//// OnClick-Methoden/// public void anmelden(View view) {//the Login methodEditText t = (EditText) findViewById(R.id.editText_username); String name = t.getText().toString(); EditText e = (EditText) findViewById(R.id.editText_passwort); String psswd = e.getText().toString(); datasource.open(); if (datasource.UserExists(name)) { if (datasource.correctPassword(name, psswd)) { datasource.close(); finish(); } else { @SuppressLint("WrongConstant") Toast toast = Toast.makeText(getApplicationContext(), "Falsches Passwort", 3); toast.show(); } } else { datasource.createUser(name, psswd, 0, 0); @SuppressLint("WrongConstant") Toast toast = Toast.makeText(getApplicationContext(), "Neuen Nutzer erstellt", 3); toast.show(); finish(); }}

}
这是我的数据源类,包含所有相关方法
public class Datasource { //Log-Tag private static final String LOG_TAG = Datasource.class.getSimpleName(); //Datenbankspezifische Variablen private SQLiteDatabase database; private Datahelper dbHelper; private String[] columns = {Datahelper.COLUMN_ID, Datahelper.COLUMN_TYPE, Datahelper.COLUMN_NAME, Datahelper.COLUMN_EXPIRATION, Datahelper.COLUMN_OWNER}; private String[] columns_users = {Datahelper.COLUMN_ID_USERS, Datahelper.COLUMN_NAME_USERS, Datahelper.COLUMN_PSSWD_USERS, Datahelper.COLUMN_SOAP_USERS, Datahelper.COLUMN_EP_USERS}; private String[] column_id_user = {Datahelper.COLUMN_ID_USERS}; private String[] column_name_user = {Datahelper.COLUMN_NAME_USERS}; private String[] column_psswd_user = {Datahelper.COLUMN_PSSWD_USERS}; //Konstruktor public Datasource(Context context) { Log.d(LOG_TAG, "DataSource is now creating the dbHelper"); dbHelper = new Datahelper(context); }//?ffnen der Datenbank public void open() { Log.d(LOG_TAG, "asking for reference"); database = dbHelper.getWritableDatabase(); Log.d(LOG_TAG, "received reference. path: " + database.getPath()); }//Schlie?en der Datenbank public void close() { dbHelper.close(); Log.d(LOG_TAG, "closed database"); }public User createUser(String name, String psswd, long soap, long ep) { ContentValues c = new ContentValues(); c.put(Datahelper.COLUMN_NAME_USERS, name); c.put(Datahelper.COLUMN_PSSWD_USERS, psswd); c.put(Datahelper.COLUMN_SOAP_USERS, soap); c.put(Datahelper.COLUMN_EP_USERS, ep); long insId = database.insert(Datahelper.TABLE_USERS, null, c); Cursor cursor = database.query(Datahelper.TABLE_USERS, columns_users, Datahelper.COLUMN_ID_USERS + "=" + insId, null, null, null, null); cursor.moveToLast(); return cursorToUser(cursor); }private User cursorToUser(Cursor cursor) {int idId = cursor.getColumnIndex(Datahelper.COLUMN_ID_USERS); int idName = cursor.getColumnIndex(Datahelper.COLUMN_NAME_USERS); int idPsswd = cursor.getColumnIndex(Datahelper.COLUMN_PSSWD_USERS); int idSoap = cursor.getColumnIndex(Datahelper.COLUMN_SOAP_USERS); int idEp = cursor.getColumnIndex(Datahelper.COLUMN_EP_USERS); long id = cursor.getLong(idId); String name = cursor.getString(idName); String psswd = cursor.getString(idPsswd); long soap = cursor.getLong(idSoap); long ep = cursor.getLong(idEp); return new User(id, name, psswd, soap, ep); }public boolean UserExists(String name) { try { Cursor cursor = database.query(Datahelper.TABLE_USERS, column_id_user, Datahelper.COLUMN_NAME_USERS + " = " + name, null, null, null, null); long id = cursor.getLong(cursor.getColumnIndex(Datahelper.COLUMN_ID_USERS)); cursor = database.query(Datahelper.TABLE_USERS, column_name_user, Datahelper.COLUMN_ID_USERS + " = " + id, null, null, null, null); String n = cursor.getString(cursor.getColumnIndex(Datahelper.COLUMN_NAME_USERS)); return true; } catch (Exception e) { } return false; }public boolean correctPassword(String name, String psswd) { try {Cursor cursor = database.query(Datahelper.TABLE_USERS, column_id_user, Datahelper.COLUMN_NAME_USERS + " = " + name, null, null, null, null); long id = cursor.getLong(cursor.getColumnIndex(Datahelper.COLUMN_ID_USERS)); cursor = database.query(Datahelper.TABLE_USERS, column_name_user, Datahelper.COLUMN_ID_USERS + " = " + id, null, null, null, null); String n = cursor.getString(cursor.getColumnIndex(Datahelper.COLUMN_NAME_USERS)); cursor = database.query(Datahelper.TABLE_USERS, column_psswd_user, Datahelper.COLUMN_ID_USERS + " = " + id, null, null, null, null); String p = cursor.getString(cursor.getColumnIndex(Datahelper.COLUMN_PSSWD_USERS)); if (n == name & & psswd == p) return true; } catch (Exception e) { } return false; }

}
希望你能帮助我,告诉你是否需要别的东西:)
日志:
12-20 20:33:56.684 24009-24009/com.ein_siedler.putzplaner D/Datasource: asking for reference 12-20 20:33:56.689 24009-24009/com.ein_siedler.putzplaner D/Datasource: received reference. path: /data/user/0/com.ein_siedler.putzplaner/databases/task_list.db 12-20 20:33:56.690 24009-24009/com.ein_siedler.putzplaner E/SQLiteLog: (1) no such column: Test 12-20 20:33:56.696 24009-24009/com.ein_siedler.putzplaner W/System.err: android.database.sqlite.SQLiteException: no such column: Test (code 1): , while compiling: SELECT _id FROM table_users WHERE name = Test 12-20 20:33:56.696 24009-24009/com.ein_siedler.putzplaner W/System.err:at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 12-20 20:33:56.696 24009-24009/com.ein_siedler.putzplaner W/System.err:at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889) 12-20 20:33:56.696 24009-24009/com.ein_siedler.putzplaner W/System.err:at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500) 12-20 20:33:56.696 24009-24009/com.ein_siedler.putzplaner W/System.err:at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) 12-20 20:33:56.696 24009-24009/com.ein_siedler.putzplaner W/System.err:at android.database.sqlite.SQLiteProgram.< init> (SQLiteProgram.java:58) 12-20 20:33:56.697 24009-24009/com.ein_siedler.putzplaner W/System.err:at android.database.sqlite.SQLiteQuery.< init> (SQLiteQuery.java:37) 12-20 20:33:56.698 24009-24009/com.ein_siedler.putzplaner W/System.err:at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44) 12-20 20:33:56.698 24009-24009/com.ein_siedler.putzplaner W/System.err:at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1328) 12-20 20:33:56.698 24009-24009/com.ein_siedler.putzplaner W/System.err:at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1175) 12-20 20:33:56.698 24009-24009/com.ein_siedler.putzplaner W/System.err:at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1046) 12-20 20:33:56.698 24009-24009/com.ein_siedler.putzplaner W/System.err:at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1214) 12-20 20:33:56.698 24009-24009/com.ein_siedler.putzplaner W/System.err:at com.ein_siedler.putzplaner.Datasource.UserExists(Datasource.java:139) 12-20 20:33:56.698 24009-24009/com.ein_siedler.putzplaner W/System.err:at com.ein_siedler.putzplaner.activities.Anmeldung.anmelden(Anmeldung.java:50) 12-20 20:33:56.698 24009-24009/com.ein_siedler.putzplaner W/System.err:at java.lang.reflect.Method.invoke(Native Method) 12-20 20:33:56.698 24009-24009/com.ein_siedler.putzplaner W/System.err:at android.view.View$DeclaredOnClickListener.onClick(View.java:4463) 12-20 20:33:56.698 24009-24009/com.ein_siedler.putzplaner W/System.err:at android.view.View.performClick(View.java:5233) 12-20 20:33:56.698 24009-24009/com.ein_siedler.putzplaner W/System.err:at android.view.View$PerformClick.run(View.java:21211) 12-20 20:33:56.698 24009-24009/com.ein_siedler.putzplaner W/System.err:at android.os.Handler.handleCallback(Handler.java:739) 12-20 20:33:56.698 24009-24009/com.ein_siedler.putzplaner W/System.err:at android.os.Handler.dispatchMessage(Handler.java:95) 12-20 20:33:56.699 24009-24009/com.ein_siedler.putzplaner W/System.err:at android.os.Looper.loop(Looper.java:148) 12-20 20:33:56.699 24009-24009/com.ein_siedler.putzplaner W/System.err:at android.app.ActivityThread.main(ActivityThread.java:5539) 12-20 20:33:56.699 24009-24009/com.ein_siedler.putzplaner W/System.err:at java.lang.reflect.Method.invoke(Native Method) 12-20 20:33:56.699 24009-24009/com.ein_siedler.putzplaner W/System.err:at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 12-20 20:33:56.699 24009-24009/com.ein_siedler.putzplaner W/System.err:at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 12-20 20:33:56.743 24009-24009/com.ein_siedler.putzplaner D/AppTracker: App Event: stop 12-20 20:33:56.795 24009-24009/com.ein_siedler.putzplaner D/AppTracker: App Event: start 12-20 20:33:56.872 24009-24009/com.ein_siedler.putzplaner D/AbstractTracker: Event success 12-20 20:33:56.878 24009-24009/com.ein_siedler.putzplaner D/AbstractTracker: Event success 12-20 20:33:56.955 24009-24146/com.ein_siedler.putzplaner D/OpenGLRenderer: endAllStagingAnimators on 0xb35ec380 (RippleDrawable) with handle 0x9f07c660

DatahHelper类:
class Datahelper extends SQLiteOpenHelper { //Strings für Datenbank public static final String DB_NAME = "task_list.db"; public static final int DB_VERSION = 1; public static final String TABLE_TASK_LIST = "task_list"; public static final String COLUMN_ID = "_id"; public static final String COLUMN_TYPE = "type"; public static final String COLUMN_NAME = "name"; public static final String COLUMN_EXPIRATION = "expiration"; public static final String COLUMN_OWNER = "owner"; public static final String SQL_CREATE = "CREATE TABLE " + TABLE_TASK_LIST + "(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_TYPE + " TEXT NOT NULL, " + COLUMN_NAME + " TEXT NOT NULL, " + COLUMN_EXPIRATION + " TEXT NOT NULL, " + COLUMN_OWNER + " TEXT NOT NULL); "; public static final String TABLE_USERS = "table_users"; public static final String COLUMN_ID_USERS = "_id"; public static final String COLUMN_NAME_USERS = "name"; public static final String COLUMN_PSSWD_USERS = "psswd"; public static final String COLUMN_SOAP_USERS = "soap"; public static final String COLUMN_EP_USERS = "ep"; public static final String SQL_CREATE_USERS = "CREATE TABLE " + TABLE_USERS + "(" + COLUMN_ID_USERS + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_NAME_USERS + " TEXT NOT NULL, " + COLUMN_PSSWD_USERS + " TEXT NOT NULL, " + COLUMN_SOAP_USERS + " INTEGER, " + COLUMN_EP_USERS + " INTEGER); "; private static final String LOG_TAG = Datahelper.class.getSimpleName(); public Datahelper(Context context) { //Konstruktor super(context, DB_NAME, null, DB_VERSION); Log.d(LOG_TAG, "Succesfully created database: " + getDatabaseName()); }//Erstellen einer neuen Datenbank @Override public void onCreate(SQLiteDatabase db) { try {Log.d(LOG_TAG, "creating table " + SQL_CREATE); db.execSQL(SQL_CREATE); db.execSQL(SQL_CREATE_USERS); } catch (Exception ex) { Log.d(LOG_TAG, "Error while creating table: " + ex.getMessage()); }}//?nderung an Struktur @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}

}
【Android SQLite用户登录不起作用】更改代码后的错误日志,但我不知道如何格式化
12-21 07:53:25.097 22705-22705/com.ein_siedler.putzplaner W/System.err:android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 0 12-21 07:53:25.098 22705-22705/com.ein_siedler.putzplaner W/System.err:at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460) 12-21 07:53:25.098 22705-22705/com.ein_siedler.putzplaner W/System.err:at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136) 12-21 07:53:25.098 22705-22705/com.ein_siedler.putzplaner W/System.err:at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:74) 12-21 07:53:25.099 22705-22705/com.ein_siedler.putzplaner W/System.err:at com.ein_siedler.putzplaner.Datasource.UserExists(Datasource.java:141) 12-21 07:53:25.099 22705-22705/com.ein_siedler.putzplaner W/System.err:at com.ein_siedler.putzplaner.activities.Anmeldung.anmelden(Anmeldung.java:47) 12-21 07:53:25.099 22705-22705/com.ein_siedler.putzplaner W/System.err:at java.lang.reflect.Method.invoke(Native Method) 12-21 07:53:25.099 22705-22705/com.ein_siedler.putzplaner W/System.err:at android.view.View$DeclaredOnClickListener.onClick(View.java:4463) 12-21 07:53:25.099 22705-22705/com.ein_siedler.putzplaner W/System.err:at android.view.View.performClick(View.java:5233) 12-21 07:53:25.099 22705-22705/com.ein_siedler.putzplaner W/System.err:at android.view.View$PerformClick.run(View.java:21211) 12-21 07:53:25.100 22705-22705/com.ein_siedler.putzplaner W/System.err:at android.os.Handler.handleCallback(Handler.java:739) 12-21 07:53:25.100 22705-22705/com.ein_siedler.putzplaner W/System.err:at android.os.Handler.dispatchMessage(Handler.java:95) 12-21 07:53:25.100 22705-22705/com.ein_siedler.putzplaner W/System.err:at android.os.Looper.loop(Looper.java:148) 12-21 07:53:25.100 22705-22705/com.ein_siedler.putzplaner W/System.err:at android.app.ActivityThread.main(ActivityThread.java:5539) 12-21 07:53:25.100 22705-22705/com.ein_siedler.putzplaner W/System.err:at java.lang.reflect.Method.invoke(Native Method) 12-21 07:53:25.100 22705-22705/com.ein_siedler.putzplaner W/System.err:at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 12-21 07:53:25.100 22705-22705/com.ein_siedler.putzplaner W/System.err:at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

答案我认为你的Datasource类中出现了问题,你的UserExists函数返回false; 你不能看到任何错误,因为你在每个try-catch的catch异常部分错过了e.printStackTrace(); 尝试将e.printStackTrace(); 设置为datasource类中的每个try-catch并重建app并运行它。在那里展示Catlog ......
更新:
试试这个功能:
public boolean UserExists(String name) { try { Cursor cursor = database.query(Datahelper.TABLE_USERS, null, Datahelper.COLUMN_NAME_USERS + " = '" + name + "'", null, null, null, null); String n = cursor.getString(cursor.getColumnIndex(Datahelper.COLUMN_NAME_USERS)); return true; } catch (Exception e) { e.printStackTrace(); } return false; }

如果在sql查询中使用字符串值,则检查两次它必须在''之间

    推荐阅读