无法在ContentProvider中连接表

时间:2014-10-28 08:39:58

标签: android android-contentprovider

我使用内容提供商,但表格并未合并。 我创建了以下表格。

test                      category 
id,text,text,text         id,text 

接下来,我在test.id = category.id"上执行" select * from test inner join category,并尝试使用MainActivity中的readDB()获取值,但显示以下错误消息。 "编译时:SELECT图像,单词,日元,类别FROM test" 我不知道应该在哪里修理。 SQL语句错了吗?或者表格没有创建? 我遇到了很大的麻烦。请帮帮我。

public class MyActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_my); 
        writeTestDB("A.png", "ABC", "¥280", "category1"); 
        writeCateDB("category1"); 
        readDB(); 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
        getMenuInflater().inflate(R.menu.my, menu); 
        return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
        int id = item.getItemId(); 
        if (id == R.id.action_settings) { 
            return true; 
        } 
        return super.onOptionsItemSelected(item); 
    } 

    private void writeTestDB(String image, String word, String yen, String category){ 
        Uri uri=Uri.parse("content://com.example.appdev.mycontentprovider/test"); 
        ContentValues values=new ContentValues(); 
        values.put("image",image); 
        values.put("word",word); 
        values.put("yen",yen); 
        getContentResolver().insert(uri,values); 
    } 

    private void writeCateDB(String cate){ 
        Uri uri=Uri.parse("content://com.example.appdev.mycontentprovider/category"); 
        ContentValues values=new ContentValues(); 
        values.put("category",cate); 
        getContentResolver().insert(uri,values); 
    } 

    private void readDB(){ 
        Uri uri=Uri.parse("content://com.example.appdev.mycontentprovider/test"); 
        Cursor c=this.getContentResolver().query(uri,new String[]{"image","word","yen","category"},null,null,null); 
        c.moveToFirst(); 
        String fileName=c.getString(0); 
        String word=c.getString(1); 
        String yen=c.getString(2); 
        String category=c.getString(3); 
        setUI(fileName,word,yen,category); 
    } 

    public Bitmap loadImage(String fileName, Context context) { 
        final AssetManager assetManager = context.getAssets(); 
        InputStream is = null; 
        Bitmap image=null; 
        try { 
            is = assetManager.open(fileName); 
            image=BitmapFactory.decodeStream(is); 
        } catch (Exception e) { 
            e.printStackTrace(); 
        }finally { 
            try{ 
                is.close(); 
            }catch (Exception e){ 
                e.printStackTrace(); 
            } 
        } 
        return image; 
    } 

    public void setUI(String fileName,String word,String yen,String category){ 
        TextView categoryText=(TextView)findViewById(R.id.category); 
        categoryText.setText(category); 
        Bitmap image=loadImage(fileName,this); 
        ImageView imageView=(ImageView)findViewById(R.id.image); 
        imageView.setImageBitmap(image); 
        TextView wordText=(TextView)findViewById(R.id.word); 
        wordText.setText(word); 
        TextView yenText=(TextView)findViewById(R.id.yen); 
        yenText.setText(yen); 
    } 
} 

-------------------------------------------------------------------------------------------- 

public class DBProvider extends ContentProvider { 
    private final static String DB_NAME="test.db"; 
    private final static String DB_TABLE="test"; 
    private static int DB_VERSION; 
    private final static String CATEGORY_DB_TABLE="category"; 

    private SQLiteDatabase db; 

    @Override 
    public boolean onCreate() { 
        PackageManager packageManager=getContext().getPackageManager(); 
        try{ 
            PackageInfo packageInfo=packageManager.getPackageInfo(getContext().getPackageName(), PackageManager.GET_ACTIVITIES); 
            DB_VERSION=packageInfo.versionCode; 
        }catch (PackageManager.NameNotFoundException e){ 
            e.printStackTrace(); 
        } 
        DBHelper dbHelper=new DBHelper(getContext()); 
        db=dbHelper.getWritableDatabase(); 
        return (db!=null); 
    } 

    @Override 
    public Cursor query(Uri uri,String[] columns,String selection, 
                        String[] selectionArgs,String sortOrder) { 
        return db.query(DB_TABLE,columns,selection, 
                selectionArgs,null,null,null); 
    } 

    @Override 
    public int update(Uri uri,ContentValues values, 
                      String selection,String[] selectionArgs) { 
        return db.update(DB_TABLE,values,null,null); 
    } 

    @Override 
    public Uri insert(Uri uri,ContentValues values) { 
        db.insert(DB_TABLE,"",values); 
        return uri; 
    } 

    @Override 
    public int delete(Uri uri,String selection, 
                      String[] selectionArgs) { 
        return 0; 
    } 

    @Override 
    public String getType(Uri uri) { 
        return null; 
    } 

    private static class DBHelper extends SQLiteOpenHelper { 

        public DBHelper(Context context) { 
            super(context,DB_NAME,null,DB_VERSION); 
        } 

        @Override 
        public void onCreate(SQLiteDatabase db) { 
            db.execSQL("create table if not exists "+DB_TABLE+"(id integer primary key autoincrement,image text,word text, yen text)"); 
        } 

        @Override 
        public void onUpgrade(SQLiteDatabase db, 
                              int oldVersion,int newVersion) { 
            db.execSQL("create table if not exists "+CATEGORY_DB_TABLE+"(id integer primary key autoincrement, category text)"); 
            db.execSQL("select * from test inner join category on test.id = category.id",null); 
        } 

        @Override 
        public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
            db.execSQL("drop table if exists "+DB_TABLE); 
            db.execSQL("drop table if exists "+CATEGORY_DB_TABLE); 
            onCreate(db); 
        } 
    } 
} 

0 个答案:

没有答案