Android SQLite数据库查询的查询语法出错

Android SQLite数据库查询的查询语法出错,android,sqlite,android-intent,arraylist,android-sqlite,Android,Sqlite,Android Intent,Arraylist,Android Sqlite,查询语法有问题。我在MainActivity中有一个listview,并将位置传递给第二个activity,然后运行查询。我还有一个数据模型类、Promise和适配器类,当然还有数据库处理程序类。以下是主要活动: public class MainActivity extends Activity { ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCr

查询语法有问题。我在MainActivity中有一个listview,并将位置传递给第二个activity,然后运行查询。我还有一个数据模型类、Promise和适配器类,当然还有数据库处理程序类。以下是主要活动:

public class MainActivity extends Activity {

ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listView = (ListView) findViewById(R.id.listViewCategories);
    String[] categories = getResources().getStringArray(R.array.categories_array);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, categories);

    listView.setAdapter(adapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // ListView Clicked item index
            int itemPosition = position;
            // ListView Clicked item value
            String itemValue = (String) listView.getItemAtPosition(position);
            // Show Alert
            Toast.makeText(getApplicationContext(),
                    "Position: " + itemPosition + "  List Item: " + itemValue, 
                    Toast.LENGTH_LONG)
                    .show();
            Intent i = new Intent(MainActivity.this, DisplayResult.class);
            Bundle bundle = new Bundle();
            bundle.putString("itemValue", itemValue);
            i.putExtras(bundle);
            startActivity(i);
        }


    });
}
最后是数据模型(或对象)类:

非常感谢您的任何帮助! 谢谢大家!

使用
rawQuery()
进行如下简单的SQL查询

如果
selectedCategory
是一个字符串,请将其放入
的“单引号”
中,或者更好的是,
参数中

例如:

Cursor c = newDB.rawQuery("SELECT * FROM " + MyDBHandler.TABLE_NAME + " WHERE KEY_CATEGORY = ?", new String[] { selectedCategory });

嗯,错误标志消失了,但是我的应用程序没有运行。当我在MainActivity的ListView中单击一个项目时,它停止工作。不知道怎么了,从stacktrace开始。我在Logcat上找不到任何消息!是的,我没说我昨天找不到它。昨天和今天都上传了新的Android Studio。找到了,但什么也没印出来!
public class PromiseAdapter extends ArrayAdapter<Promise> {

public PromiseAdapter(Context context, ArrayList<Promise> verses) {
    super(context, 0, verses);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // get the data item for this position
    Promise promise = getItem(position);
    // check to see if an existing view is being reuses, otherwise inflate the view
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.verses, parent, false);
    }
    // lookup view for data population
    TextView txtCategory = (TextView) convertView.findViewById(R.id.txtViewCategory);
    TextView txtBook = (TextView) convertView.findViewById(R.id.txtViewBook);
    TextView txtChapter = (TextView)convertView.findViewById(R.id.txtViewChapter);
    TextView txtVerse = (TextView) convertView.findViewById(R.id.txtViewVerse);
    TextView txtWord = (TextView) convertView.findViewById(R.id.txtViewWord);
    // populate the data into the template view using the data object
    txtCategory.setText(promise._category);
    txtBook.setText(promise._book);
    txtChapter.setText(promise._chapter);
    txtVerse.setText(promise._verse);
    txtWord.setText(promise._word);
    // return the completed view to render on screen
    return convertView;
}
public class MyDBHandler extends SQLiteOpenHelper {

private static String DATABASE_PATH = 
     "/data/data/com.blogspot.joyouslybeingjoy.biblecyb/databases/";
private static final String DATABASE_NAME = "promisesdatabase.sqlite";
private static final int DATABASE_VERSION = 1;
public static final String TABLE_NAME = "promises";
public static final String KEY_ROWID = "_id";
public static final String KEY_CATEGORY = "category";
public static final String KEY_BOOK = "book";
public static final String KEY_CHAPTER = "chapter";
public static final String KEY_VERSE = "verse";
public static final String KEY_WORD = "word";

private static final String DATABASE_CREATE =
        "CREATE TABLE if not exists " + TABLE_NAME + " (" +
                KEY_ROWID + "integer PRIMARY KEY," +
                KEY_CATEGORY + " TEXT," +
                KEY_BOOK + " TEXT," +
                KEY_CHAPTER + " TEXT," +
                KEY_VERSE + " TEXT," +
                KEY_WORD + " TEXT" + ")";

/**
 * Constructor
 * Takes and keeps a reference of the passed context in order to access to the
 * application assets and resources.
 */
public MyDBHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    this.myContext = context;
    //createDatabase();
}

