按下按钮时Android应用开发失败

时间:2019-04-25 14:23:26

标签: android database sqlite

我对android开发非常陌生,对应用程序开发的知识为零。我想寻求指导并帮助您在使用sqlite进行YouTube帐户注册和登录的示例中进行问题排查。我已经仔细地遵循了该示例,但是当按下按钮时该程序最终崩溃了。该示例可以在
找到 https://www.youtube.com/watch?v=mVIZFV0Ttds

我正在使用android studio 3.1.3。在我的build.gradle上,我将compileSdkVersion 28更改为25,将targetSdkVersion更改为25,并与youtube中的示例匹配以减少构建错误。

以下是我认为可能是该应用程序的databasehelper.java中错误原因的代码

package com.example.weh.testsql.sql;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.example.weh.testsql.model.User;

public class DatabaseHelper extends SQLiteOpenHelper{

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "UserManager.db";
    private static final String TABLE_USER = "user";

    private static final String COLUMN_USER_ID = "user_id";
    private static final String COLUMN_USER_NAME = "user_name";
    private static final String COLUMN_USER_EMAIL = "user_email";
    private static final String COLUMN_USER_PASSWORD = "user_password";

    private String CREATE_USER_TABLE = "CREATE TABLE " + TABLE_USER + "("
            + COLUMN_USER_ID +" INTERGER PRIMARY KEY AUTOINCREMENT," +
            COLUMN_USER_NAME + " TEXT,"+ COLUMN_USER_EMAIL + " TEXT," + 
COLUMN_USER_PASSWORD +
            " TEXT" + ")";
    private String DROP_USER_TABLE = "DROP TABLE IF EXISTS " + TABLE_USER;

    public DatabaseHelper(Context context){
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db){
        db.execSQL(CREATE_USER_TABLE);
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
        db.execSQL(DROP_USER_TABLE);
        onCreate(db);
    }

    public void addUser(User user){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_USER_NAME, user.getName());
        values.put(COLUMN_USER_EMAIL, user.getEmail());
        values.put(COLUMN_USER_PASSWORD, user.getPassword());

        db.insert(TABLE_USER, null, values);
        db.close();
    }

    public boolean checkUser(String email){
        String[] columns ={
                COLUMN_USER_ID,
        };
        SQLiteDatabase db = this.getWritableDatabase();
        String selection = COLUMN_USER_EMAIL + " = ?";
        String[] selectionArgs = {email};

        Cursor cursor = db.query(TABLE_USER,
                columns,
                selection,
                selectionArgs,
                null,
                null,
                null);
        int cursorCount = cursor.getCount();
        cursor.close();
        db.close();
        if(cursorCount > 0){
            return true;
        }
        return false;
    }

    public boolean checkUser(String email, String password){
        String[] columns ={
                COLUMN_USER_EMAIL,
                COLUMN_USER_PASSWORD
        };
        SQLiteDatabase db = this.getWritableDatabase();
        String selection = COLUMN_USER_EMAIL + " =?" + " AND " +     COLUMN_USER_PASSWORD +" =?";
        String[] selectionArgs = {email, password};

        Cursor cursor = db.query(TABLE_USER,
                columns,
                selection,
                selectionArgs,
                null,
                null,
                null);
        int cursorCount = cursor.getCount();
        cursor.close();
        db.close();
        if(cursorCount > 0){
            return true;
        }
        return false;
    }
}

运行此应用程序时,错误显示在我的logcat上

E/SQLiteLog: (1) no such column: user_email
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.weh.testsql, PID: 5240
                  android.database.sqlite.SQLiteException: no such column:     user_email (code 1): , while compiling: SELECT user_id FROM user WHERE     user_email =?
                      at     android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                      at     android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnec    tion.java:889)
                      at     android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
                      at     android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                      at android.database.sqlite.SQLiteProgram.<init>    (SQLiteProgram.java:58)
                      at android.database.sqlite.SQLiteQuery.<init>    (SQLiteQuery.java:37)
                      at     android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDrive    r.java:44)
                      at     android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java    :1316)
                      at     android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1163)
                  at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1034)
                  at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1202)
                  at com.example.weich.testsql.sql.DatabaseHelper.checkUser(DatabaseHelper.java:61)
                  at com.example.weich.testsql.activities.RegisterActivity.postDataToSQLite(RegisterActivity.java:111)
                  at com.example.weich.testsql.activities.RegisterActivity.onClick(RegisterActivity.java:87)
                  at android.view.View.performClick(View.java:4756)
                  at android.view.View$PerformClick.run(View.java:19749)
                  at android.os.Handler.handleCallback(Handler.java:739)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:135)
                  at android.app.ActivityThread.main(ActivityThread.java:5221)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:372)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

0 个答案:

没有答案