1

I have a syntax error in the SQL I'm generating below and I think it's in the getContactMatches() method:

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 = 4;
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;
        }

The log output shows:

12-28 12:39:13.254: E/SQLiteLog(565): (1) statement aborts at 7: [SELECT * FROM contactsTable WHERE (NAME MATCH ?)] unable to use function MATCH in the requested context
12-28 12:39:13.284: E/SQLiteQuery(565): exception: unable to use function MATCH in the requested context (code 1); query: SELECT * FROM contactsTable WHERE (NAME MATCH ?)
4

1 に答える 1

11

MATCH仮想 FTS テーブルに対してのみ有効です。LIKE通常のテーブルで使用する必要があります。

于 2012-12-28T18:59:51.727 に答える