搜索

Python开发torndb

May 28, 2017 am 10:02 AM

Torndb模块

  概要:torndb是一个轻量级的基于MySQLdb封装的一个模块,其是tornado框架的一部分。其项目主页为:https://github.com/bdarnell/torndb 。从tornado3.0版本以后,其已经作为一个独立模块发行了。可以通过easy_install 或pip的方式直接安装

1、连接数据库


# 创建连接
import torndb
# 数据库、账号、密码、时区
db = torndb.Connection("192.168.1.134:3306",'Jefrey',user='lzl',password='123456',time_zone='+8:00')


补充:

  • 默认字符集UTF8,没必要在加上 charset = "utf8"

  • 另外需要注意的是其默认时区为time_zone='+0:00'

  • 这个可以手动在连接的时候指定为东8区

 

2、创建数据表


# 创建
create='create table blog(id int,content text)'
db.execute(create)


 

3、插入数据

、execute


# 插入execute
# execute='insert into blog(id,content)values(%d,"%s")'%(7,'Jefrey5')
# db.execute(execute)


、insert


# 单条insert
insert =  'insert into blog(id,content)values(%s,%s)'
db.insert(insert,10,'Jefrey10')


、insertmany


# insertmany
insertmany =  'insert into blog(id,content)values(%s,%s)'
db.insertmany(insert,[[11,'Jefrey11'],[12,'Jefrey12']])


  

4、查询数据

、query查询


# Query查询
sql = &#39;SELECT * FROM blog WHERE content = %s AND id < %s&#39;
# 查询到一条数据时为列表
database = db.query(sql, &#39;Jefrey&#39;, 11)
# [{&#39;content&#39;: u&#39;Jefrey&#39;, &#39;id&#39;: 1L}]

# 查询到多条数据为也为列表
database5 = db.query(sql, &#39;Jefrey5&#39;, 11)
# [{&#39;content&#39;: u&#39;Jefrey5&#39;, &#39;id&#39;: 5L}, {&#39;content&#39;: u&#39;Jefrey5&#39;, &#39;id&#39;: 8L}, {&#39;content&#39;: u&#39;Jefrey5&#39;, &#39;id&#39;: 7L}]

# 查询数据不存在时为空列表
database10 = db.query(sql, &#39;Jefrey10&#39;, 11)
# []


、get查询


# Get查询
sql = &#39;SELECT * FROM blog WHERE content = %s AND id < %s&#39;
# 查询到一条数据时为字典
database = db.get(sql, &#39;Jefrey&#39;, 11)
# {&#39;content&#39;: u&#39;Jefrey&#39;, &#39;id&#39;: 1L}

# 查询到多条数据时报错
database5 = db.get(sql, &#39;Jefrey5&#39;, 11)
# Exception: Multiple rows returned for Database.get() query

# 查询数据不存在时为None
database10 = db.get(sql, &#39;Jefrey10&#39;, 11)
# None


  

 

以上是Python开发torndb的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
Python:编译器还是解释器?Python:编译器还是解释器?May 13, 2025 am 12:10 AM

Python是解释型语言,但也包含编译过程。1)Python代码先编译成字节码。2)字节码由Python虚拟机解释执行。3)这种混合机制使Python既灵活又高效,但执行速度不如完全编译型语言。

python用于循环与循环时:何时使用哪个?python用于循环与循环时:何时使用哪个?May 13, 2025 am 12:07 AM

useeAforloopWheniteratingOveraseQuenceOrforAspecificnumberoftimes; useAwhiLeLoopWhenconTinuingUntilAcIntiment.ForloopSareIdeAlforkNownsences,而WhileLeleLeleLeleLoopSituationSituationSituationsItuationSuationSituationswithUndEtermentersitations。

Python循环:最常见的错误Python循环:最常见的错误May 13, 2025 am 12:07 AM

pythonloopscanleadtoerrorslikeinfiniteloops,modifyingListsDuringteritation,逐个偏置,零indexingissues,andnestedloopineflinefficiencies

对于循环和python中的循环时:每个循环的优点是什么?对于循环和python中的循环时:每个循环的优点是什么?May 13, 2025 am 12:01 AM

forloopsareadvantageousforknowniterations and sequests,供应模拟性和可读性;而LileLoopSareIdealFordyNamicConcitionSandunknowniterations,提供ControloperRoverTermination.1)forloopsareperfectForeTectForeTerToratingOrtratingRiteratingOrtratingRitterlistlistslists,callings conspass,calplace,cal,ofstrings ofstrings,orstrings,orstrings,orstrings ofcces

Python:深入研究汇编和解释Python:深入研究汇编和解释May 12, 2025 am 12:14 AM

pythonisehybridmodelofcompilationand interpretation:1)thepythoninterspretercompilesourcececodeintoplatform- interpententbybytecode.2)thepytythonvirtualmachine(pvm)thenexecuteCutestestestesteSteSteSteSteSteSthisByTecode,BelancingEaseofuseWithPerformance。

Python是一种解释或编译语言,为什么重要?Python是一种解释或编译语言,为什么重要?May 12, 2025 am 12:09 AM

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允许fordingfordforderynamictynamictymictymictymictyandrapiddefupment,尽管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

对于python中的循环时循环与循环:解释了关键差异对于python中的循环时循环与循环:解释了关键差异May 12, 2025 am 12:08 AM

在您的知识之际,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations则youneedtoloopuntilaconditionismet

循环时:实用指南循环时:实用指南May 12, 2025 am 12:07 AM

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器