0

みんな、私はこのコードに非常に長い間取り組んできましたが、何もできません。ところで、列は存在しますが、エラーを返しています。sqlitebrowser でデータベースを作成しました。

エラーを返すメソッドは次のとおりです。

Cursor getWordMatches(String query) {
    myDataBase = khol();
    Cursor cur = myDataBase.rawQuery("SELECT " + colid + "," +col2+" ,"
            + col3 + " from " + deptTable + " WHERE " + col2 + " LIKE '"
            + query + "%'", new String[] {});


    myDataBase.close();
    return cur;
}

ここで cur="column2" と私は SuggestionProvider クラスでこの関数を呼び出しています

SuggestionProvider クラス

public class SuggestionProvider extends ContentProvider {
String TAG = "SuggestionProvider";

public static String AUTHORITY = "example.search.custom.SuggestionProvider";
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
        + "/Curedata");

// MIME types used for searching words or looking up a single definition
public static final String DISEASE_MIME_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE
        + "/vnd.example.search.custom.Curedata";
public static final String DESCRIPTION_MIME_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE
        + "/vnd.example.search.custom.Curedata";

private DatabaseHelper2 mHelper;

// UriMatcher stuff
private static final int SEARCH_DISEASE = 0;
private static final int GET_DISEASE = 1;
private static final int SEARCH_SUGGEST = 2;
private static final UriMatcher sURIMatcher = buildUriMatcher();

@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
    // TODO Auto-generated method stub
    throw new UnsupportedOperationException();

}

private static UriMatcher buildUriMatcher() {
    // TODO Auto-generated method stub
    UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
    // to get definitions...
    matcher.addURI(AUTHORITY, "Curedata", SEARCH_DISEASE);
    matcher.addURI(AUTHORITY, "Curedata/#", GET_DISEASE);
    // to get suggestions...
    matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY,
            SEARCH_SUGGEST);
    matcher.addURI(AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY + "/*",
            SEARCH_SUGGEST);

    return matcher;
}

@Override
public String getType(Uri uri) {
    // TODO Auto-generated method stub
    switch (sURIMatcher.match(uri)) {
    case SEARCH_DISEASE:
        return DISEASE_MIME_TYPE;
    case GET_DISEASE:
        return DESCRIPTION_MIME_TYPE;
    case SEARCH_SUGGEST:
        return SearchManager.SUGGEST_MIME_TYPE;

    default:
        throw new IllegalArgumentException("Unknown URL " + uri);
    }
}

@Override
public Uri insert(Uri uri, ContentValues values) {
    // TODO Auto-generated method stub
    throw new UnsupportedOperationException();
}

@Override
public boolean onCreate() {
    mHelper = new DatabaseHelper2(getContext());
    return true;
}

@Override
public Cursor query(Uri uri, String[] projection, String selection,
        String[] selectionArgs, String sortOrder) {
    // TODO Auto-generated method stub
    switch (sURIMatcher.match(uri)) {
    case SEARCH_SUGGEST:
        if (selectionArgs == null) {
            throw new IllegalArgumentException(
                    "selectionArgs must be provided for the Uri: " + uri);
        }

        return mHelper.getWordMatches(selectionArgs[0]);
    case SEARCH_DISEASE:
        if (selectionArgs == null) {
            throw new IllegalArgumentException(
                    "selectionArgs must be provided for the Uri: " + uri);
        }
        return mHelper.getWordMatches(selectionArgs[0]);

    case GET_DISEASE:
        return getWord(uri);

    default:
        throw new IllegalArgumentException("Unknown Uri: " + uri);
    }
}


private Cursor getWord(Uri uri) {
    // TODO Auto-generated method stub
    String disease = uri.getLastPathSegment();
    mHelper.getstring(disease);
    return mHelper.getdisease();

}

@Override
public int update(Uri uri, ContentValues values, String selection,
        String[] selectionArgs) {
    // TODO Auto-generated method stub
    throw new UnsupportedOperationException();
}

しかし、LogCat は例外を与えています

android.database.sqlite.SQLiteException->no such column:column2

手伝ってください。ハマった!

編集:
データベース作成コード

public class DatabaseHelper2 extends SQLiteOpenHelper {


private static String DB_PATH = "/data/data/example.SQLitetest/";

private static String DB_NAME = "desicures.db";

    private SQLiteDatabase myDataBase, mydb1;

private final Context myContext;

private String colname = "Area";

private String deptTable = "Curedata";

public static String col2 = "column2";

public static String col3 = "Description";


public static String colid = "_ID";
private String s;
private String y = "Y";


public DatabaseHelper2(Context context) {

    super(context, DB_NAME, null, 1);
    this.myContext = context;
}



public SQLiteDatabase khol() throws SQLException {

    // Open the database
    String myPath = DB_PATH + DB_NAME;
    myDataBase = SQLiteDatabase.openDatabase(myPath, null,
            SQLiteDatabase.OPEN_READWRITE);
    return myDataBase;
}

@Override
public synchronized void close() {

    if (myDataBase != null)
        myDataBase.close();

    super.close();

}

ArrayList<String> getAllDiseases() {
    ArrayList<String> list = new ArrayList<String>();

    myDataBase = khol();
    Cursor cur = myDataBase.rawQuery("SELECT " + col2 + " from "
            + deptTable + " WHERE " + colname + "='head'", new String[] {});
    while (cur.moveToNext()) {
        list.add(cur.getString(cur.getColumnIndexOrThrow(col2)));

    }
    cur.close();
    myDataBase.close();
    return list;
}

Cursor getWordMatches(String query) {
    myDataBase = khol();
    Cursor cur = myDataBase.rawQuery("SELECT " + colid +","+col2+" ,"
            + col3 + " from " + deptTable + " WHERE " + col2 + " LIKE '"
            + query + "%'", new String[] {});
    /*Cursor cur = myDataBase.rawQuery("SELECT " +"'"+ colid+"'" +","+"'"+col2+"'"+" ,"
            + "'"+col3+"'" + " from " + deptTable + " WHERE " + "'"+col2+"'" + " LIKE '"
            + query + "%'", new String[] {});*/

    //myDataBase.close();
    return cur;
}

void getstring(String s) {
    this.s = s;
}

Cursor getdisease() {

    myDataBase = khol();
    Cursor cur = myDataBase.rawQuery("SELECT " + col3 + " from "
            + deptTable + " WHERE " + colid + "='" + s + "'",
            new String[] {});
    return cur;
}

String getdisease1() {

    String list;
    myDataBase = khol();
    Cursor cur = myDataBase.rawQuery("SELECT " + col3 + " from "
            + deptTable + " WHERE " + colid + "='" + s + "'",
            new String[] {});
    cur.moveToFirst();
    list = cur.getString(cur.getColumnIndexOrThrow(col3));
    return list;
}


@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

}

私は多くの方法を試しましたが、getWordMatches が何らかの問題を引き起こしていることがわかりました。回答で提案されているように、すべての列で「'」を使用しても、何も返されません。しかし、「'」なしで使用すると、「そのような列はありません」というエラーが発生します。
助けてください。

4

1 に答える 1

0

データベースを閉じないでください。この行を削除myDataBase.close();

Habib.OSUが言ったことに加えて

于 2012-04-17T17:58:35.090 に答える