Android应用程序崩溃与Cursor db.rawQuery()调用?

时间:2017-12-17 00:09:29

标签: android sqlite

所以我的应用程序非常基本,它是一个客户应用程序,它将值输入到存储名称,数字和地址所有字符串的SQLite数据库中。现在我的插入函数似乎工作得很好,但我的应用程序崩溃了,当我试图在我试图返回字符串的函数中调用rawQuery时。

    public String returnString(String name) {

    String temp = "";

    String query = "SELECT * FROM " + TABLE_CUSTOMERS + " WHERE " + COLUMN_NAME + " = ?";
    String[] value = {name};

    SQLiteDatabase db = this.getReadableDatabase();

    if(db.isOpen()) {
        Cursor c = db.rawQuery(query, value);

        c.moveToFirst();

        while (c.moveToNext()) {

            if (c.getString(c.getColumnIndex("name")) != null && c.getString(c.getColumnIndex("name")).equals(name)) {

                temp = c.getString(c.getColumnIndex("name"));
                break;
            }// if

        } // while

        c.close();
        db.close();
    } // if
    else{

        Log.i("Failure -> ", " DataBase failed to open.");
        temp = "ERROR";

    }
    return temp;
}

基本上你发送名称,它应该返回整列。但我无法弄清楚为什么我的应用程序崩溃在以下行......

Cursor c = db.rawQuery(query, value);

仅供参考,这是我的onCreate和onUpgrade的插入功能...

    public class DBHandler extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1; // version
private static final String DATABASE_NAME = "customers.db"; // stored file name
public static final String TABLE_CUSTOMERS = "customers"; // name of table

public static final String COLUMN_ID = "_id";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_ADDRESS = "address";
public static final String COLUMN_EMAIL = "email";
public static final String COLUMN_PHONE = "phone";

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

// first time creation of db
@Override
public void onCreate(SQLiteDatabase db) {

    String query = "CREATE TABLE " + TABLE_CUSTOMERS + "(" +
            COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " +
            COLUMN_NAME + " TEXT NOT NULL, " +
            COLUMN_ADDRESS + " TEXT NOT NULL, " +
            COLUMN_EMAIL + " TEXT NOT NULL, " +
            COLUMN_PHONE + " TEXT NOT NULL);";

    db.execSQL(query);

}

// on upgrade of db
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    db.execSQL("DROP TABLE IF EXISTS " + TABLE_CUSTOMERS);

    onCreate(db);

}

// add customer by custom container
public void addCustomer(StudentsDB customer) {

    SQLiteDatabase db = getWritableDatabase();

    ContentValues values = new ContentValues();

    values.put(COLUMN_NAME, customer.get_name());
    values.put(COLUMN_ADDRESS, customer.get_address());
    values.put(COLUMN_EMAIL, customer.get_email());
    values.put(COLUMN_PHONE, customer.get_phone());

    db.insert(TABLE_CUSTOMERS, null, values);

    db.close();

} // addCustomer

// delete all of table
public void dropTable() {

    SQLiteDatabase db = getWritableDatabase();
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_CUSTOMERS);

    db.close();

}

// return string with string
public String returnString(String name) {

    String temp = "";

    String query = "SELECT * FROM " + TABLE_CUSTOMERS + " WHERE " + COLUMN_NAME + " = ?";
    String[] value = {name};

    SQLiteDatabase db = getReadableDatabase();

    if (db.isOpen()) {

        Cursor c = db.rawQuery(query, value);

        c.moveToFirst();

        while (c.moveToNext()) {

            if (c.getString(c.getColumnIndex("name")) != null && c.getString(c.getColumnIndex("name")).equals(name)) {

                temp = c.getString(c.getColumnIndex("name"));
                break;
            }// if

        } // while

        c.close();
        db.close();
    } // if
    else {

        Log.i("Failure -> ", " DataBase failed to open.");
        temp = "ERROR";

    }
    return temp;
}

1 个答案:

答案 0 :(得分:-2)

在创建查询")";

中更改您的表格查询尾随分号
String query = "CREATE TABLE " + TABLE_CUSTOMERS + "(" +
                COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " +
                COLUMN_NAME + " TEXT NOT NULL, " +
                COLUMN_ADDRESS + " TEXT NOT NULL, " +
                COLUMN_EMAIL + " TEXT NOT NULL, " +
                COLUMN_PHONE + " TEXT NOT NULL + ")";