如何利用數(shù)據(jù)庫儲(chǔ)存圖檔_MySQL
Jun 01, 2016 pm 02:06 PM
在 SQL Server 當(dāng)中有一款資料類型號(hào)作 Image , 除了可以儲(chǔ)存圖檔外它還可以儲(chǔ)存大型的二進(jìn)位資料檔, 對(duì)這一個(gè)欄位大部分的人是聽過但是不知影按怎來用, 今日的文章就要來討論如何將圖檔存入去資料庫
準(zhǔn)備工作
為了降低這篇文章的篇幅及複雜度, 咱決定借用 Upload 元件來替我們完成檔案上傳的工作, 所要使用的是 Dundas 所提供免錢的上傳元件, 請(qǐng)到下底的網(wǎng)址下載 Dundas Upload 元件並安裝
http://www.dundas.com/
創(chuàng)造資料表
在這個(gè)例咱要用到 SQL 內(nèi)建的 Pubs 資料庫來作測(cè)試, 請(qǐng)打開 QA 然後執(zhí)行下底的創(chuàng)造資料表指令, 所要建立的資料表中一個(gè)欄位是紀(jì)錄檔案的 Content-Type, 另一個(gè)則是儲(chǔ)存圖檔
Use Pubs
Create Table ImgData
(
ImgID Int Identity Not Null Primary Key,
ContentType VarChar(20),
FileData Image
)
HTML 表單部分
現(xiàn)在來看看 HTML 表單的部分, 因?yàn)槭怯米鰴n案上傳因此用 enctype="multipart/form-data" , 不過要注意的是一但使用了 form-data 後表單資料的取得也就不能再用 Request.Form, 因?yàn)檫@不是這篇文章的重點(diǎn)所以在這就不多做解釋, 請(qǐng)將下底的碼存成 insert.htm
程式碼
擱來看麥 ASP 的部分, 請(qǐng)將下底的碼存成 insert.asp
Response.Buffer = True
ConnStr = "Provider=SQLOLEDB;" _
& "Data Source=你的電腦名稱;" _
& "Initial Catalog=Pubs;" _
& "User Id=sa;" _
& "Password=你的密碼"
'建立 oUpload 上傳物件
Set oUpload = Server.CreateObject("Dundas.Upload.2")
'在使用 oUpload 集合 (Collection) 前, 要先呼叫 Save 或 SaveToMemory 方法
oUpload.SaveToMemory
Set oRs = Server.CreateObject("Adodb.Recordset")
oRs.Open "ImgData", ConnStr, 2, 3
oRs.AddNew
'呼叫 oUpload 物件的 ContentType, Binary 屬性, 已取得我們要的資料
oRs("ContentType").Value = oUpload.Files(0).ContentType
oRs("FileData").Value = oUpload.Files(0).Binary
oRs.Update
oRs.Close
Set oRs = Nothing
%>
頂高的程式假設(shè)你只上傳一個(gè)檔案, 所以使用 oUpload.Files(0), 如果你一次上傳一個(gè)以上的檔案, 你可以將程式小改為
...
oRs.Open ...
For Each oFile In oUpload.Files
If InStr(1,oFile.ContentType,"image") 0 Then
oRs.AddNew
oRs("ContentType").Value = oFile.ContentType
oRs("imgdata").Value = oFile.Binary
End If
Next
oRs.Update
...
現(xiàn)在你可以利用瀏覽器開啟 Insert.htm 來進(jìn)行上傳圖檔到資料庫的動(dòng)作, 執(zhí)行完後你可以 Select ImgData 資料表, 應(yīng)該是出現(xiàn)一筆資料, 不過 FileData 欄位應(yīng)該是看不懂的啦!
今日的文章就先介紹到這, 下一篇文章再來介紹如何將圖檔從資料庫中拉出來!
希望這篇文章對(duì)你有幫助!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

Using the database callback function in Golang can achieve: executing custom code after the specified database operation is completed. Add custom behavior through separate functions without writing additional code. Callback functions are available for insert, update, delete, and query operations. You must use the sql.Exec, sql.QueryRow, or sql.Query function to use the callback function.

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.
