社区
数据库及相关技术
帖子详情
如何把任意文件存入数据库问题
Redball
2003-08-29 08:31:16
文件都是二进制的,内容存入一个单元,文件名存入一个单元,不就实现了文件在数据库中的存放了么。
这个思想具体怎么实现阿?
...全文
28
7
打赏
收藏
如何把任意文件存入数据库问题
文件都是二进制的,内容存入一个单元,文件名存入一个单元,不就实现了文件在数据库中的存放了么。 这个思想具体怎么实现阿?
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
7 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
zzhong2
2003-08-29
打赏
举报
回复
数据类型一定要是image
以下是以存图片为例,实际应用中,读什么文件存什么文件
存jpeg到SQL数据库:
if(OpenPictureDialog1->Execute())
{
ADOQuery1->Edit();
TBlobField *Field = (TBlobField*)ADOQuery1->FieldByName("photo");
Field->LoadFromFile(OpenPictureDialog1->FileName);
ADOQuery1->Post();
}
//以下是从数据库读jpeg,并显示在TDBImage控件中
#include <clipbrd.hpp>
TStream *Stream1;
TJPEGImage *Pjp;
Pjp=new TJPEGImage();
ADOQuery1->Open();
try
{
Stream1=ADOQuery1->CreateBlobStream(ADOQuery1->FieldByName("treenodes"), bmRead);//treenodes是存放jpeg内容的字段,它的类型一定要用image
Pjp->LoadFromStream(Stream1);
//Image2是TDBImage组件,它的DateSource,和FieldName属性要空着
Image2->Picture->Bitmap->Assign(Pjp);
delete Stream1;
}
__finally
{
ADOQuery1->Close();
delete Pjp;
}
下面是别人的更好的方法,可存各种图形
以下是讀出各種類型的圖片的程序,支持ADO,BDE或TClientDataSet
#define PICTURE_MAP__(TBit) {TBit *PG = new TBit(); \
try {PG->LoadFromStream(TmpStream);\
Pic->Assign(PG); \
}\
catch(...)\
{delete PG ;\
return false;\
}\
delete PG;\
}
//----------------------------------------------------------------
//該模板將二進制字段中的圖像(GIF或JPG等等)使用Assign方法轉為TPicture,TBitmap等等。
template <class T >
bool LoadPhotoFromField(TField *F_Photo,const AnsiString Format,T *Pic)
{if(!F_Photo->DataSet->Active) return false ;
if(F_Photo->IsNull) return false ;
else
{TStream *TmpStream = F_Photo->DataSet->CreateBlobStream(F_Photo,bmRead);
if(Format == ".JPG" || Format == ".JPEG")PICTURE_MAP__(TJPEGImage )
else if(Format == ".BMP") PICTURE_MAP__(Graphics::TBitmap)
// else if(Format == ".GIF") PICTURE_MAP__(TGIFImage )
else if(Format == ".ICO") PICTURE_MAP__(TIcon)
else if(Format == ".WMF" || Format ==".EMF") PICTURE_MAP__(TMetafile)
else return false ;
}
return true;
}
#undef PICTURE_MAP__(TBit)
//如果要支持GIF,那你要安裝支持GIF的VCL類。
支持多種格式
存入:
if(OpenPictureDialog1->Execute())
{DataSet->Edit();
TBlobField *Field = (TBlobField*)DataSet->FieldByName("photo");
Field->LoadFromFile(OpenPictureDialog1->FileName);
DataSet->FieldByName("photoFormat")->AsString =
ExtractFileExt(OpenPictureDialog1->FileName).UpperCase();
DataSet->Post();
}
Robin
2003-08-29
打赏
举报
回复
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
ADOTable1->Insert();
if(OpenPictureDialog1->Execute())
{
Edit1->Text=OpenPictureDialog1->FileName;
Image1->Picture->LoadFromFile(OpenPictureDialog1->FileName);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
TMemoryStream *stream = new TMemoryStream();
Image1->Picture->Bitmap->SaveToStream(stream);
ADOTable1->Insert();
ADOTable1->FieldByName("title")->AsString=Edit1->Text;
((TGraphicField *)(ADOTable1->FieldByName("content")))->LoadFromStream(stream);
ADOTable1->Post();
delete stream;
}
//---------------------------------------------------------------------------
Robin
2003-08-29
打赏
举报
回复
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
#include <Dialogs.hpp>
#include <ExtDlgs.hpp>
#include <ADODB.hpp>
#include <DB.hpp>
#include <DBCtrls.hpp>
#include <Mask.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
TButton *Button2;
TOpenPictureDialog *OpenPictureDialog1;
TADOTable *ADOTable1;
TWideStringField *ADOTable1title;
TBlobField *ADOTable1content;
TEdit *Edit1;
TImage *Image1;
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
Robin
2003-08-29
打赏
举报
回复
回答的积极也有问题啊!
Redball
2003-08-29
打赏
举报
回复
flyinger(琉璃翡翠) :怎么我的贴你回的最积极阿
我就是问怎么实现,代码~!代码~!!或伪码也行。
binbin
2003-08-29
打赏
举报
回复
没什么问题,用blob字段就可以了.
用流来和blob字段读写内容.
Robin
2003-08-29
打赏
举报
回复
大哥!
可以啊!
怎么了,有问题吗!
1小时入门最新小程序授权登录注册功能开发,并把注册记录添加到
数据库
新版微信小程序授权登录,退出,缓存,小程序个人中心的实现 如果把用户授权的信息
存入
Mysql/云开发的
数据库
,怎么实现呢? 授权登录对于不同的用户好像没有唯一的标识,是不是还得获取openid才行? 授权登录必须从...
如何将爬取的数据
存入
数据库
中
这里我就将自己前面爬取的豆瓣电影top 250的数据
存入
数据库
中吧,爬数据的代码我就不再写了,如果有需要可以参考我这篇文章
Python_爬虫数据
存入
数据库
(超详细过程
目录一、新建项目二、程序的编写三、数据的爬取1.在cmd窗口输入scrapy startproject [项目名称] 创建爬虫项目接着创建爬虫
文件
,scrapy genspider [爬虫名字] [爬虫域名]打开pycharm...4.在
数据库
中新建数据表MyArticle
将爬虫爬到的数据
存入
MySQL
数据库
先要在
数据库
中建立相对于的table(表),然后将爬到的数据
存入
表中就可以了,如下是用类实现的某电影网站爬到的电影数据
存入
数据库
的过程 import requests import csv from lxml import etree import pymysql ...
将 Session 数据
存入
数据库
将 Session 数据
存入
数据库
由于Session数据数组是附带一个Session ID保存在用户cookie里的,你无法验证它,除非你把session数据存储在
数据库
中。在一些不需要或很少需要安全保护的应用中,session ID 或许并不...
数据库及相关技术
1,178
社区成员
18,939
社区内容
发帖
与我相关
我的任务
数据库及相关技术
C++ Builder 数据库及相关技术
复制链接
扫一扫
分享
社区描述
C++ Builder 数据库及相关技术
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章