Git Product home page Git Product logo

python-tool's Introduction

python-mysql

python操作mysql,基本的curd操作,连贯操作,基于MySQLdb。如果语句复杂可以执行原生sql语句。

使用说明

依赖

python2.7.6,需要安装MySQLdb

使用示例

引入文件

from Db import mysql

实例化一个连接

test_db = {"host": "localhost", "user": "root", "passwd": "123456", "db": "dev", "port": "3306", "charset": "utf8"}
# 默认端口是3306,默认编码是utf-8。可以不指定,更多配置信息参考MySQLdb
mysql = mysql(**test_db)

# OR
# mysql = mysql(host='localhost', user='root', passwd='123456', db='dev', port='3306', charset='utf8')

查询一条记录

所有操作都必须调用table()

print mysql.table("test").selectOne()

条件查询

print mysql.table("test").where("name='asd'").selectALl()

选择字段

print mysql.table("test").selectAll('name')

更新

mysql.table("test").where("name='asd'").update("point=100")

插入

# data为数据关联的dict
data = {"name": "John", "point": 98}
mysql.table("test").table("test").insert(data)

批量插入

# data为数据关联的list
data = [{"name": "Lilei", "point": 69}, {"name": "xiaohong", "point": 58}]
mysql.table("test").insert_batch(data)

删除

mysql.table("test").delete()

limit

print mysql.table("test").limit(0,10).selectAll()
# 转化为mysql语句为
# SELECT * FROM test WHERE 1=1 LIMIT 0, 10;

order by

print mysql.table("test").order('point desc').selectAll()
# 转化为mysql语句为
# SELECT * FROM test WHERE 1=1 ORDER BY point DESC;

执行原生mysql

mysql.query("SELECT * FROM test WHERE name = 'asd' ")

python-tool's People

Contributors

asdcu avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.