获取SQLiteException:没有这样的列

时间:2016-07-25 16:26:36

标签: java android sqlite

public class MyDBHandler extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "winkelskortrijk.db";
    private static final String TABLE_USERS = "users";
    private static final String TABLE_FAVOURITES = "favorieten";

    public static final String COLUMN_ID = "id";
    public static final String COLUMN_USERNAME = "username";
    public static final String COLUMN_PASSWORD = "password";

    public static final String COLUMN_ID2 = "id2";
    public static final String COLUMN_NAME = "naam";
    public static final String COLUMN_STRAAT = "straat";
    public static final String COLUMN_HUISNR = "nr";
    public static final String COLUMN_POSTCODE = "postcode";
    public static final String COLUMN_DEELGEMEENTE = "deelgemeente";
    public static final String COLUMN_GEMEENTE = "gemeente";
    public static final String COLUMN_ADRES = "adres";

    public MyDBHandler(Context context, String name,
                       SQLiteDatabase.CursorFactory factory, int version) {
        super(context, DATABASE_NAME, factory, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        String CREATE_USERS_TABLE = "CREATE TABLE " +
                TABLE_USERS + "("
                + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                + COLUMN_USERNAME + " TEXT,"
                + COLUMN_PASSWORD + " TEXT"
                + ")";
        db.execSQL(CREATE_USERS_TABLE);

        String CREATE_FAVOURITES_TABLE = "CREATE TABLE " +
                TABLE_FAVOURITES + "("
                + COLUMN_ID2 + " INTEGER PRIMARY KEY,"
                + COLUMN_NAME + " TEXT,"
                + COLUMN_STRAAT + " TEXT,"
                + COLUMN_HUISNR + " INTEGER,"
                + COLUMN_POSTCODE + " INTEGER,"
                + COLUMN_DEELGEMEENTE + " TEXT,"
                + COLUMN_GEMEENTE + " TEXT,"
                + COLUMN_ADRES + " TEXT,"
                + COLUMN_ID + " INT, "
                + "FOREIGN KEY(" + COLUMN_ID + ") REFERENCES "
                + TABLE_USERS + "(id) " + ")";
        db.execSQL(CREATE_FAVOURITES_TABLE);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion,
                          int newVersion) {

    }


    public User findUser(User user) {

        String selectQuery = "SELECT  * FROM " + TABLE_USERS + " WHERE "
                + COLUMN_USERNAME + " = " + user.getUsername();

        SQLiteDatabase db = this.getWritableDatabase();

        Cursor cursor = db.rawQuery(selectQuery, null);

        User findUser = new User();

        if (cursor.moveToFirst()) {
            cursor.moveToFirst();
            findUser.setUserName(cursor.getString(1));
            findUser.setPassword(cursor.getString(2));
            cursor.close();
        } else {
            findUser = null;
        }
        db.close();
        return findUser;

    }

}

当我尝试运行findUser(User user)方法时,我得到一个SQLException没有这样的列:Gebruikersnaam(注意:Gebruikersnaam是user.getUserName()中的变量,我希望此函数返回用户,如果它是发现或在没有的时候为空。

我做错了什么,是否有更好的方法来使用查询?

崩溃日志:

  

07-25 15:22:42.345   32264-32264 / com.example.hoofdgebruiker.winkelskortrijk   E / AndroidRuntime:致命异常:主要                                                                                                   过程:com.example.hoofdgebruiker.winkelskortrijk,PID:32264                                                                                                   android.database.sqlite.SQLiteException:没有这样的列:   Gebruikersnaam(代码1):,同时编译:SELECT * FROM users WHERE   username = Gebruikersnaam                                                                                                       在   android.database.sqlite.SQLiteConnection.nativePrepareStatement(母语   方法)                                                                                                       在   android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)                                                                                                       在   android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)                                                                                                       在   android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)                                                                                                       在android.database.sqlite.SQLiteProgram。(SQLiteProgram.java:58)                                                                                                       在android.database.sqlite.SQLiteQuery。(SQLiteQuery.java:37)                                                                                                       在   android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)                                                                                                       在   android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)                                                                                                       在   android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1255)                                                                                                       在   com.example.hoofdgebruiker.winkelskortrijk.Database.MyDBHandler.findUser(MyDBHandler.java:78)                                                                                                       在   com.example.hoofdgebruiker.winkelskortrijk.Homepage.MainActivity.register(MainActivity.java:49)                                                                                                       在   com.example.hoofdgebruiker.winkelskortrijk.Homepage.Register $ 1.onClick(Register.java:47)                                                                                                       在android.view.View.performClick(View.java:5198)                                                                                                       在android.view.View $ PerformClick.run(View.java:21147)                                                                                                       在android.os.Handler.handleCallback(Handler.java:739)                                                                                                       在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                                       在android.os.Looper.loop(Looper.java:148)                                                                                                       在android.app.ActivityThread.main(ActivityThread.java:5417)                                                                                                       at java.lang.reflect.Method.invoke(Native Method)                                                                                                       在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)                                                                                                       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

2 个答案:

答案 0 :(得分:1)

只需更新查询

String selectQuery = "SELECT  * FROM " + TABLE_USERS + " WHERE "
            + COLUMN_USERNAME + " = " + user.getUsername();

String selectQuery = "SELECT  * FROM " + TABLE_USERS + " WHERE "
            + COLUMN_USERNAME + " = '" + user.getUsername()+"'";

将user.getUsername()值括在单引号中,因为您将COLUMN_USERNAME定义为文本。 如果您的字符串已包含引号,则它不起作用。在添加user.getUsername()以查询从用户名中删除引号之前,如下所示

  

user.getUsername()替换( “\”“, ” ''“);

答案 1 :(得分:-1)

你的问题在这里:

String selectQuery = "SELECT  * FROM " + TABLE_USERS + " WHERE "
        + COLUMN_USERNAME + " = " + user.getUsername();

由于user.getUsername()是文字,您必须执行以下操作:

String selectQuery = "SELECT  * FROM " + TABLE_USERS + " WHERE "
        + COLUMN_USERNAME + " = '" + user.getUsername() + "'";

干杯