首页 新闻 论坛 群组 Blog 文档 下载 读书 Tag 网摘 搜索 .NET Java 游戏 视频 人才 外包 培训 数据库 书店 程序员
中国软件网
欢迎您:游客 | 登录 注册 帮助
  • python往mysql的blob字段写入二进制数据,怎么做???? [已结帖,结帖人:longxj04]
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • longxj04
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    • 结帖率:
    发表于:2008-03-19 11:38:16 楼主
    最好有示例代码,急着用
    20  修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • iihero
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-03-19 16:30:041楼 得分:2
    这有什么难的吗?
    你把你的二进制数据可以转成文本串插入,就跟普通的插入一样啊。
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • iambic
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    • 5

      5

      6

    发表于:2008-03-19 23:57:212楼 得分:18
    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( )

    修改 删除 举报 引用 回复