java - SQLite数据库无法找到字符串

标签 java android database sqlite

我正在 Android 设备上进行一些非常初级的数据库编程。在下面的代码中,我尝试使用一些过滤器查询数据库。如果我选择根据行的 ID 进行过滤,则它会正确返回,但如果我按不同的列进行过滤,则无法在数据库中找到任何条目。谁能发现我犯了什么样的错误?

*注意,我已在我询问的特定部分添加了评论。

import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DataBaseManager {

Context context;
private SQLiteDatabase db;
private final String DB_NAME = "database_name";
private final int DB_VERSION = 1;

private final String TABLE_NAME = "database_table";
private final String TABLE_COLUMN_ID = "id";
private final String TABLE_COLUMN_ONE = "user_name";
private final String TABLE_COLUMN_TWO = "pass_word";

public DataBaseManager(Context context) {
    this.context = context;
    CustomSQLiteOpenHelper helper = new CustomSQLiteOpenHelper(context);
    this.db = helper.getWritableDatabase();
}

private class CustomSQLiteOpenHelper extends SQLiteOpenHelper {

    public CustomSQLiteOpenHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    public void onCreate(SQLiteDatabase db) {
        String newTableQueryString =
            "create table " +
            TABLE_NAME +
            " (" +
            TABLE_COLUMN_ID + " integer primary key autoincrement not null," +
            TABLE_COLUMN_ONE + " text," +
            TABLE_COLUMN_TWO + " text" +
            ");";
        db.execSQL(newTableQueryString);
    }

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

}

public void addRow(String username, String password) {
    ContentValues values = new ContentValues();
    values.put(TABLE_COLUMN_ONE, username);
    values.put(TABLE_COLUMN_TWO, password);
    try {
        db.insert(TABLE_NAME, null, values);
    } catch (Exception e) {
        Log.e("DB ERROR", e.toString());
        e.printStackTrace();
    }
}

public void deleteRow(long rowID) {
    try {
        db.delete(TABLE_NAME, TABLE_COLUMN_ID + "=" + rowID, null);
    } catch (Exception e) {
        Log.e("DB ERROR", e.toString());
        e.printStackTrace();
    }
}

public void updateRow(long rowID, String username, String password) {
    ContentValues values = new ContentValues();
    values.put(TABLE_COLUMN_ONE, username);
    values.put(TABLE_COLUMN_TWO, password);
    try {
        db.update(TABLE_NAME, values, TABLE_COLUMN_ID + "=" + rowID, null);
    } catch (Exception e) {
        Log.e("DB Error", e.toString());
        e.printStackTrace();
    }
}

public ArrayList<Object> getRowAsArray(long rowID) {
    ArrayList<Object> rowArray = new ArrayList<Object>();
    Cursor cursor = null;
    try {
        cursor = db.query(
                TABLE_NAME,
                new String[] { TABLE_COLUMN_ID, TABLE_COLUMN_ONE, TABLE_COLUMN_TWO },
                TABLE_COLUMN_ID + "=" + rowID,
                null, null, null, null, null
        );
        cursor.moveToFirst();
        if (!cursor.isAfterLast()) {
            do {
                ArrayList<Object> dataList = new ArrayList<Object>();
                dataList.add(cursor.getLong(0));
                dataList.add(cursor.getString(1));
                dataList.add(cursor.getString(2));
                rowArray.add(dataList);
            } while(cursor.moveToNext());
        }
        cursor.close();
    } catch (Exception e) {
        Log.e("DB ERROR", e.toString());
        e.printStackTrace();

    }
    return rowArray;
}

//THIS IS THE METHOD I AM ASKING ABOUT
//
//
public ArrayList<ArrayList<Object>> getAllRowsAsArrays() {
    ArrayList<ArrayList<Object>> dataArrays = new ArrayList<ArrayList<Object>>();
    Cursor cursor;

    try {
        /* If I have TABLE_COLUMN_ID + "=" + 1, for example, the sql query will return
           the correct value, but this returns nothing, even though i am 100% sure
           that "teharris" is in the database
        */
        cursor = db.query(
                TABLE_NAME,
                new String[]{TABLE_COLUMN_ID, TABLE_COLUMN_ONE, TABLE_COLUMN_TWO},
                TABLE_COLUMN_ONE + "=" + "teharris", null, null, null, null
        );
        cursor.moveToFirst();
        if (!cursor.isAfterLast()) {
            do {
                ArrayList<Object> dataList = new ArrayList<Object>();

                dataList.add(cursor.getLong(0));
                dataList.add(cursor.getString(1));
                dataList.add(cursor.getString(2));

                dataArrays.add(dataList);
            } while (cursor.moveToNext());
        }
    }
    catch (Exception e) {
        Log.e("DB Error", e.toString());
        e.printStackTrace();
    }

    // return the ArrayList that holds the data collected from
    // the database.
    return dataArrays;
}

public boolean loginVerify(String username, String password) {
    Cursor cursor = null;
    boolean matches = false;
    try {
        cursor = db.query(
                TABLE_NAME,
                new String[] { TABLE_COLUMN_ID, TABLE_COLUMN_ONE, TABLE_COLUMN_TWO },
                TABLE_COLUMN_ONE + "=" + username, 
                null, null, null, null, null
        );
        cursor.moveToFirst();
        if (!cursor.isAfterLast()) {
            do {
                if (username.equals(cursor.getString(1).toString()) && 
                        password.equals(cursor.getString(2).toString())) {
                    matches = true;
                }
            } while(cursor.moveToNext() && !matches);
        }
        cursor.close();
    } catch (Exception e) {
        Log.e("DB ERROR", e.toString());
        e.printStackTrace();

    }
    return matches;
}

}

最佳答案

您需要引用String文字:

cursor = db.query(
            TABLE_NAME,
            new String[]{TABLE_COLUMN_ID, TABLE_COLUMN_ONE, TABLE_COLUMN_TWO},
            TABLE_COLUMN_ONE + "=" + "'teharris'", null, null, null, null
);

关于java - SQLite数据库无法找到字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21757018/

相关文章:

Java,文件上传后更新Jlist

java - 安卓 : AsyncTask onPostExecute is getting null reply

java - 按钮与鼠标输入 Canvas 重叠

android - 一个应用两套资源

php insert语句未在mysql中插入数据

sql - SQL Server 数据库中具有最大大小的 INT 类型

Java应用程序无法连接到openshift中的mysql数据库

java - 如何检查Spring Boot使用的是什么数据库池?

javascript - 我的 mailto 链接中的换行符在 Android 手机上损坏

Android 我的应用程序在 2.3 上运行良好,但即使在 4.0 上也无法启动