获取带id的单行时Sqlite数据库出错

时间:2013-12-07 07:56:13

标签: android sqlite

在我的应用程序中,我将保存数据保存到表簿,它将book_id和Intent返回给ThemesActivity.class。如果用户选择ThemeGridview中的主题(ThemesActivity中的布局)。我想检查它bofore将它们保存在Table Theme.now my解决方案是通过book_id来获取表主题中的数据主题。如果它返回null。我想sava一个单击Table Theme的ThemeName,但它是返回数据(id为book_id的数据行),我想检查点击的ThemeName Table Theme中的和themeName如果相等则不执行任何操作。但不等于我想要在新的themeName中更新列filepath中的数据。

这是我在BookDB中的代码(关于insert,getAll,getByID,Delete等的类操作)

public Object getthemeByID(int id){
    //สร้าง array เพื่อรับค่าจากชุดกลุ่มใข้อมูลจาก cursor
    Object imagepath = new ArrayList<ThemeEntry>();
    // selectionArgs คือ เงื่อนไข
    int bookID = id;
    Cursor cursor = db.rawQuery("SELECT filepath FROM " + BooksDBHelper.TABLE_THEME + " WHERE bokID = ? ", new String[]{Integer.toString(bookID)});
    cursor.moveToFirst();
    if(cursor.getCount()!=0){
        ThemeEntry theme = new ThemeEntry();
        theme.setFilepath(cursor.getString(cursor.getColumnIndex(BooksDBHelper.KEY_fILEPATH)));
        imagepath = theme;
    }
    return (imagepath);



}

这是我的活动

public class ChThemesActivity extends Activity{

private GridView myGrid;
private Context context;
private GridViewAdapter adapter;
private BooksDB db;


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_themes);
    context = this;

    myGrid = (GridView) findViewById(R.id.gridView1);
    myGrid.setAdapter(new GridViewAdapter(context));

    //event 
    myGrid.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position,
                long id) {
            adapter = new GridViewAdapter(context);
            db = new BooksDB(context);
            //get bokID
            Intent intent = getIntent();
            int bokID = intent.getIntExtra("id", 0);
            //get image name
            String imageName = getResources().getResourceEntryName(adapter.thumbimage[position]);
            //save data to Theme table
            //pass bokID to check value

           Object imagepath = db.getthemeByID(bokID);
            if(imagepath != null){
                String value = imagepath.toString();
                Toast.makeText(getApplicationContext(), "is"+value, Toast.LENGTH_SHORT).show();
            }       



            Toast.makeText(getApplicationContext(), "book name"+imageName+","+"bokID is"+bokID+", Toast.LENGTH_SHORT).show();

        }
    });
}

这是BookDBHelper中的表主题

public static String THEME_CREATE_SQL = "CREATE TABLE IF NOT EXISTS " + TABLE_THEME + "(" +
                                ""+KEY_ID+" INTEGER PRIMARY KEY AUTOINCREMENT," +
                                ""+KEY_fILEPATH+" VACHAR(255)," +
                                ""+KEY_BOK_ID+" INTEGER(11)," +
                                " FOREIGN KEY ("+KEY_BOK_ID+") REFERENCES "+TABLE_BOOK+" ("+KEY_ID+"));";

这是一个logcat错误

    12-07 03:30:13.262: E/SQLiteLog(1513): (1) no such column: filepath
12-07 03:30:13.272: D/AndroidRuntime(1513): Shutting down VM
12-07 03:30:13.272: W/dalvikvm(1513): threadid=1: thread exiting with uncaught exception (group=0x41465700)
12-07 03:30:13.325: E/AndroidRuntime(1513): FATAL EXCEPTION: main
12-07 03:30:13.325: E/AndroidRuntime(1513): android.database.sqlite.SQLiteException: no such column: filepath (code 1): , while compiling: SELECT filepath FROMthemes WHERE bokID = ?
12-07 03:30:13.325: E/AndroidRuntime(1513):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
12-07 03:30:13.325: E/AndroidRuntime(1513):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
12-07 03:30:13.325: E/AndroidRuntime(1513):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
12-07 03:30:13.325: E/AndroidRuntime(1513):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
12-07 03:30:13.325: E/AndroidRuntime(1513):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
12-07 03:30:13.325: E/AndroidRuntime(1513):     at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
12-07 03:30:13.325: E/AndroidRuntime(1513):     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
12-07 03:30:13.325: E/AndroidRuntime(1513):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
12-07 03:30:13.325: E/AndroidRuntime(1513):     at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
12-07 03:30:13.325: E/AndroidRuntime(1513):     at com.example.mutitablesql.db.BooksDB.getthemeByID(BooksDB.java:84)
12-07 03:30:13.325: E/AndroidRuntime(1513):     at com.example.mutitablesql.ChThemesActivity$1.onItemClick(ChThemesActivity.java:49)
12-07 03:30:13.325: E/AndroidRuntime(1513):     at android.widget.AdapterView.performItemClick(AdapterView.java:298)
12-07 03:30:13.325: E/AndroidRuntime(1513):     at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
12-07 03:30:13.325: E/AndroidRuntime(1513):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2788)
12-07 03:30:13.325: E/AndroidRuntime(1513):     at android.widget.AbsListView$1.run(AbsListView.java:3463)
12-07 03:30:13.325: E/AndroidRuntime(1513):     at android.os.Handler.handleCallback(Handler.java:730)
12-07 03:30:13.325: E/AndroidRuntime(1513):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-07 03:30:13.325: E/AndroidRuntime(1513):     at android.os.Looper.loop(Looper.java:137)
12-07 03:30:13.325: E/AndroidRuntime(1513):     at android.app.ActivityThread.main(ActivityThread.java:5103)
12-07 03:30:13.325: E/AndroidRuntime(1513):     at java.lang.reflect.Method.invokeNative(Native Method)
12-07 03:30:13.325: E/AndroidRuntime(1513):     at java.lang.reflect.Method.invoke(Method.java:525)
12-07 03:30:13.325: E/AndroidRuntime(1513):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-07 03:30:13.325: E/AndroidRuntime(1513):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-07 03:30:13.325: E/AndroidRuntime(1513):     at dalvik.system.NativeStart.main(Native Method)

请帮忙,谢谢

2 个答案:

答案 0 :(得分:0)

正确检查KEY_fILEPATH,异常清楚地表明列文件路径不存在。

答案 1 :(得分:0)

compiling: SELECT filepath FROMthemes WHERE bokID = ?

请注意这里。所以FROMthemes应该是FROM themes,就像这样,

选择查询:

Cursor cursor = db.rawQuery("SELECT filepath FROM" + BooksDBHelper.TABLE_THEME + " WHERE bokID = ? ", new String[]{Integer.toString(bookID)});

这应该是

Cursor cursor = db.rawQuery("SELECT filepath FROM " + BooksDBHelper.TABLE_THEME + " WHERE bokID = ? ", new String[]{Integer.toString(bookID)});

此处添加了空格 FROM“

像这样。

让我知道会发生什么。