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

问题描述 投票:-1回答:1

所以我的应用程序非常基本,它是一个客户应用程序,它将值输入到存储名称,数字和地址所有字符串的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;
}
android sqlite
1个回答
-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 + ")";
© www.soinside.com 2019 - 2024. All rights reserved.