注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

山歌

微信公众号:Jack-Xiaoshan

 
 
 
 
 

日志

 
 

SQLite  

2009-12-02 11:33:57|  分类: 默认分类 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

Blob数据类型在数据库中的存储:

Connection con....... FileInputStream fis = new FileInputStream(f); String sql = "insert into shop(id,name,images) values(?,?,?)"; PreparedStatement pstmt = con.prepareStatement(sql); pstmt.setInt(1,id); pstmt.setString(2,name); pstmt.setBinaryStream(3, fis,f.length()); pstmt.executeUpdate(); fis.close(); pstmt.close(); con.close();
这是一个Android范例,兹说明如下: Step-1:首先将.mp3档案放入Project的/res/raw/里,如下: 程式一开始执行,建立一个资料库,含有BLOB栏位,如下之指令:
sql = "create table mySong(" + "song_no text not null, " + "song_mp3 blob );"; try { db.execSQL(sql); } catch (SQLException e) { Log.e( "ERROR", e.toString()); return; }
Step-2:从Project的/res/raw/读取*.mp3歌曲,然后分段储存到SQLite的BLOB里,如下之指令:
InputStream is = getResources().openRawResource(rid); int bufSize = 63*1024; byte buffer = new byte[bufSize]; try { int size = is.read(buffer); while(size >= 0){ ByteArrayOutputStream out = new ByteArrayOutputStream(size); out.write(buffer, 0, size); out.flush(); out.close(); cv.put( "song_mp3", out.toByteArray()); db.insert( "mySong", null, cv); size = is.read(buffer); } } catch (IOException e) { Log.e( "ERROR", e.toString()); }
Step-3:从SQLite的BLOB里,读取歌曲并存入 /data/data/com.misoo.SQ01/files/song.mp3, 如下之指令:
FileOutputStream os = null; try{ os = openFileOutput("song.mp3", MODE_WORLD_READABLE); } catch(FileNotFoundException e){ Log.e( "ERROR", e.toString()); } byte red_buf; //---------------------------------------- mOpenHelper = new DatabaseHelper(this); SQLiteDatabase db = mOpenHelper.getReadableDatabase(); String col = {"song_no", "song_mp3" }; cur = db.query("mySong", col, cond, null, null, null, null); int k =0; cur.moveToFirst(); try{ while(!cur.isAfterLast()){ red_buf = cur.getBlob(1); os.write(red_buf); k ++; cur.moveToNext(); } os.flush(); os.close(); } catch(Exception e){ Log.e( "ERROR", e.toString()); return; }
Step-4:使用MediaPlayer将 /data/data/com.misoo.SQ01/files/song.mp3, 播放出来,如下之指令:
String path = "/data/data/com.misoo.SQ01/files/song.mp3"; mPlayer = new MediaPlayer(); try { mPlayer.setDataSource(path); mPlayer.prepare(); } catch (IOException e) { e.printStackTrace(); } mPlayer.start(); }
其实,BLOB栏位可储存很大的资料量,在本范例里,刻意将歌曲切成许多段,逐一存入资料库里。其目的只是为了举例而已。
package com.misoo.SQ01; import java.io.ByteArrayOutputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import android.app.Activity; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.media.MediaPlayer; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; public class ac01 extends Activity implements OnClickListener{ private static final String DB_NAME = "mp3Song.db"; private static final int DB_VERSION = 2; private Button btn, btn2, btn3; private Cursor cur; private MediaPlayer mPlayer; private static class DatabaseHelper extends SQLiteOpenHelper { DatabaseHelper(Context context) { super(context, DB_NAME, null, DB_VERSION); } @Override public void onCreate(SQLiteDatabase db) { } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } } @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); LinearLayout layout = new LinearLayout(this); layout.setOrientation(LinearLayout.VERTICAL); btn = new Button(this); btn.setId( 101); btn.setText( "play"); btn.setBackgroundResource(R.drawable.heart); btn.setOnClickListener( this); LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(80, 50); param.topMargin = 10; layout.addView(btn, param); btn2 = new Button(this); btn2.setId( 102); btn2.setText( "stop"); btn2.setBackgroundResource(R.drawable.heart); btn2.setOnClickListener( this); layout.addView(btn2, param); btn3 = new Button(this); btn3.setId( 103); btn3.setText( "exit"); btn3.setBackgroundResource(R.drawable.heart); btn3.setOnClickListener( this); layout.addView(btn3, param); setContentView(layout); setTitle( "Saving into SQliteDB..."); //--------------------------------- init(); setTitle( "Saved in SQliteDB."); } private DatabaseHelper mOpenHelper; public void init(){ mOpenHelper = new DatabaseHelper(this); SQLiteDatabase db = mOpenHelper.getWritableDatabase(); //----------------------------------- String sql = "drop table mySong"; try { db.execSQL(sql); } catch (SQLException e) { Log.e( "ERROR", e.toString()); } //----------------------------------- sql = "create table mySong(" + "song_no text not null, " + "song_mp3 blob );"; try { db.execSQL(sql); } catch (SQLException e) { Log.e( "ERROR", e.toString()); return; } //--------------------------------- SaveOneSong(db,"s01", R.raw.den_li_guing); } public void SaveOneSong(SQLiteDatabase db, String key, int rid){ ContentValues cv = new ContentValues(); cv.put( "song_no", key); InputStream is = getResources().openRawResource(rid); int bufSize = 63*1024; byte buffer = new byte[bufSize]; try { int size = is.read(buffer); while(size >= 0){ ByteArrayOutputStream out = new ByteArrayOutputStream(size); out.write(buffer, 0, size); out.flush(); out.close(); cv.put( "song_mp3", out.toByteArray()); db.insert( "mySong", null, cv); size = is.read(buffer); } } catch (IOException e) { Log.e( "ERROR", e.toString()); } } public void play(String cond){ FileOutputStream os = null; try{ os = openFileOutput("song.mp3", MODE_WORLD_READABLE); } catch(FileNotFoundException e){ Log.e( "ERROR", e.toString()); } byte red_buf; //---------------------------------------- mOpenHelper = new DatabaseHelper(this); SQLiteDatabase db = mOpenHelper.getReadableDatabase(); String col = {"song_no", "song_mp3" }; cur = db.query("mySong", col, cond, null, null, null, null); int k =0; cur.moveToFirst(); try{ while(!cur.isAfterLast()){ red_buf = cur.getBlob(1); os.write(red_buf); k ++; cur.moveToNext(); } os.flush(); os.close(); } catch(Exception e){ Log.e( "ERROR", e.toString()); return; } String path = "/data/data/com.misoo.SQ01/files/song.mp3"; mPlayer = new MediaPlayer(); try { mPlayer.setDataSource(path); mPlayer.prepare(); } catch (IOException e) { e.printStackTrace(); } mPlayer.start(); } public void onClick(View v) { switch (v.getId()) { case 101: String cond = "song_no='s01'"; play(cond); break; case 102: stop(); break; case 103: stop(); finish(); break; } } public void stop() { if (mPlayer != null) { mPlayer.stop(); mPlayer.release(); mPlayer = null; } } }
  评论这张
 
阅读(14)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017