在android中的SQLite中获取行的id

时间:2012-12-24 06:35:30

标签: android sqlite

在我的应用程序中,我想获取特定行的id。我使用以下代码:

public String getid(String  heading) throws SQLException
    {
        System.out.println("ddbpos="+heading);
        long recc=0;
        String rec=null;
        Cursor mCursor = db.rawQuery(
                 "SELECT id  FROM  savedstoriestable WHERE heading="+heading , null);
    if (mCursor != null) 
    {
    mCursor.moveToFirst();
    recc=mCursor.getLong(0);
    rec=String.valueOf(recc);
    }
    return rec;
    }

我的数据库如下所示:

id      heading                                              Description                                    
1       Congress countdown for 2014 polls:                   ghdfsfdghsdjfsdfhds
        Possibility of UPA-III hinges on how other         

但它显示“Force to close”错误:

我的Logcat如下:

12-24 12:02:10.733: E/AndroidRuntime(22030): Uncaught handler: thread main exiting due to uncaught exception
12-24 12:02:10.804: E/AndroidRuntime(22030): android.database.sqlite.SQLiteException: near "countdown": syntax error: , while compiling: SELECT id  FROM  savedstoriestable WHERE heading=Congress countdown for 2014 polls: Possibility of UPA-III hinges on how other parties fare
12-24 12:02:10.804: E/AndroidRuntime(22030):    at android.database.sqlite.SQLiteProgram.native_compile(Native Method)
12-24 12:02:10.804: E/AndroidRuntime(22030):    at android.database.sqlite.SQLiteProgram.compile(SQLiteProgram.java:110)
12-24 12:02:10.804: E/AndroidRuntime(22030):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
12-24 12:02:10.804: E/AndroidRuntime(22030):    at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:49)
12-24 12:02:10.804: E/AndroidRuntime(22030):    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:49)
12-24 12:02:10.804: E/AndroidRuntime(22030):    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1221)
12-24 12:02:10.804: E/AndroidRuntime(22030):    at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1194)
12-24 12:02:10.804: E/AndroidRuntime(22030):    at com.newspaper.DBadapter.getid(DBadapter.java:104)
12-24 12:02:10.804: E/AndroidRuntime(22030):    at com.newspaper.SavedStoriesActivity.onOptionsItemSelected(SavedStoriesActivity.java:72)
12-24 12:02:10.804: E/AndroidRuntime(22030):    at android.app.Activity.onMenuItemSelected(Activity.java:2170)
12-24 12:02:10.804: E/AndroidRuntime(22030):    at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:730)
12-24 12:02:10.804: E/AndroidRuntime(22030):    at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:139)
12-24 12:02:10.804: E/AndroidRuntime(22030):    at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
12-24 12:02:10.804: E/AndroidRuntime(22030):    at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:525)
12-24 12:02:10.804: E/AndroidRuntime(22030):    at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122)
12-24 12:02:10.804: E/AndroidRuntime(22030):    at android.view.View.onTouchEvent(View.java:4179)
12-24 12:02:10.804: E/AndroidRuntime(22030):    at android.widget.TextView.onTouchEvent(TextView.java:6541)
12-24 12:02:10.804: E/AndroidRuntime(22030):    at android.view.View.dispatchTouchEvent(View.java:3709)
12-24 12:02:10.804: E/AndroidRuntime(22030):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
12-24 12:02:10.804: E/AndroidRuntime(22030):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
12-24 12:02:10.804: E/AndroidRuntime(22030):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
12-24 12:02:10.804: E/AndroidRuntime(22030):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
12-24 12:02:10.804: E/AndroidRuntime(22030):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-24 12:02:10.804: E/AndroidRuntime(22030):    at android.os.Looper.loop(Looper.java:123)
12-24 12:02:10.804: E/AndroidRuntime(22030):    at android.app.ActivityThread.main(ActivityThread.java:4363)
12-24 12:02:10.804: E/AndroidRuntime(22030):    at java.lang.reflect.Method.invokeNative(Native Method)
12-24 12:02:10.804: E/AndroidRuntime(22030):    at java.lang.reflect.Method.invoke(Method.java:521)
12-24 12:02:10.804: E/AndroidRuntime(22030):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-24 12:02:10.804: E/AndroidRuntime(22030):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-24 12:02:10.804: E/AndroidRuntime(22030):    at dalvik.system.NativeStart.main(Native Method)

帮我解决这个问题。提前谢谢。

1 个答案:

答案 0 :(得分:6)

试试这个:

Cursor mCursor = db.rawQuery(
                 "SELECT id  FROM  savedstoriestable WHERE heading= '"+heading+"'" , null);

标题是一个字符串。所以你必须把它放在''。