zoukankan      html  css  js  c++  java
  • sqlalchemy soup raw sql update sql example

    #!/usr/bin/env python
    #encoding=utf-8
    from sqlalchemy.ext.sqlsoup import SqlSoup
    from sqlalchemy import *
    import datetime
    from elixir import *
    db = create_engine("mysql://root:1111t@localhost:3306/search?charset=utf8")#使用前首先要创建数据库
    metadata.bind = db
    metadata.bind.echo = True
    class Product(Entity):
        using_options(tablename='model_product')
        name                        = Field(Unicode(200))
        en_name    = Field(Unicode(200))
        price                       = Field(Float)
        en_price    = Field(Float)
        productid                   = Field(Unicode(200))
        site                        = Field(Unicode(30))
        link                        = Field(Unicode(300))
        smallImage                  = Field(Unicode(300))
        bigImage                    = Field(Unicode(300))
        description                 = Field(UnicodeText)
        en_description              = Field(UnicodeText)
        createdOn                   = Field(DateTime, default=datetime.datetime.now)
        modifiedOn                  = Field(DateTime, default=datetime.datetime.now)
        size                        = Field(Unicode(30))
        en_size                     = Field(Unicode(30))
        weight                      = Field(Unicode(30))
        en_weight                   = Field(Unicode(30))
        wrap                        = Field(Unicode(30))
        en_wrap                     = Field(Unicode(30))
        material                    = Field(Unicode(30))
        en_material                 = Field(Unicode(30))
        packagingCount              = Field(Unicode(30))
        stock                       = Field(Integer)
        location                    = Field(Unicode(30))
        en_location                 = Field(Unicode(30))
        popularity                  = Field(Integer)
        inStock                     = Field(Boolean)
        categories                  = Field(Unicode(30))
        def __repr__(self):
            return '<Global "%s" (%s)>' % (self.codepath, self.storepath)    
    setup_all()
    create_all()
    print "hello"
    #更新
    #session.query(Product).update({Product.en_name:None}) 
    #session.commit()
    #session.commit()
    #sql语句
    soup = SqlSoup(MetaData(db))
    rp=soup.bind.execute("select id from model_product")
    print len(rp.fetchall())
    print "done"

    Raw SQL

    SqlSoup works fine with SQLAlchemy's text block support.

    You can also access the SqlSoup's engine attribute to compose SQL directly. The engine's execute method corresponds to the one of a DBAPI cursor, and returns aResultProxy that has fetch methods you would also see on a cursor:

    >>> rp = db.bind.execute('select name, email from users order by name')
    >>> for name, email in rp.fetchall(): print name, email
    Bhargan Basepair basepair+nospam@example.edu
    Joe Student student@example.edu

    The docs for SqlSoup in the 0.3 branch are preserved at SqlSoup03. This page describes the 0.4 branch.

    Introduction

    SqlSoup provides a convenient way to access database tables without having to declare table or mapper classes ahead of time.

    Suppose we have a database with users, books, and loans tables (corresponding to the PyWebOff dataset, if you're curious). For testing purposes, we'll create this db as follows:

    >>> from sqlalchemy import create_engine
    >>> e = create_engine('sqlite:///:memory:')
    >>> for sql in _testsql: e.execute(sql) #doctest: +ELLIPSIS
    <...
    

    Creating a SqlSoup gateway is just like creating an SQLAlchemy engine:

    >>> from sqlalchemy.ext.sqlsoup import SqlSoup
    >>> db = SqlSoup('sqlite:///:memory:')
    

    or, you can re-use an existing metadata or engine:

    >>> db = SqlSoup(MetaData(e))
    

    You can optionally specify a schema within the database for your SqlSoup:

    # >>> db.schema = myschemaname
    http://www.rmunn.com/sqlalchemy-tutorial/tutorial.html
  • 相关阅读:
    yii2 qq邮箱配置发送
    dede tag标签静态化
    PHP5.5 mysqli如何连接MySQL数据库和读取数据
    Yii2搭建后台并实现rbac权限控制完整实例教程
    Linux系统下安装rz/sz命令及使用说明
    一个网站完整的SEO优化方案,方法,怎么做seo优化?
    史上最全的百度索引量下降原因分析及解决方案
    Dedecms手机站三种不同建设方法和优劣分析
    网络抓包wireshark
    2-文本新特性
  • 原文地址:https://www.cnblogs.com/lexus/p/1855402.html
Copyright © 2011-2022 走看看