加载中…
个人资料
bicloud
bicloud
  • 博客等级:
  • 博客积分:0
  • 博客访问:378,075
  • 关注人气:492
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
相关博文
推荐博文
谁看过这篇博文
加载中…
正文 字体大小:

python连接mysql

(2011-05-11 17:39:12)
标签:

python

mysql

it

分类: python
CREATE DATABASE `menu`;
USE menu;
DROP TABLE IF EXISTS `fish`;
SET @saved_cs_client = @@character_set_client;
SET character_set_client = utf8;
CREATE TABLE `fish` (
`ID` int(11) NOT NULL auto_increment,
`NAME` varchar(30) NOT NULL default ‘’,
`PRICE` decimal(5,2) NOT NULL default ‘0.00’,
PRIMARY KEY (`ID`)
) ENGINE=MyISAM AUTO_INCREMENT=27 DEFAULT CHARSET=latin1;
SET character_set_client = @saved_cs_client;
LOCK TABLES `fish` WRITE;
INSERT INTO `fish` VALUES (1,’catfish’,’8.50’),(2,’catfish’,’8.50’),(3,’tuna’,’8.00’),(4,’catfish’,’5.00’),(5,’bass’,’6.75’),(6,’haddock’,’6.50’),(7,’salmon’,’9.50’),(8,’trout’,’6.00’),(9,’tuna’,’7.50’),(10,’yellowfin tuna’,’12.00’),(11,’yellowfin tuna’,’13.00’),(12,’tuna’,’7.50’);
UNLOCK TABLES;


mysql> show tables;
+----------------+
| Tables_in_menu |
+----------------+
| fish           |
+----------------+
1 row in set (0.00 sec)

mysql> select * from fish;
+----+----------------+-------+
| ID | NAME           | PRICE |
+----+----------------+-------+
1 | catfish        8.50 |
2 | catfish        8.50 |
3 | tuna           8.00 |
4 | catfish        5.00 |
5 | bass           6.75 |
6 | haddock        6.50 |
7 | salmon         9.50 |
8 | trout          6.00 |
9 | tuna           7.50 |
| 10 | yellowfin tuna | 12.00 |
| 11 | yellowfin tuna | 13.00 |
| 12 | tuna           7.50 |
+----+----------------+-------+
12 rows in set (0.03 sec)

mysql> describe fish;
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| ID    | int(11)      | NO   | PRI | NULL    | auto_increment |
| NAME  | varchar(30)  | NO                           |
| PRICE | decimal(5,2) | NO      | 0.00                  |
+-------+--------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)

mysql> select * from fish where id = 5;
+----+------+-------+
| ID | NAME | PRICE |
+----+------+-------+
5 | bass |  6.75 |
+----+------+-------+
1 row in set (0.00 sec)

query.py

import MySQLdb
mydb = MySQLdb.connect(host = 'localhost', user = 'root', passwd = '******', db = 'menu')
cur = mydb.cursor()
cur.execute('select * from fish')
results = cur.fetchall()
mydb.close()
for record in results:
    print record[0] , "-->", record[1] , "@", record[2], "each"

afan@ubuntu:~/python-code$ python query.py
1 --> catfish @ 8.50 each
2 --> catfish @ 8.50 each
3 --> tuna @ 8.00 each
4 --> catfish @ 5.00 each
5 --> bass @ 6.75 each
6 --> haddock @ 6.50 each
7 --> salmon @ 9.50 each
8 --> trout @ 6.00 each
9 --> tuna @ 7.50 each
10 --> yellowfin tuna @ 12.00 each
11 --> yellowfin tuna @ 13.00 each
12 --> tuna @ 7.50 each

0

阅读 评论 收藏 转载 喜欢 打印举报/Report
前一篇:python 迭代器
后一篇:mahout算法
  • 评论加载中,请稍候...
发评论

    发评论

    以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

    < 前一篇python 迭代器
    后一篇 >mahout算法
      

    新浪BLOG意见反馈留言板 电话:4000520066 提示音后按1键(按当地市话标准计费) 欢迎批评指正

    新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 会员注册 | 产品答疑

    新浪公司 版权所有