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. 在應(yīng)用配置文件 app.php 中找不到’route_check_cache’配置項2. html按鍵開關(guān)如何提交我想需要的值到數(shù)據(jù)庫3. HTML 5輸入框只能輸入漢字、字母、數(shù)字、標(biāo)點符號?正則如何寫?4. javascript - 請教如何獲取百度貼吧新增的兩個加密參數(shù)5. gvim - 誰有vim里CSS的Indent文件, 能縮進@media里面的6. 跟著課件一模一樣的操作使用tp6,出現(xiàn)了錯誤7. PHP類屬性聲明?8. javascript - JS請求報錯:Unexpected token T in JSON at position 09. objective-c - ios 怎么實現(xiàn)微信聯(lián)系列表 最好是swift10. java - 安卓接入微信登錄,onCreate不會執(zhí)行
