android.database.sqlite.SQLiteException:没有这样的列:COLUMN_USER_USERNAME

时间:2016-09-26 22:00:33

标签: android sqlite android-sqlite

我知道StackOverflow可能已经看到太多关于没有这样的专栏的问题 但就我搜索而言,我找到的是使用rawQuery()并使用单个字符串和命令 根据@ Code-Apprentice的要求,他自己建议我使用query()代替,我创造了我的。

下面的方法连接到一个表,以便用户检查登录名/密码,然后启动另一个活动 我的错误是"没有这样的专栏"。

在发布此消息之前,我花了很长时间寻找答案并尝试其他内容,但无济于事。
所以,如果它与另一个重复,我很抱歉。

public boolean search_access(user user){
    SQLiteDatabase db = this.getReadableDatabase();
    String [] tabela_user = new String[]{"usuaid","username","password"};
    String [] whereargs   = new String[]{user.getUsername()};
    String compare_user = "COLUMN_USER_USERNAME = ?";

    Cursor cursor = db.query(TABLE_USER, tabela_user, compare_user, whereargs, null, null, null );

    cursor.moveToFirst();
    if(cursor.getString(0).equals(user.getPassword())) {
        return true;
    } else{
        CharSequence text = "Incorrect Username/Password!!";
        int duration = Toast.LENGTH_SHORT;
        Toast toast = Toast.makeText(null, text, duration);
        toast.show();
        return false;
    }
}

错误:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.relatoriodeobras, PID: 2714
              android.database.sqlite.SQLiteException: no such column: COLUMN_USER_USERNAME (code 1): , while compiling: SELECT usuaid, username, password FROM usuario WHERE COLUMN_USER_USERNAME = ?
                  at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                  at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.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(SQLiteDirectCursorDriver.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.relatoriodeobras.projeto_db.busca_acesso(projeto_db.java:173) <- ERROR LINE HERE, which is Cursor's
                  at com.example.relatoriodeobras.MainActivity$1.onClick(MainActivity.java:42)
                  at android.view.View.performClick(View.java:4780)
                  at android.view.View$PerformClick.run(View.java:19866)
                  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:5254)
                  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:903)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

编辑:SQLiteOpenHelper类。刚刚添加了与用户管理相关的部分。

package com.example.relatoriodeobras;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public final class projeto_db extends SQLiteOpenHelper{

public static final String LOG = "DatabaseHelper";
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME =  "projeto_db";

public static final String TABLE_USER = "usuario";

public static final String COLUMN_USER_ENTRY_ID = "usuaid";
public static final String COLUMN_USER_USERNAME = "username";
public static final String COLUMN_USER_PASSWORD = "password";

private static final String CREATE_TABLE_USER = "CREATE TABLE "
        + TABLE_USER + "(" + COLUMN_USER_ENTRY_ID + " INTEGER PRIMARY KEY, " + COLUMN_USER_USERNAME
        + " TEXT, " + COLUMN_USER_PASSWORD + " TEXT, " + KEY_CREATED_AT + " DATETIME" + ")";

public projeto_db(Context context) {

    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db){
    db.execSQL(CREATE_TABLE_USER);
}

 @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // on upgrade drop older tables
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_USER);
    onCreate(db);
}

 public boolean registra_usuario(user user){    //Registra fiscais que utilizarão o sistema.registrado = true;
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    try{
        values.put(COLUMN_USER_USERNAME, add_usuario.username);
        values.put(COLUMN_USER_PASSWORD, add_usuario.password);
        values.put(KEY_CREATED_AT      , add_usuario.created_at);
        db.insert(TABLE_USER, null, values);
        db.close();
        return true;
    }catch (Exception e){
        db.close();
        return false;
    }
}

public boolean busca_acesso(user user){
    SQLiteDatabase db = this.getReadableDatabase();
    String [] tabela_user = new String[]{"usuaid","username","password"};
    String [] whereargs   = new String[]{user.getUsername()};
    String compare_user = "COLUMN_USER_USERNAME = ?";

    Cursor cursor = db.query(TABLE_USER, tabela_user, compare_user, whereargs, null, null, null );

    cursor.moveToFirst();
    if(cursor.getString(0).equals(user.getPassword())) {
        return true;
    } else{
        CharSequence text = "Incorrect Username/Password!!";
        int duration = Toast.LENGTH_SHORT;
        Toast toast = Toast.makeText(null, text, duration);
        toast.show();
        return false;
    }
}
public static String getDateTime() {
    SimpleDateFormat dateFormat = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss", Locale.getDefault());
    Date date = new Date();
    return dateFormat.format(date);
}
}}

用户类

package com.example.relatoriodeobras;

import android.database.sqlite.SQLiteDatabase;


public class user {
private static projeto_db dbHelper;
private static SQLiteDatabase database;

int id = 0;
private String username;
String password;
String created_at;
static boolean registrado = false;

public int getId() {

    return id;
}

public void setId() {
    this.id = id++;
}

public String getUsername() {

    return username;
}

public void setUsername(String username) {

    this.username = username;
}

public String getPassword() {

    return password;

}

public void setPassword(String password) {

    this.password = password;
}


public void setCreated_at(String created_at) {
    this.created_at = created_at;
}

public String getCreated_at() {

    return created_at;
}
}

我非常感谢代码组织方面的一些帮助,因为我猜你们可能会觉得它很乱。

1 个答案:

答案 0 :(得分:1)

你的错误不是谎言。您没有名为COLUMN_USER_USERNAME的列。

但是,你确实有一个带有该名称的变量

它的字符串值为"username"

选项1:

String compare_user = projeto_db.COLUMN_USER_USERNAME + " = ?";

选项2:

String compare_user = "username = ?";

除此之外:不要以明文形式存储密码