1. ubuntu安装MySQL

how to install:
$ sudo apt-get install mysql-server
$ sudo apt-get install mysql-client
$ sudo apt-get install libmysqlclient-dev
#python DB API
$  sudo apt-get install python-mysqldb

check:
sudo netstat -tap | grep mysql

run:
mysql -u root -p

simple commands:
mysql> show databases;
mysql> use mysql    #use database mysql
mysql> show tables;

TIPs:
1. always ends an argument with a ';'
2. not case-sensitive except TABLE and DATABASE names

2. 跟着学一点简单命令"MySql CookBook 3rd"

> CREATE DATABASE cookbook;
> USE cookbook;
> CREATE TABLE limbs (thing VARCHAR(20), legs INT, arms INT);

> INSERT INTO limbs (thing, leg, arms) VALUES('insect',6, 0);
> INSERT INTO limbs (thing, leg, arms) VALUES('armchair', 4, 2);
> INSERT INTO limbs (thing, leg, arms) VALUES('human',2, 2);
> INSERT INTO limbs (thing, leg, arms) VALUES('tripod',3, 0);
> INSERT INTO limbs (thing, leg, arms) VALUES('squid', 0, 10);
> INSERT INTO limbs (thing, leg, arms) VALUES('fish', 0, 0);
> INSERT INTO limbs (thing, leg, arms) VALUES('centipede', 100, 0);
> INSERT INTO limbs (thing, leg, arms) VALUES('table', 4, 0);
> INSERT INTO limbs (thing, leg, arms) VALUES('armchair', 4, 2);
> INSERT INTO limbs (thing, leg, arms) VALUES('phonograph',0,1);
> INSERT INTO limbs (thing, leg, arms) VALUES('Peg Leg Pete',1,2);
> INSERT INTO limbs (thing, leg, arms) VALUES('space alien',NULL,NULL);

> SELECT * FROM limbs;
> SHOW COLUMNS FROM limbs;
> SHOW FULL COLUMNS FROM limbs;
> SHOW FULL COLUMNS FROM limbs \G;
> SHOW FULL COLUMNS FROM limbs LIKE 'thing';
> SHOW FULL COLUMNS FROM limbs LIKE 'thing'\G;
> SELECT COUNT(*) FROM limbs;

3. 修改默认的登录账户和密码

$ sudo vim /etc/mysql/my.cnf
    [client]
    user        = cbuser
    password    = cbpass
$ mysql --print-defaults
here you get:
mysql would have been started with the following arguments:
--user=root --password=****** --port=3306 --socket=/var/run/mysqld/mysqld.sock

$ mysql -e "SELECT COUNT(*) FROM limbs" cookbook
$ mysql -e "SELECT COUNT(*) FROM limbs;SELECT NOW()" cookbook
$ mysql -u root -p -e "SELECT COUNT(*) FROM limbs;SELECT NOW()" cookbook

4. 运行一个SQL 文件:
$mysql cookbook < limbs.sql
or:
    mysql> source limbs.sql;
    mysql> \. limbs.sql;

here limbs.sql is:

    DROP TABLE IF EXISTS limbs;
CREATE TABLE limbs
(
thing VARCHAR(20), # what the thing is
legs INT, # number of legs it has
arms INT # number of arms it has
); INSERT INTO limbs (thing, legs, arms) VALUES('human',2, 2);
INSERT INTO limbs (thing, legs, arms) VALUES('insect',6, 0);
INSERT INTO limbs (thing, legs, arms) VALUES('armchair', 4, 2);
INSERT INTO limbs (thing, legs, arms) VALUES('tripod',3, 0);
INSERT INTO limbs (thing, legs, arms) VALUES('squid', 0, 10);
INSERT INTO limbs (thing, legs, arms) VALUES('fish', 0, 0);
INSERT INTO limbs (thing, legs, arms) VALUES('centipede', 100, 0);
INSERT INTO limbs (thing, legs, arms) VALUES('table', 4, 0);
INSERT INTO limbs (thing, legs, arms) VALUES('armchair', 4, 2);
INSERT INTO limbs (thing, legs, arms) VALUES('phonograph',0,1);
INSERT INTO limbs (thing, legs, arms) VALUES('Peg Leg Pete',1,2);
INSERT INTO limbs (thing, legs, arms) VALUES('space alien',NULL,NULL);
#=====================end of limbs.sql===================================

the mysqldump utility generates database backups by writing a set of SQL statements that re-create the database.

$ mysqldump cookbook > dump.sql

> SELECT * FROM limbs WHERE legs=0;
$ echo "SELECT * FROM limbs WHERE legs=0" | mysql cookbook

Producing HTML or XML output
$ mysql -H -e "SELECT * FROM limbs WHERE legs=0" cookbook > out.html
$ mysql -X -e "SELECT * FROM limbs WHERE legs=0" cookbook > out.xml

mysql -X -e "SELECT * FROM limbs WHERE legs=0" cookbook \
| xsltproc mysql-xml.xsl-

> SELECT @max_limbs := MAX(arms+legs) FROM limbs;
NB: here ':=' should not be =

> SELECT * FROM limbs WHERE arms+legs = @max_limbs;
> SELECT @name := thing FROM limbs WHERE legs = 0;
> SELECT @name

> SET @max_limbs = (SELECT MAX(arms+legs) FROM limbs);
> SET @x = 1, @X = 2; SELECT @x, @X; #User variable names are not case sensitive
> SELECT CONNECTION_ID();

