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 ...
随机推荐
- Nginx (基于linux)综合
重启Nginx服务:centos:测试NGINX配置文件是否有效:/usr/local/nginx/sbin/nginx -t 平滑重启:/usr/local/nginx/sbin/nginx -s ...
- Codeforces Round #146 (Div. 1) B. Let's Play Osu! dp
B. Let's Play Osu! 题目连接: http://www.codeforces.com/contest/235/problem/B Description You're playing ...
- Codeforces Gym 100231B Intervals 线段树+二分+贪心
Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...
- BZOJ 4318: OSU! 期望DP
4318: OSU! 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4318 Description osu 是一款群众喜闻乐见的休闲软件 ...
- UVA 573 (13.08.06)
The Snail A snail is at the bottom of a 6-foot well and wants to climb to the top.The snail can cl ...
- GridView编辑删除操作
第一种:使用DataSource数据源中自带的编辑删除方法,这样的不经常使用,在这里就不加说明了. 另外一种:使用GridView的三种事件:GridView1_RowEditing(编辑).Grid ...
- iOS开发——数据持久化OC篇&plist文件增删改查操作
Plist文件增删查改 主要操作: 1.//获得plist路径 -(NSString*)getPlistPath: 2.//判断沙盒中名为plistname的文件是否存在 -(BOOL ...
- 清除SQL Server 2008中登陆时的历史记录
win7 在地址栏直接输入下面路径,删除SqlStudio.bin文件%AppData%\Microsoft\Microsoft SQL Server\100\Tools\Shell
- SQL SERVER中查找某关键词位于哪些存储过程或函数
USE [MYDB] go SELECT * FROM INFORMATION_SCHEMA.ROUTINES WHERE routine_type='PROCEDURE' AND routine_d ...
- oc-18-继承
//Animal.h #import <Foundation/Foundation.h> @interface Animal : NSObject { int _age; // 不写@pu ...