English 中文(简体)
• 如何在MySQL db中用假装插入/检索作为BLOB储存的档案
原标题:How to insert / retrieve a file stored as a BLOB in a MySQL db using python
  • 时间:2009-08-18 14:50:58
  •  标签:

我想写一本有某些信息的书状。 我的表格中有一个栏目是,我想为每个条目节省一个档案。

我如何阅读档案(综合),并用假日插入该档案。 同样,我如何取回,并写回硬车上某些任意地点?

最佳回答
thedata = open( thefile ,  rb ).read()
sql = "INSERT INTO sometable (theblobcolumn) VALUES (%s)"
cursor.execute(sql, (thedata,))

That code of course works as written only if your table has just the BLOB column and what you want to do is INSERT, but of course you could easily tweak it to add more columns, use UPDATE instead of INSERT, or whatever it is that you exactly need to do.

I m also assuming your file is binary rather than text, etc; again, if my guesses are incorrect it s easy for you to tweak the above code accordingly.

Some kind of SELECT on cursor.execute, then some kind of fetching from the cursor, is how you retrieve BLOB data, exactly like you retrieve any other kind of data.

问题回答

您可插入并读出行文中与所有其他一栏一样的行文。 从数据库APICS的角度来看,对BLOBs没有任何特殊意义。





相关问题
热门标签