【问题标题】:Insertion of images to SQL将图像插入 SQL
【发布时间】:2012-11-27 12:33:22
【问题描述】:

我在 Google 上搜索了一些关于 SQL 数据库的教程。我在将图像插入数据库时​​遇到了一些问题。我尝试了很多方法(例如setImageBitmapsetImageDrawablesetImageResource),但没有一个有效。我已经在我的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)插入数据库并显示图像的方式吗?谢谢。

【问题讨论】:

  • 您可以将图像路径保存在数据库中,也可以将图像保存在文件夹中。这是最好的方法
  • 已在此处被询问和回答,例如 stackoverflow.com/questions/7331310/… - 但仅存储图像的路径通常是一个更好的主意。存储小图标就可以了
  • @NiravRanpara 所以,如果图像在这个路径中,/SqlRecipe/res/drawable-hdpi/img1.png,我必须在我的插入中将它保存为文本?
  • @zapl 我也看到了链接,但图片来自网站。我有我自己的图像。谢谢!
  • 只能保存图片名称。并使用 Environment.getExternalStorageDirectory().getAbsolutePath() 获取图像路径

标签: android sql database image


【解决方案1】:

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

也可以参考Link1

【讨论】:

    猜你喜欢
    • 2019-02-22
    • 2014-03-03
    • 2017-12-02
    • 1970-01-01
    • 1970-01-01
    • 2020-10-09
    • 2014-03-03
    • 1970-01-01
    • 2017-11-16
    相关资源
    最近更新 更多