我试图制作一个布尔函数,返回是否在数据库中找到了来自Arraylist的字符串,它不断崩溃我的应用程序,我不确定原因。
ArrayList<String> suggestedWords = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
// mApp.setGlobalArray(suggestedWords);
for (int i = 0; i < suggestedWords.size(); i++) {
if(hasObject(suggestedWords.get(i))){
Log.d("GOT IT RIGHT",suggestedWords.get(i));
}
}
这是布尔函数
public boolean hasObject(String id) {
db = new DataBaseHelper(getApplicationContext());
sqdb = db.getWritableDatabase();
String selectString = "SELECT * FROM wordsdata WHERE Enwd"+ " =?";
Cursor cursor = sqdb.rawQuery(selectString, new String[]{id});
boolean hasObject = false;
if(cursor.moveToFirst()){
hasObject = true;
int count = 0;
while(cursor.moveToNext()){
count++;
}
//here, count is records found
Log.d("IS WORD THERE", String.format("%d records found", count));
}
cursor.close();
db.close();
return hasObject;
}
编辑:logcat
Caused by: android.database.sqlite.SQLiteException: no such column: Square (code 1): , while compiling: Select * from wordsdata where Enwd = Square
我使用的数据库是我的Assets文件中预先存在的数据库,我已经测试过它我的数据库没有问题
EDIT2:我的DataBaseHelper
class DataBaseHelper extends SQLiteAssetHelper {
//database variables
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "voicedata.db";
private static final String TABLE_NAME = "wordsdata";
private static final String KEY_ID = "_id";
private static final String Enwd = "Enwd";
private static final String Enno = "Enno";
private static final String Enyes = "Enyes";
private final Context mContext;
DataBaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
mContext = context;
}
这是我的完整logcat
FATAL EXCEPTION: main
Process: com.example.nourhamran.anothertest, PID: 1142
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=999, result=-1, data=Intent { launchParam=MultiScreenLaunchParams { mDisplayId=0 mFlags=0 }(has extras) }} to activity {com.example.nourhamran.anothertest/com.example.nourhamran.anothertest.MainActivity}: android.database.sqlite.SQLiteException: no such column: plane (code 1): , while compiling: Select * from wordsdata where Enwd = plane
#################################################################
Error Code : 1 (SQLITE_ERROR)
Caused By : SQL(query) error or missing database.
(no such column: plane (code 1): , while compiling: Select * from wordsdata where Enwd = plane)
#################################################################
at android.app.ActivityThread.deliverResults(ActivityThread.java:4472)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4515)
at android.app.ActivityThread.-wrap22(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1687)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Caused by: android.database.sqlite.SQLiteException: no such column: plane (code 1): , while compiling: Select * from wordsdata where Enwd = plane
#################################################################
Error Code : 1 (SQLITE_ERROR)
Caused By : SQL(query) error or missing database.
(no such column: plane (code 1): , while compiling: Select * from wordsdata where Enwd = plane)
#################################################################
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1005)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:570)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1697)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1636)
at com.example.nourhamran.anothertest.MainActivity.iswordindb(MainActivity.java:284)
at com.example.nourhamran.anothertest.MainActivity.onActivityResult(MainActivity.java:229)
at android.app.Activity.dispatchActivityResult(Activity.java:7256)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4468)
答案 0 :(得分:1)
查询无效,因为Square
没有像这样引用
Select * from wordsdata where Enwd = 'Square'
但是,您正确使用rawQuery
,因此我认为没有理由显示所显示的代码会导致该错误,因此我认为您在其他地方获得了该错误,"Enwd = " + id
作为您所拥有的内容的替代方案,请在DataBaseHelper
中实施此方法。
public boolean hasObject(String id) {
SQLiteDatabase sqdb = getReadableDatabase();
String selectString = "SELECT * FROM wordsdata WHERE " + Enwd + "=?";
Cursor cursor = sqdb.rawQuery(selectString, new String[]{id});
cursor.moveToFirst();
long count = cursor.getCount();
boolean hasObject = count > 0;
//here, count is records found
Log.d("IS WORD THERE", String.format("%d records found", count));
cursor.close();
db.close();
return hasObject;
}
在外面使用
db = new DataBaseHelper(getApplicationContext());
boolean hasId = db.hasObject(id);
注意:你应该在查询中真正做COUNT(*)
以使SQLite自己进行计数