在方法getContactMatches()中,sql语句出错 字符串选择= COL_NAME +“MATCH?”;我不太熟悉sql所以我不知道该怎么做
数据库类和getContactMatches()方法:
public class DatabaseTable {
private static final String TAG = "ContactsDatabase";
//The columns we'll include in the contacts table
public static final String COL_ID = "_id";
public static final String COL_NAME = "NAME";
public static final String COL_EMAIL = "EMAIL";
private static final String DATABASE_NAME = "CONTACT";
private static final String DATABASE_TABLE = "contactsTable";
private static final int DATABASE_VERSION = 2;
private Context ourContext;
private DbHelper DBHelper;
private SQLiteDatabase db;
private static final String DATABASE_CREATE =
"CREATE TABLE " + DATABASE_TABLE + " (" +
COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL_NAME + " TEXT NOT NULL, " +
COL_EMAIL + " TEXT NOT NULL);";
private static class DbHelper extends SQLiteOpenHelper {
DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(DATABASE_CREATE);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
public DatabaseTable(Context context) {
ourContext = context;
}
public DatabaseTable open() throws SQLException {
DBHelper = new DbHelper(ourContext);
db = DBHelper.getWritableDatabase();
return this;
}
public void close() {
DBHelper.close();
}
public long insertContact(String name, String email) {
ContentValues initialValues = new ContentValues();
initialValues.put(COL_NAME, name);
initialValues.put(COL_EMAIL, email);
return db.insert(DATABASE_TABLE, null, initialValues);
}
public String getData()
{
String[] columns = new String[]{COL_ID, COL_NAME, COL_EMAIL};
Cursor c = db.query(DATABASE_TABLE, columns, null, null, null, null, null);
String data = "";
int id = c.getColumnIndex(COL_ID);
int colname = c.getColumnIndex(COL_NAME);
int colemail = c.getColumnIndex(COL_EMAIL);
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
data = data + c.getString(id) + " " + c.getString(colname) + " " + c.getString(colemail) + "\n";
}
return data;
}
public Cursor getContactMatches(String query, String[] columns) {
String selection = COL_NAME + " MATCH ?";
String[] selectionArgs = new String[] {query+"*"};
return query(selection, selectionArgs, columns);
}
private Cursor query(String selection, String[] selectionArgs, String[] columns) {
DBHelper = new DbHelper(ourContext);
SQLiteQueryBuilder build = new SQLiteQueryBuilder();
build.setTables(DATABASE_TABLE);
Cursor cursor = build.query(DBHelper.getReadableDatabase(),
columns, selection, selectionArgs, null, null, null);
if (cursor == null) {
return null;
}
return cursor;
}
public int cursorLength(Cursor cursor) {
int x = 0;
for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext())
x = x + 1;
return x;
}