5. Python DB API

#!/usr/bin/python
# connect.py: connect to the MySQL server
# please goto the belowing link for help:
# MySQLdb User's Guide: http://mysql-python.sourceforge.net/MySQLdb.html
import MySQLdb
try:
conn = MySQLdb.connect(host='localhost',db="cookbook",user='root', passwd='*******l', port=3306)
print("Connected")
cur=conn.cursor()
count=cur.execute('select * from limbs')
print 'there are '+str(count)+ 'in all'
while 0 != count:
result=cur.fetchone()
print result
count-=1
cur.close()
except:
print("Cannot connect to server")
else:
conn.close()
print("Disconnected")

一个小玩具:Python调用Mysql的更多相关文章

  1. python3.4学习笔记(二十五) Python 调用mysql redis实例代码

    python3.4学习笔记(二十五) Python 调用mysql redis实例代码 #coding: utf-8 __author__ = 'zdz8207' #python2.7 import ...

  2. python 调用mysql存储过程返回结果集

    存储过程: delimiter | ),)) begin select * from tb_test where mid = imid and user = iuser; end; | delimit ...

  3. Python调用MYSQL,将文件名和路径批量入库用法小结

    最近项目需要将大量的压缩文件导入到数据库中,所以开始总结用Python批量处理的办法,本次是首先将这些压缩文件的文件名提取出来,然后导入到数据库中. 由于涉及到路径的读取处理,所以方法有os模块和co ...

  4. 一个小玩具:NDK编译FFmpeg的例子

    FFmpeg NDK编译 和最简单的APK 准备 硬件: 一台电脑,实验在Lenovo T430上 一个Android设备,实验在 三星S3/A7 编译环境: Ubuntu 14.04 (ant\ja ...

  5. 统计的一个小题目python实现

    最近面试碰到的一个题目,业余时间用python实现的. 拿到数据,先用sort 命令排序,也可再进一步去重复 sort -k 1,2  data.txt |uniq  > data.new # ...

  6. python调用MySQL数据库

    在Python中访问mysql数据库中的数据需要三步骤: 1,建立连接 2,操作数据库 3,连接关闭

  7. 一个小玩具:NDK编译SDL的例子

    NDK编译SDL 准备: 硬件 一台电脑,实验在Lenovo T430上 一个Android设备,实验在 三星S3/A7 编译环境: Ubuntu 14.04 (ant\java等命令必须支持) 工具 ...

  8. 一个小问题 关于 com.mysql.jdbc.PacketTooBigException: Packet for query is too large

    这个错本身就是应为传输的数据大于mysql的max_allowed_packet参数默认值造成的: 之前遇到这个问题,一直是改max_allowed_packet的值 ,做项目遇到这个错误改了好几次, ...

  9. Python调用MySQL的一些用法小结

    目标:1个excel表内容导入到数据库中,例如:原始excel文件为 aaa.xls 首先:将aaa.xls 转换成aaa.txt ,注意当文件中含有中文字符时,可以通过notepad++打开,在“格 ...

随机推荐

  1. 在线支付接口之PHP支付宝接口开发

    支付接口一般是第三方提供的代收款.付款的平台,可以通过支付接口帮助企业或个人利用一切可以使用的支付方式.常见支付平台:支付宝.快钱.云网支付.财付通. 支付宝页面:订单页面.状态页面.返回页面.--- ...

  2. 一句JS搞定只允许输入数字和字母

    一句JS搞定输入框只允许用户输入数字和字母类型的内容,对象是input输入框,当然也可以其它对象,只不过input输入框用的频率非常高.一句代码,不信么?那就看下边代码: <INPUT clas ...

  3. 如何解决mysql数据库8小时无连接自动关闭

    windows下打开my.ini,增加: interactive_timeout=28800000 wait_timeout=28800000 专家解答:MySQL是一个小型关系型数据库管理系统,由于 ...

  4. vsftpd 访问 权限控制

    vsftpd 重启命令 service vsftpd  start|restart|stop vsftpd 关于权限控制,有两个文件分别设置,都会起作用 /etc/vsftpd/user_list / ...

  5. UCOS 信号量的初值问题

    当 pend请求发出的时候信号量的值减1,当post的时候信号量的值加1,信号量的值0跟1分别是用来同步跟互斥的,什么是同步,什么是互斥呢...假设你把信号量的值设为0,有A,B连个任务,当A发出pe ...

  6. IOS面试攻略

    IOS面试攻略(1.0) 2013-10-13 20:58:09|  分类: IOS面试 |  标签:ios知识点总汇  ios面试  |举报|字号 订阅     来自:伊甸网 @ 看到这个关键字,我 ...

  7. codevs 1220 数字三角形

    1220 数字三角形  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 如图所示的数字三角形,从顶部出发,在每 ...

  8. Android中layout_weight的属性理解

    https://www.zybuluo.com/zzudhj/note/102067 在Android开发过程中,在编写布局文件经常会用layout_weight属性:从字面意思上看权重.比值.按比例 ...

  9. Largest product in a series

    这个我开始理解错了,算错了. 我以为是求连续5个数的最大值,结果,是连接5个数相乘的最大值. 英语不好,容易吃亏啊. Find the greatest product of five consecu ...

  10. C# 注销掉事件,解决多播委托链表的问题

    c#的事件是多播委托.当绑定多个事件时,事件会依次触发,清除掉注册的委托链表:方法1  C# Code  12345678910111213141516171819202122232425262728 ...