private SQLiteDatabase myDataBase;
private final Context myContext;

public void createDataBase() throws IOException {
    boolean dbExist = checkDataBase();
    if (dbExist) {
        //do nothing - database already exist
    } else {
        //By calling this method and empty database will be created into the default system 

        //of your application so we are gonna be able to overwrite that database with our 
        this.getReadableDatabase();

        try {
            copyDataBase();
        } catch (IOException e) {
            throw new Error("Error copying database");
        }
    }
}
/**

 *
 * @return true if it exists, false if it doesn't
 */
private boolean checkDataBase() {
    SQLiteDatabase checkDB = null;
    try {
        String myPath = DATABASE_PATH + DATABASE_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
    } catch (SQLiteException e) {
        //database doesn't exist yet.
    }
    if (checkDB != null) {
        checkDB.close();
    }
    return checkDB != null ? true : false;
}
/**
 * Copies your database from your local assets-folder to the just created empty database in 
 * system folder, from where it can be accessed and handled.
 * This is done by transferring bytestream.
 */
private void copyDataBase() throws IOException {
    //Open your local db as the input stream
    InputStream myInput = myContext.getAssets().open(DATABASE_NAME);
    // Path to the just created empty db
    String outFileName = DATABASE_PATH + DATABASE_NAME;
    //Open the empty db as the output stream
    OutputStream myOutput = new FileOutputStream(outFileName);
    //transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer)) > 0) {
        myOutput.write(buffer, 0, length);
    }
    //Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();
}
public void openDataBase() throws SQLException {
    //Open the database
    String myPath = DATABASE_PATH + DATABASE_NAME;
    myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close() {
    if (myDataBase != null)
        myDataBase.close();
    super.close();
}
public void onCreate(SQLiteDatabase db) {
    //Log.w(LOG_TAG, DATABASE_CREATE);
    db.execSQL(DATABASE_CREATE);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    //Log.w(LOG_TAG, "Upgrading database from " + oldVersion + " to " + newVersion
    // + ", which will destroy all old data (user pref, features");
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(db);
}
public class Promise {
//private variables
int _id;
String _category;
String _book;
String _chapter;
String _verse;
String _word;

// empty constructor
public Promise() {
}
// constructor (input)
public Promise(int rowid, String category, String book, String chapter, String verse,
               String word) {
    this._id = rowid;
    this._category = category;
    this._book = book;
    this._chapter = chapter;
    this._verse = verse;
    this._word = word;
}
// constructor (output)
public Promise(String category, String book, String chapter, String verse, String word) {
    this._category = category;
    this._book = book;
    this._chapter = chapter;
    this._verse = verse;
    this._word = word;
}
// getters and setters
public int getRowID() { return this._id; }
public  void setRowID(int rowID) { this._id = rowID; }

public String getCategory() {
    return this._category;
}
public void setCategory(String category) {
    this._category = category;
}

public String getBook() {
    return this._book;
}
public void setBook(String book) {
    this._book = book;
}

public String getChapter() {
    return this._chapter;
}
public void setChapter(String chapter) {
    this._chapter = chapter;
}

public String getVerse() {
    return this._verse;
}
public void setVerse(String verse) {
    this._verse = verse;
}

public String getWord() {
    return this._word;
}
public void setWord(String word) {
    this._word = word;
}

/* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
// will be used by the ArrayAdapter in the ListView
@Override
public String toString() {
    return "Promise [category=" + _category + ", book=" + _book + ", chapter=" + _chapter
            + ", verse=" + _verse + ", word=" + _word + "]";
}
Cursor c = newDB.query("SELECT * FROM " + MyDBHandler.TABLE_NAME + " WHERE KEY_CATEGORY = " + selectedCategory);
Cursor c = newDB.rawQuery("SELECT * FROM " + MyDBHandler.TABLE_NAME + " WHERE KEY_CATEGORY = ?", new String[] { selectedCategory });