0

我在 Google 上搜索了一些关于 SQL 数据库的教程。我在将图像插入数据库时​​遇到了一些问题。我尝试了很多方法(例如setImageBitmap, setImageDrawable, setImageResource),但没有一个有效。我已经将一些图像插入到我的drawable-hdpi.

这是我的一些插入代码:

public void insertIntoTable(){
    try{
        mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null);

        mydb.execSQL("INSERT INTO " + TABLE + "(DNAME, DTAG, DSUMMARY, DINGRE, DSTEP) VALUES('Chicken Cabonara','Chicken','aaa', '3', '5')");
        mydb.execSQL("INSERT INTO " + TABLE + "(DNAME, DTAG, DSUMMARY, DINGRE, DSTEP) VALUES('Seafood Spagetti','Fish','bbb', '5', '3')");
        mydb.execSQL("INSERT INTO " + TABLE + "(DNAME, DTAG, DSUMMARY, DINGRE, DSTEP) VALUES('Tomyam Soup','Soup','ccc', '3', '1')");

       mydb.close();
    }
}

我的数据显示代码:

public void showTableValues(){
    try{
        mydb = openOrCreateDatabase(DBNAME, Context.MODE_PRIVATE,null);
        Cursor allrows  = mydb.rawQuery("SELECT * FROM "+  TABLE, null);
        System.out.println("COUNT : " + allrows.getCount());
        Integer cindex = allrows.getColumnIndex("DNAME");
        Integer cindex1 = allrows.getColumnIndex("DTAG");
        Integer cindex2 = allrows.getColumnIndex("DSUMMARY");
        Integer cindex3 = allrows.getColumnIndex("DINGRE");
        Integer cindex4 = allrows.getColumnIndex("DSTEP");

        TextView t = new TextView(this);
        t.setText("========================================");
        Linear.addView(t);

        if(allrows.moveToFirst()){
            do{
                LinearLayout did_row   = new LinearLayout(this);
                LinearLayout dname_row = new LinearLayout(this);
                LinearLayout dtag_row= new LinearLayout(this);          
                LinearLayout dsummary_row= new LinearLayout(this);
                LinearLayout dingre_row= new LinearLayout(this); 
                LinearLayout dstep_row= new LinearLayout(this);

                final TextView did_  = new TextView(this);
                final TextView dname_ = new TextView(this);
                final TextView dtag_ = new TextView(this);
                final TextView dsummary_ = new TextView(this);
                final TextView dingre_ = new TextView(this);
                final TextView dstep_ = new TextView(this);
                final TextView   sep  = new TextView(this);

                String DID = allrows.getString(0);
                String DNAME= allrows.getString(1);
                String DTAG= allrows.getString(2);
                String DSUMMARY= allrows.getString(3);
                String DINGRE= allrows.getString(4);
                String DSTEP= allrows.getString(5);

                System.out.println("DNAME " + allrows.getString(cindex) + " DTAG : "+ allrows.getString(cindex1) + " DSUMMARY : "+ allrows.getString(cindex2) + " DINGRE : "+ allrows.getString(cindex3) + " DSTEP : "+ allrows.getString(cindex4));
                System.out.println("DID : "+ DID  + " || DNAME " + DNAME + "|| DTAG : "+ DTAG + "|| DSUMMARY : "+ DSUMMARY + "|| DINGRE : "+ DINGRE + "|| DSTEP : "+ DSTEP);

                did_.setText("ID : " + DID);
                did_row.addView(did_);
                Linear.addView(did_row);

                dname_.setText("NAME : "+ DNAME);
                dname_row.addView(dname_);
                Linear.addView(dname_row);

                dtag_.setText("TAG : " + DTAG);
                dtag_row.addView(dtag_);
                Linear.addView(dtag_row);

                dsummary_.setText("SUMMARY : "+ DSUMMARY);
                dsummary_row.addView(dsummary_);
                Linear.addView(dsummary_row);

                dingre_.setText("INGREDIENT : " + DINGRE);
                dingre_row.addView(dingre_);
                Linear.addView(dingre_row);

                dstep_.setText("STEP : "+ DSTEP);
                dstep_row.addView(dstep_);
                Linear.addView(dstep_row);

                sep.setText("---------------------------------------------------------------");
                Linear.addView(sep);
            }
            while(allrows.moveToNext());
        }
        mydb.close();
     }
}

有人可以告诉我将图像(png)插入数据库并显示图像的方式吗?谢谢你。

4

1 回答 1

0

您必须将图像作为 blob 数据插入。您可以查看链接。

也可以参考Link1

于 2012-11-27T12:58:05.193 回答