国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

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

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

瀏覽:123日期: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:

一般存路徑,小點就序列化

回答3:

不建議直接把文件寫到數(shù)據(jù)庫里。你可以在數(shù)據(jù)庫記錄下文件的元數(shù)據(jù),以及在硬盤中的路徑。如果是海量的小文件,可以用hadoop hdfs的mapfile格式存儲。

回答4:

數(shù)據(jù)庫里面一般存的都是文件的路徑

回答5:

讀成二進制,表字段也是二進制。

回答6:

可以了解一下mongodb

回答7:

一般是把文件儲存在某個地方,然后把存儲地址放在數(shù)據(jù)庫中。不過小型文件的話,也可以使用二進制碼的方式放入數(shù)據(jù)庫。

相關(guān)文章:
主站蜘蛛池模板: 介休市| 乃东县| 尚志市| 嘉荫县| 庆安县| 天气| 丰都县| 临高县| 通榆县| 来宾市| 万荣县| 永登县| 马关县| 怀来县| 集贤县| 遵化市| 诸城市| 甘南县| 墨脱县| 舒城县| 朔州市| 宁阳县| 西安市| 红原县| 肥乡县| 沙洋县| 萨嘎县| 光山县| 南阳市| 曲靖市| 缙云县| 大悟县| 会昌县| 新蔡县| 中西区| 翁牛特旗| 丰都县| 特克斯县| 定结县| 朝阳区| 云南省|