一般很少将二进制的数据,存到数据库里一般是加二年制的数据转换成指定的文件保存在,嗯文件目录里然后再,数据库里存储对应的存储地址,或者是,蒋,数据转换成其他类型存入到数据库。
在循化等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都做网站、网站设计 网站设计制作专业公司,公司网站建设,企业网站建设,成都品牌网站建设,成都全网营销,外贸网站建设,循化网站建设费用合理。
用ADO方式连的MYSQL, 已经用AppendChunk图片以二进制插入数据库中,下面是读取的不分,写入相反的方式
void DatabaseFile::GetImage()
{
_RecordsetPtr pRs = NULL;
pRs.CreateInstance(__uuidof(Recordset));
_variant_t varChunk;
_variant_t varBLOB;
try
{
CString sql = "select image from visdata where DetectTime = '2008-06-19 11:23:44';";
//Open a Recordset
HRESULT hr = pRs-Open(_variant_t("select * from visdata;"),_variant_t((IDispatch *) m_pConnection,true),adOpenKeyset,adLockOptimistic,adCmdText);
//read data
long lDataLength = pRs-Fields-GetItem("image")-ActualSize;//数据长度可以正确获取
varBLOB = pRs-GetFields()-GetItem("image")-GetChunk(lDataLength);
if(varBLOB.vt == (VT_ARRAY | VT_UI1))
{
BYTE *pBuf = NULL;
pBuf = (BYTE*)GlobalAlloc(GMEM_FIXED,lDataLength);//分配空间
hr = SafeArrayAccessData(varBLOB.parray,(void **)pBuf);
if (FAILED(hr))
return;
//Build a File
char tmpPath[_MAX_PATH+1];
GetCurrentDirectory( MAX_PATH,tmpPath);
CString strFileName = "\\temp2.bmp";//输出图片的文件名
strFileName = tmpPath+strFileName;
CFile outFile(strFileName,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary );
/*要判断文件是否创建成功*/
LPSTR buffer = (LPSTR)GlobalLock((HGLOBAL)(pBuf));
outFile.WriteHuge(buffer,lDataLength);
GlobalUnlock((HGLOBAL)pBuf);
outFile.Close();
SafeArrayUnaccessData (varBLOB.parray);
}
pRs-Close();
}
这有什么难的吗?
你把你的二进制数据可以转成文本串插入,就跟普通的插入一样啊。
import MySQLdb, cPickle
# Connect to a DB, e.g., the test DB on your localhost, and get a cursor
connection = MySQLdb.connect(db="test")
cursor = connection.cursor( )
# Make a new table for experimentation
cursor.execute("CREATE TABLE justatest (name TEXT, ablob BLOB)")
try:
# Prepare some BLOBs to insert in the table
names = 'aramis', 'athos', 'porthos'
data = { }
for name in names:
datum = list(name)
datum.sort( )
data[name] = cPickle.dumps(datum, 2)
# Perform the insertions
sql = "INSERT INTO justatest VALUES(%s, %s)"
for name in names:
cursor.execute(sql, (name, MySQLdb.escape_string(data[name])) )
# Recover the data so you can check back
sql = "SELECT name, ablob FROM justatest ORDER BY name"
cursor.execute(sql)
for name, blob in cursor.fetchall( ):
print name, cPickle.loads(blob), cPickle.loads(data[name])
finally:
# Done. Remove the table and close the connection.
cursor.execute("DROP TABLE justatest")
connection.close( )
二进制文件大的话,建议使用保存文件名(含路径)的方法!
文件小的话,用text即可,将二进制转换成16进制形式,读的时候再转换回去