Python Opearte SQLAlchemy Do Something
近段时间在看SQLAlchemy,总之万事开头难,但是么办法。
Database Urls
The create_engine() function produces an Engine object based on a URL. These URLs follow RFC-1738, and usually can include username, password, hostname, database name as well as optional keyword arguments for additional configuration. In some cases a file path is accepted, and in others a “data source name” replaces the “host” and “database” portions. The typical form of a database URL is:
dialect+driver://username:password@host:port/database 标准连接数据库规范
MS-SQL连接案例
Microsoft SQL Server
The SQL Server dialect uses pyodbc as the default DBAPI. pymssql is also available:
# pyodbc
engine = create_engine('mssql+pyodbc://scott:tiger@mydsn')
# pymssql
engine = create_engine('mssql+pymssql://scott:tiger@hostname:port/dbname')
More notes on connecting to SQL Server at Microsoft SQL Server.
这里面测试用的是pyodbc进行连接的,分两种
engine=create_engine("mssql+pyodbc://sa:@192.168.6.112:1433/FactoryHome?driver=SQL+Server+Native+Client+10.0")
还有一种就是通过微软的dsn进行连接,如不知道dsn连接,可以百度一下看看是什么意思
对数据的插入
from sqlalchemy import *
engine=create_engine("mssql+pyodbc://sa:@192.168.6.112:1433/FactoryHome?driver=SQL+Server+Native+Client+10.0")
metadata=MetaData()
Table_1=Table("Table_1",metadata,
Column("Code",String(10)),Column("Name",String(10)))
ins=Table_1.insert().values(Code='cccccc',Name='王二')
conn=engine.connect()
result=conn.execute(ins)
参数化的形式,感觉有点感觉比拼接SQL来的快。
result=conn.execute(Table_1.insert(),Code='kkkkk',Name='网易')
对于给定的参数也可以这样传值。
对于数据的查询,也必须的先构造一个TABLE,然后对应的字段进行查询
from sqlalchemy import *
engine=create_engine("mssql+pyodbc://sa:@192.168.6.112:1433/FactoryHome?driver=SQL+Server+Native+Client+10.0")
metadata=MetaData()
Table_1=Table("Table_1",metadata,
Column("Code",String(10)),Column("Name",String(10)))
conn=engine.connect()
result=conn.execute(select([Table_1]))
for row in result:
print(row)
SQLAlchemy最好的方式就是能像SQL语句一样能实现join连接查询
>>> s = select([users, addresses]).where(users.c.id == addresses.c.user_id) SQL>>> for row in conn.execute(s): ... print(row)
这样可以通过相关表的关联就能查询数据。
有好多东西,再叙。
Python Opearte SQLAlchemy Do Something的更多相关文章
- 基于Python的SQLAlchemy的操作
安装 在Python使用SQLAlchemy的首要前提是安装相应的模块,当然作为python的优势,可以到python安装目录下的scripts下,同时按住shift+加上鼠标左键,从而在菜单中打开命 ...
- SQLAlchemy(1) -- Python的SQLAlchemy和ORM
Python的SQLAlchemy和ORM(object-relational mapping:对象关系映射) web编程中有一项常规任务就是创建一个有效的后台数据库.以前,程序员是通过写sql语句, ...
- python使用sqlalchemy连接pymysql数据库
python使用sqlalchemy连接mysql数据库 字数833 阅读461 评论0 喜欢1 sqlalchemy是python当中比较出名的orm程序. 什么是orm? orm英文全称objec ...
- python之SQLAlchemy
ORM介绍 orm英文全称object relational mapping,就是对象映射关系程序,简单来说我们类似python这种面向对象的程序来说一切皆对象,但是我们使用的数据库却都是关系型的,为 ...
- Python’s SQLAlchemy vs Other ORMs[转发 7] 比较结论
Comparison Between Python ORMs For each Python ORM presented in this article, we are going to list t ...
- Python’s SQLAlchemy vs Other ORMs[转发 6]SQLAlchemy
SQLAlchemy SQLAlchemy is an open source SQL toolkit and ORM for the Python programming language rele ...
- Python’s SQLAlchemy vs Other ORMs[转发 3]Django's ORM
Django's ORM Django is a free and open source web application framework whose ORM is built tightly i ...
- Python’s SQLAlchemy vs Other ORMs[转发 2]Storm
Storm Storm is a Python ORM that maps objects between one or more databases and Python. It allows de ...
- Python’s SQLAlchemy vs Other ORMs[转发 0]
原文地址:http://pythoncentral.io/sqlalchemy-vs-orms/ Overview of Python ORMs As a wonderful language, Py ...
随机推荐
- javascript实现继承的方式
this this表示当前对象,如果在全局作用范围内使用this,则指代当前页面对象window: 如果在函数中使用this,则this指代什么是根据运行时此函数在什么对象上被调用. 我们还可以使用a ...
- POJ 2376 Cleaning Shifts 贪心
Cleaning Shifts 题目连接: http://poj.org/problem?id=2376 Description Farmer John is assigning some of hi ...
- JS可以做什么,它的能力范围 View----------Request/Submit------------------Server
View----------Request/Submit------------------Server javascript--------><script>标签方式(页面,动态插 ...
- [Angular2 Router] Optional Route Query Parameters - The queryParams Directive and the Query Parameters Observable
In this tutorial we are going to learn how to use the Angular 2 router to pass optional query parame ...
- HDU 5019 Revenge of GCD(数学)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5019 Problem Description In mathematics, the greatest ...
- cocos2dx 读取json及解析
ball.json 数据例如以下: { "entities": [ { "entity": { "TapOpposite": 0, &quo ...
- ubuntu在xampp下安装memcache扩展
sudo wget http://pecl.php.net/get/memcache-2.2.1.tgz sudo tar vxzf memcache-2.2.1.tgz cd memcache-2. ...
- boost.asio源码剖析(二) ---- 架构浅析
* 架构浅析 先来看一下asio的0层的组件图. (图1.0) io_object是I/O对象的集合,其中包含大家所熟悉的socket.deadline_tim ...
- oc-23-static
#import <Foundation/Foundation.h> #import "Person.h" int main(int argc, const char * ...
- JavaWeb学习总结
http://www.cnblogs.com/xdp-gacl/tag/JavaWeb%E5%AD%A6%E4%B9%A0%E6%80%BB%E7%BB%93/ http://www.cnblogs. ...