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:端口号/数据库') ...
随机推荐
- could not resolve property问题(ssh框架)
could not resolve property不能解析属性问题, 刚开始把hql语句中的"from User user where user.user_name = '"+u ...
- 伪AP检测技术研究
转载自:http://www.whitecell-club.org/?p=310 随着城市无线局域网热点在公共场所大规模的部署,无线局域网安全变得尤为突出和重要,其中伪AP钓鱼攻击是无线网络中严重的安 ...
- 《HTML5权威指南》
<HTML5权威指南> HTML元素: html字符实体 html全局属性 html base标签 用元数据元素说明文档 标记文字(第八章) 标记文字.组织内容.文档分节 表格元素 表单元 ...
- .Net 第三方工具包整理
抓取数据 Jumony (http://www.jumony.net/)是一个基于 .NET 技术,用 C# 编写的一个 HTML 引擎,其可以用来分析解读 HTML 文档中的数据,也可以修改和绑定数 ...
- Process Monitor V2.96 (系统监视工具) 汉化免费绿色版
软件名称: Process Monitor V2.96 (系统监视工具) 汉化免费绿色版软件语言: 简体中文授权方式: 免费软件运行环境: Win7 / Vista / Win2003 / WinXP ...
- 【转】CSS
css概念 http://www.cnblogs.com/moveofgod/archive/2012/09/18/2691101.html css八大功能 http://developer.51ct ...
- 【翻译】编译Cordova项目
针对iOS创建项目 需要安装iOS SDK才能创建Workshop项目 打开终端工具并使用cd命令进入workshop目录执行下面都命令 cordova build ios 项目建立在workshop ...
- Activiti(工作流)学习资源总结
刚进公司第一个项目就是oa项目,不会activiti,只得自学,途中搜集到的activiti学习资源总结 1.activiti初体验 http://blog.csdn.net/bluejoe2000/ ...
- namenode无法启动
查看日志错误信息关键语句: There appears to be a gap in the edit log. We expected txid 44353, but got txid 原因: n ...
- centos 7 连接 xshell5
首先 保证你的centos版本与你选择的linux版本相同. 1.首先查看本机IP和网关 2.在centos7命令行下输入nmtui 进入 Edit a commection 选择Edit 按照刚 ...