Python torndoa mysql 模块安装

时间:2023-03-08 21:26:24

pip install torndb

pip install pip install mysql-python #不支持3.x版本

ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/libmysqlclient.so.18    #64为系统

ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib/libmysqlclient.so.18    #32位系统

import torndb
db=torndb.Connection(hostaddress,database name,user,password)
import MySQLdb
conn = MySQLdb.connect(host='localhost',port=3306,user='root',passwd='walleleego',db='demo1')

pip install -i http://pypi.douban.com/simple/  torndb

所以以后pypi站索引无法访问的时候尝试利用豆瓣的pypi索引。

使用:

    连接数据库  

1
2
import torndb
db=torndb.Connection(hostaddress,database name,user,password)

  查询

查询有两种查询方式,一种为get,一种为query,get是得到一行数据。query是得到一列数据。get返回数据为封装好的dict,query得到的数据为封装好的list,单元为dict。

1
2
3
>>> a=db.get('select * from query where id=1')
>>> a
{'queryc''dac''id'1}

  

1
2
3
>>> a=db.query('select * from query')
>>> a
[{'id'2'queryc''isca'}, {'id'1'queryc''dac'}]

  执行sql语句

下面的命令是无返回参数的执行sql语句的方法。

1
2
3
string='dac'
str='insert into query(id,queryc)values(%d,"%s")'%(1,string)
db.execute(exe)
  • MySQL命令行操作创建数据库和表的时候指定编码的命令

数据库

1
mysql> CREATE DATABASE IF NOT EXISTS my_db DEFAULT CHARSET utf8 COLLATE utf8_general_ci;

  数据表

1
mysql> CREATE TABLE my_table (name VARCHAR(20) NOT NULL) type=MyISAM DEFAULT CHARSET utf8;