mysql - 文件存進數(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:
一般存路徑,小點就序列化
回答3:不建議直接把文件寫到數(shù)據(jù)庫里。你可以在數(shù)據(jù)庫記錄下文件的元數(shù)據(jù),以及在硬盤中的路徑。如果是海量的小文件,可以用hadoop hdfs的mapfile格式存儲。
回答4:數(shù)據(jù)庫里面一般存的都是文件的路徑
回答5:讀成二進制,表字段也是二進制。
回答6:可以了解一下mongodb
回答7:一般是把文件儲存在某個地方,然后把存儲地址放在數(shù)據(jù)庫中。不過小型文件的話,也可以使用二進制碼的方式放入數(shù)據(jù)庫。
相關(guān)文章:
1. javascript - axios請求回來的數(shù)據(jù)組件無法進行綁定渲染2. javascript - avalon使用:duplex設(shè)置select默認(rèn)option的bug3. python - pandas dataframe如何對某列的空數(shù)據(jù)位置進行update?update的函數(shù)是自定義的,參數(shù)是同一行的另外兩列數(shù)據(jù)4. html5 - 請問現(xiàn)在主流的前端自動化構(gòu)建工具是哪個?5. javascript - jQuery post()方法,里面的請求串可以轉(zhuǎn)換為GBK編碼么?可以的話怎樣轉(zhuǎn)換?6. java中返回一個對象,和輸出對像的值,意義在哪兒7. css3 - 純css實現(xiàn)點擊特效8. 安全性測試 - nodejs中如何防m(xù)ySQL注入9. javascript - 關(guān)于apply()與call()的問題10. javascript - 有適合開發(fā)手機端Html5網(wǎng)頁小游戲的前端框架嗎?
