CSDN-CSDN社区-其他开发语言-脚本语言(Perl ,Python)

收藏 python往mysql的blob字段写入二进制数据,怎么做????[问题点数:20,结帖人:longxj04]

  • longxj04
  • (lxj)
  • 等 级:
  • 结帖率:
楼主发表于:2008-03-19 11:38:16
最好有示例代码,急着用
回复次数:2
  • iihero用户头像
  • iihero
  • (iihero)
  • 等 级:
#1楼 得分:2回复于:2008-03-19 16:30:04
这有什么难的吗?
你把你的二进制数据可以转成文本串插入,就跟普通的插入一样啊。
#2楼 得分:18回复于:2008-03-19 23:57:21
Python Cookbook, 2nd Edition
Recipe 7.10. Storing a BLOB in a MySQL Database

Python code
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( )

相关问题
问如何使用perl来操作mysql对图片的操作? 其他开发语言/ 脚本语言 ...
如何将图片存入postgresql中