python-连接数据库
from sqlalchemy import create_engine,text,Column,Integer,String,Sequence
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker Base = declarative_base() DB_CON_STR = 'mysql+mysqlconnector://root:123456@localhost/blog'
# DB_CON_STR = 'mysql+mysqlconnector://guijianliang:123456@localhost/blog'
engine =create_engine(DB_CON_STR,echo=True) Session = sessionmaker(bind=engine)
session = Session() #eg1 via class
#res = session.query(Product).filter(Product.id==1).one()
# res = session.query(Product).filter(text("id=1")).one() # print(res.id, res.name, res.count) class Product(Base):
__tablename__='entries' id = Column(Integer,Sequence('id'),primary_key=True)
name = Column(String)
count = Column(Integer) def __repr__(self):
return "<Product(id='%d',name='%s',count='%d')>"%(self.id,self.name,self.count) # print(res)
#eg2 via sql
sql = text("select * from entries")
res = session.execute(sql).fetchall()
print(res)
#
# for row in res:
# for col in row:
# print(col) session.close()
python-连接数据库的更多相关文章
- python连接数据库问题小结
在使用python连接数据库的时候遇到了这个问题: 大概意思就是在django的setting.py中配置的用户名和密码报错. 主要就是修改setting.py的配置 其中在里边的name和user项 ...
- 3.Python连接数据库PyMySQL
1.安装PyMySQL,输入命令:pip3 install PyMySQL 2.使用Navicat,创建数据库:TESTDB,表:EMPLOYEE,字段:FIRST_NAME,LAST_NAME,AG ...
- 数据测试001:利用python连接数据库插入excel数据
数据测试001:利用python连接数据库插入excel数据 最近在做数据测试,主要是做报表系统,需要往数据库插入数据验证服务逻辑,本次介绍如何利用python脚本插入Oracle和Mysql库中: ...
- python连接数据库自动发邮件
python连接数据库实现自动发邮件 1.运行环境 redhat6 + python3.6 + crontab + Oracle客户端 2.用到的模块 3.操作步骤 (1)安装python3.6参考 ...
- Python连接数据库流行用到的第三方库
Python连接数据库流行用到的第三方库: mysqldb:只支持Python2.x mysqlclient : mysqldb的衍生版本,完全兼容mysqldb,同时支持Python3.x,安装较复 ...
- Windows下Python连接数据库(mysql, mongodb)
一 实验平台 1 os: win7 64位旗舰版sp1 2 python: 2.7.10 x64 二 连接数据库 1 连接 mysql数据库 (1)下载mysql(5.6.25-winx64) 建议下 ...
- python连接数据库使用SQLAlchemy
参考python核心编程 ORM(Object Relational Mapper),如果你是一个更愿意操作Python对象而不是SQL查询的程序员,并且仍然希望使用关系型数据库作为你的后端,那么你可 ...
- python27期python连接数据库:
import pymysql创建connectinon对象:con = pymysql.connect(host = "localhost",user = "root&q ...
- python 连接数据库操作
import mysql #打开数据库连接(用户名,密码,数据库名) db = mysql.connect("localhost","testuser",&qu ...
- python 连接数据库-设置oracle ,mysql 中文字符问题
import cx_Oracle import MySQLdb def conn_oracle(): cnn = cx_Oracle.connect('用户名','密码','ip:端口号/数据库') ...
随机推荐
- web 服务器
作为一个跨专业转行的我来说,对后台一团浆糊,最近在看php,学的进度比较慢 (1)ApacheApache是世界使用排名第一的Web服务器软件.它可以运行在几乎所有广泛使用的计算机平台上.Apache ...
- 获取checked的值
<div class="rule-multi-porp"> <span> <%var itemList = PublicQuery.GetItemLi ...
- java 随机生成11位 组合
public static String generate8RateUuid() { String[] chars = new String[] { "a", & ...
- 让IE6 IE7 IE8 IE9 IE10 IE11支持Bootstrap的解决方法 转载
最近做一个Web网站,之前一直觉得bootstrap非常好,这次使用了bootstrap3,在chrome,firefox,safari,opera,360浏览器(极速模式).搜狗浏览器等浏览器下均没 ...
- mysql添加远程用户
-- 增加一个用户test1密码为abc,让他可以在任何主机上登录,并对所有数据库有查询.插入.修改.删除的权限. -- 首先用以root用户连入MYSQL,然后键入以下命令: grant selec ...
- LWP::UserAgent介绍2
#这个LWP::UserAgent一般要配合其他模块使用 #比如: #HTTP::Request #HTTP::Cookie #HTTP::Respose #HTTP::Status #LWP::Us ...
- Spring.net 学习IOC------准备
在学习spring.net开始时,我们首先要下载spring.net所用到的类库: Common.Logging.dll(必要)Spring.Core.dll(必要)Spring.Data.dllSp ...
- 在eclipse中生成实体类
1.在eclipse的windows中选中preferences在查询框中输入driver definition 2.点击add在Name/type中选中mysql jdbc driver 5.1然后 ...
- lr_abort()、exit(-1) 和 return-1之间的区别
int status; status = web_url("Login", "URL=https://secure.computing.com//login.asp?us ...
- ACM常用模板
数论: 中国剩余定理(互质与非互质通用版) ],r[]; int e_gcd(int a,int b,int &x,int &y) { ) { x=; y=; return a; } ...