伙计们我一直在研究这段代码很长一段时间,但我无能为力。虽然列存在,但它一直在返回错误,顺便说一句。我在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 Class
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导致了一些问题。它没有返回任何内容,即使我在答案中建议的所有列上使用“'”。但如果我没有使用“'”那么它就会出现“没有这样的列”的错误 请帮忙。
答案 0 :(得分:0)
不要关闭数据库。删除此行myDataBase.close();
除了Habib.OSU所说的