应用由于语法错误而不断崩溃

时间:2020-06-11 19:56:22

标签: java android database sqlite android-sqlite

请帮助我解决这个问题。因此,我的应用程序不断崩溃。我想做的是在用户登录到个人资料活动的文本视图后,显示SQLite数据库中用户的信息。请帮我完成我的项目。我还是Android Studio的新手。

这是我的logcat中的语法错误

android.database.sqlite.SQLiteException: near "null": syntax error (code 1 SQLITE_ERROR): , while compiling: Select * from USER_TABLE where null=?null =?null =?

这是我的数据库助手

public class DatabaseHelper extends SQLiteOpenHelper {


    public static String C_EMAIL,C_PREFERENCES,C_PASSWORD;;



    public DatabaseHelper(@Nullable Context context) {
        super(context, constants.DB_NAME, null, constants.DB_VERSION);
    }


    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL(constants.CREATE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + constants.TABLE_NAME);
        onCreate(db);
    }

    public boolean insertInfo(String email, String preference,String password) {

        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(constants.C_EMAIL, email);
        values.put(constants.C_PREFERENCES, preference);
        values.put(constants.C_PASSWORD , password);

        long id = db.insert(constants.TABLE_NAME, null, values);
        if (id == -1) {
            return false;
        }else {
            return true;
        }
    }

    public boolean userExists (String email){
        String [] columns = {C_EMAIL};
        SQLiteDatabase db = getReadableDatabase();
        String selection = C_EMAIL + "=?";
        String selectionArgs []= { email };
        Cursor cursor = db.query(TABLE_NAME,columns,selection,selectionArgs,null,null,null);
        int count = cursor.getCount();
        cursor.close();
        db.close();
        if (count > 0)
            return true;
        else
            return false;
    }

  public Cursor getData(String email, String Password){
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor res = db.rawQuery(" Select * from "+ TABLE_NAME + " where "+ C_EMAIL + "=?" + C_PASSWORD + " =?", new String[]{email,Password});
        return res;
    }
}

这是我的login.java

loginacc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Cursor rs = dbHelper.getData(loginemail.getText().toString(),loginpassword.getText().toString());
                if(rs.moveToFirst()){
                    String email = rs.getString(rs.getColumnIndex(DatabaseHelper.C_EMAIL));
                    String preference = rs.getString(rs.getColumnIndex(DatabaseHelper.C_PREFERENCES));
                    String password = rs.getString(rs.getColumnIndex(DatabaseHelper.C_PASSWORD));


                    Toast.makeText(login.this,"Login Successful", Toast.LENGTH_SHORT).show();

                    Intent intent = new Intent(login.this,profile.class);
                    intent.putExtra("email", email);
                    intent.putExtra("preference", preference);
                    intent.putExtra("password", password);
                    startActivity(intent);

                    if(rs != null && rs.isClosed()){
                        rs.close();
                    }
                }
                else{
                    Toast.makeText(login.this,"Invalid Login", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

这是我的个人资料。java

public class profile extends AppCompatActivity {
    DatabaseHelper dbHelper;
    TextView pemail,ppreferences, ppassword;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);
        dbHelper = new DatabaseHelper(this);

        pemail = findViewById(R.id.pemail);

        ppreferences = findViewById(R.id.ppreferences);
        ppassword = findViewById(R.id.ppassword);


        pemail.setText(getIntent().getStringExtra("email"));
        ppreferences.setText(getIntent().getStringExtra("preferences"));
        ppassword.setText((getIntent().getStringExtra("password")));

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

您在此处声明3个静态变量:

public static String C_EMAIL,C_PREFERENCES,C_PASSWORD;

没有为其分配任何值,因此它们都是null
getData()中使用它们时:

" Select * from "+ TABLE_NAME + " where "+ C_EMAIL + "=?" + C_PASSWORD + " =?"

结果是:

Select * from USER_TABLE where null=?null =?null =?

(您的代码不清楚3d null =?的来源)
您想要的是(我想)将这些变量用作表的列名。
因此,将声明更改为:

public static String C_EMAIL = "email"; // change to the actual column name
public static String C_PASSWORD = "password"; // change to the actual column name
public static String C_PREFERENCES = "preferences"; // change to the actual column name

还要在sql语句中添加AND运算符:

"Select * from "+ TABLE_NAME + " where "+ C_EMAIL + "= ? AND " + C_PASSWORD + " = ?"

编辑
在方法insertInfo()中,您使用constants.C_EMAILconstants.C_PREFERENCESconstants.C_PASSWORD,它们似乎是您列的名称。
如果是这样,则还要在sql语句中使用它们,并删除静态变量:

"Select * from "+ TABLE_NAME + " where "+ constants.C_EMAIL + "= ? AND " + constants.C_PASSWORD + " = ?"