2012-11-27 3 views
0

SQL 데이터베이스에 대한 몇 가지 자습서를 살펴 보았습니다. 데이터베이스에 이미지를 삽입 할 때 몇 가지 문제가 발생했습니다. 많은 방법 (예 : setImageBitmap, setImageDrawable, setImageResource)을 시도했지만 그 중 일부는 작동하지 않습니다. 이미 이미지 중 일부를 내 drawable-hdpi에 삽입했습니다.SQL에 이미지 삽입

여기에 삽입 내 코드의 일부입니다 :

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)의 길을 보여줄 수 이미지? 고맙습니다.

+2

당신은 데이터베이스에서 이미지 경로를 저장하고 폴더에 이미지를 저장할 수 있습니다 참조 할 수 있습니다. 이것이 최선의 방법입니다. –

+0

예를 들어 여기에서 답을 얻었습니다. http://stackoverflow.com/questions/7331310/how-to-store-image-as-blob-in-sqlite-how-to-retrieve-it - but 일반적으로 이미지의 경로 만 저장하는 것이 좋습니다. 작은 아이콘을 저장하는 것이 좋습니다 – zapl

+0

@NiravRanpara 따라서 이미지가 /SqlRecipe/res/drawable-hdpi/img1.png 경로에 있으면 텍스트로 삽입해야합니다. –

답변

0

이미지를 BLOB 데이터로 삽입해야합니다. this 링크를 볼 수 있습니다.

또한 Link1