久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

mysql - 文件存進(jìn)數(shù)據(jù)庫

瀏覽:120日期:2022-06-21 16:37:22

問題描述

例如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)文章:
主站蜘蛛池模板: 安龙县| 葵青区| 五家渠市| 呈贡县| 任丘市| 杭锦后旗| 九台市| 西乌| 正镶白旗| 澄迈县| 昌乐县| 沙湾县| 武宣县| 繁峙县| 达日县| 武冈市| 五常市| 保山市| 新闻| 黄浦区| 射洪县| 定边县| 嘉荫县| 淄博市| 汉源县| 五家渠市| 英超| 志丹县| 大方县| 伊宁市| 千阳县| 玛多县| 田阳县| 兰溪市| 锡林浩特市| 龙海市| 万安县| 天镇县| 二手房| 南昌县| 化隆|