mysql - 文件存進(jìn)數(shù)據(jù)庫
問題描述
例如word TXT excal 圖片等,這些程序中經(jīng)常會用到的文件,怎么把它們存到數(shù)據(jù)庫中;
問題解答
回答1:第一種Python如果你是用類似sqlalchemy這樣的orm數(shù)據(jù)庫
def upload_blob(file_data): fb = StringIO.StringIO() file_data.save(fb) filename = file_data.filename c_blob_id = None if filename:blob = T_Blob()blob.c_filename = filenameblob.c_blob = fb.getvalue()db.session.add(blob)db.session.flush()c_blob_id = blob.id return c_blob_id調(diào)用 form = AddFileForm() if form.validate_on_submit():user = g.userc_blob_id = models.upload_blob(form.c_fj.data)
第二種 java jdbc方式插入Oracle
import java.sql.*; import java.io.*; import oracle.sql.*; public class IntoOracle { public static void main(String[] args) { try { DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); Connection conn = OracleFactory.getOracle(); conn.setAutoCommit(false);BLOB blob = null;PreparedStatement pstmt = conn.prepareStatement('insert into blobtest(id,b) values(?,empty_blob())'); pstmt.setString(1,'50'); pstmt.executeUpdate(); pstmt.close(); pstmt = conn.prepareStatement('select b from blobtest where id= ? for update'); pstmt.setString(1,'50'); ResultSet rset = pstmt.executeQuery(); if (rset.next()) blob = (BLOB) rset.getBlob(1); String fileName = 'd:bjx.jpg'; File f = new File(fileName); FileInputStream fin = new FileInputStream(f); System.out.println('file size = ' + fin.available()); pstmt = conn.prepareStatement('update blobtest set b=? where id=?'); OutputStream ut = blob.getBinaryOutputStream(); int count = -1, total = 0; byte[] data = new byte[(int)fin.available()]; fin.read(data); out.write(data); fin.close(); out.close(); pstmt.setBlob(1,blob); pstmt.setString(2,'50'); pstmt.executeUpdate(); pstmt.close(); conn.commit(); conn.close(); } catch (SQLException e) { System.err.println(e.getMessage()); e.printStackTrace(); } catch (IOException e) { System.err.println(e.getMessage()); } catch (Exception e){ e.printStackTrace();} }}以上回答2:
一般存路徑,小點(diǎn)就序列化
回答3:不建議直接把文件寫到數(shù)據(jù)庫里。你可以在數(shù)據(jù)庫記錄下文件的元數(shù)據(jù),以及在硬盤中的路徑。如果是海量的小文件,可以用hadoop hdfs的mapfile格式存儲。
回答4:數(shù)據(jù)庫里面一般存的都是文件的路徑
回答5:讀成二進(jìn)制,表字段也是二進(jìn)制。
回答6:可以了解一下mongodb
回答7:一般是把文件儲存在某個地方,然后把存儲地址放在數(shù)據(jù)庫中。不過小型文件的話,也可以使用二進(jìn)制碼的方式放入數(shù)據(jù)庫。
相關(guān)文章:
1. 為什么我ping不通我的docker容器呢???2. docker安裝后出現(xiàn)Cannot connect to the Docker daemon.3. 將SQLServer數(shù)據(jù)同步到MySQL 用什么方法?4. android - webview 自定義加載進(jìn)度條5. 并發(fā)模型 - python將進(jìn)程池放在裝飾器里為什么不生效也沒報(bào)錯6. numpy - python [:,2][:,None]是什么意思7. javascript - 微信小程序限制加載個數(shù)8. javascript - 微信小程序封裝定位問題(封裝異步并可能多次請求)9. javascript - 微信音樂分享10. python 怎樣用pickle保存類的實(shí)例?
