SQLite中的rawQuery()出错

时间:2018-01-09 15:05:49

标签: android database android-sqlite sqliteopenhelper

public class DataBaseHelper extends SQLiteOpenHelper

 {
 public static final String DATABASE_NAME="register.db";
 public static final String TABLE_NAME="registration";
 public static final String COL_1="ID";
 public static final String COL_2="FirstName";
 public static final String COL_3="LastName";
 public static final String COL_4="Password";
 public static final String COL_5="Email";
 public static final String COL_6="Phone";

 public DataBaseHelper(Context context) {
    super(context,DATABASE_NAME,null,1);}

 @Override
 public void onCreate(SQLiteDatabase sqLiteDatabase) {
    sqLiteDatabase.execSQL("create table "+TABLE_NAME+"(ID INTEGER PRIMARY 
     KEY AUTOINCREMENT, FirstName TEXT,LastName TEXT,Password TEXT,Email 
     TEXT,Phone TEXT)");}

 @Override
 public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
    sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " +TABLE_NAME); //Drop 
    older table if exists
    onCreate(sqLiteDatabase);
}

 public int checkusercredentials(String email,String password){

    Log.i("INISDE","CHECK USER ");
    SQLiteDatabase db=this.getWritableDatabase();
    try{
       int i=0;
       Cursor c=null;
       c=db.rawQuery("select * from "+TABLE_NAME+"where "+ "Email=? and 
       Password=?",new String[]{email,password});
        c.moveToNext();
        i=c.getCount();
        c.close();
        return i;
    }catch(Exception e)
    {e.printStackTrace();}

    return 0;
}

在我的代码中,当用户单击SIGNIN按钮时,将调用checkusercredentials。

我面临的错误是c = db.rawQuery(“select * from”+ TABLE_NAME +“where”+“Email =?and        密码=?“,新字符串[] {电子邮件,密码});

我在Email后的'='符号中出错,错误显示为Expected复合运算符,join运算符,...,WHERE得到'='

Logcat读取     致命的例外:主要

Process: com.example.android.login, PID: 26858

android.database.sqlite.SQLiteException: near "=": syntax error (code 1): , 
while compiling: SELECT * FROM registrationWHERE Email=? AND Password=?

at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native 
Method)
                                                                                 at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)

at 
android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)

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(SQLiteDirectCursorDriver.java:44)

at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
                                                                           at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1255)
                                                                           at com.example.android.login.DataBaseHelper.checkusercredentials(DataBaseHelper.java:62)
                                                                           at com.example.android.login.MainActivity$1.onClick(MainActivity.java:43)
                                                                           at android.view.View.performClick(View.java:5207)
                                                                           at android.view.View$PerformClick.run(View.java:21177)
                                                                           at android.os.Handler.handleCallback(Handler.java:739)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                           at android.os.Looper.loop(Looper.java:148)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5438)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)

什么是错误?

2 个答案:

答案 0 :(得分:0)

问题是由于表名和WHERE关键字之间缺少空格,因此它认为表名是 registrationWHERE

因此您需要更改c=db.rawQuery("select * from "+TABLE_NAME+"where "+ "Email=? and Password=?",new String[]{email,password});

    c=db.rawQuery("select * from "+TABLE_NAME+" where "+ "Email=? and Password=?",new String[]{email,password});

如果您遇到或遇到no such column,请删除应用的数据并重新运行应用。这将导致调用onCreate方法并重新创建表。不了解onCreate方法仅在创建数据库时自动运行,这是列不存在的一个非常常见的原因,因为在onCreate方法中更改代码以添加列但该方法不运行

如果您仍然遇到问题,请编辑您的问题,并在日志中包含描述以及堆栈跟踪。

修改

似乎第二个错误是一个错误(在Android Studio 3.1中解决)as per - <expr> expected, got '?'

根据

,这有两个
    Cursor c; // No need for Cursor c = null; so just Cursor c;

    // Get-around 1 - pass where clause as a String.
    String whereclause = " WHERE Email=? AND Password=?"; // <<<<use string for the WHERE clause
    c = db.rawQuery("select * from "+
            TABLE_NAME+
            whereclause,//<<<< use string for the WHERE clause
        new String[]{email,password}
    );


    // Get-around 2 use query method as opposed to rawQuery method 
    c = db.query(TABLE_NAME,
        null,
        "Email=? AND Password=?",
        new String[]{email,password},
        null,null,null 
    );

答案 1 :(得分:0)

我这样做了......

String queryString = "SELECT * FROM " + DatabaseHelper.TABLE_NAME + " WHERE " //
                        + DatabaseHelper.COL_5 + "=? AND " + DatabaseHelper.COL_4 + "=?";
                cursor = db.rawQuery(queryString, new String[]{userName, passLogin});

现在工作正常