android: error on SQLiteDatabase rawQuery (fetching data)
So I have a SQliteDatabase mDb. It only has one column, and its data are Strings for previously saved inputs. I'm trying to populate all the data from mDb into a String[] for AutoCompleteTextView (so that the autocomplete is based on previous inputs), and here's my code to get all of the String.
public String[] fetchAllSearch() {
ArrayList<String> allSearch = new ArrayList<String>();
Cursor c = mDb.rawQuery("select * from " + DATABASE_TABLE, null);
c.moveToFirst();
if (c.getCount() > 0) {
do {
allSearch.add(c.getString(c.getColumnIndex("KEY")));
} while (c.moveToNext());
}
String[] foo = (String[]) allSearch.toArray();
if (foo == null) {
foo = new String[] {""};
}
return foo;
}
my CREATE_TABLE command is
private static final String DATABASE_CREATE = "create table " + DATABASE_TABLE;
..
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}
But for some reason the line mDb.rawQuery(...) is giving me "no such table found" exception, and for t开发者_开发技巧he life of me I can't figure out why. Any pointers?
What is the value of DATABASE_TABLE
?
If it is just a table name, then the create statement is incomplete because it doesn't specify the columns.
If it is a name plus column definitions, then the select will not work.
So, you need to use different text in the two places you used DATABASE_TABLE
Try using the SQLite3 command line program to try out your SQL. E.g.,
sqlite> create table foo;
Error: near ";": syntax error
sqlite> create table foo(col);
sqlite> select * from foo(col);
Error: near "(": syntax error
sqlite>
Use SELECT column_name FROM table_name
.
Avoid using *
in the query as it might cause the problem related to primary key.
精彩评论