Showing posts with label MySQL. Show all posts
Showing posts with label MySQL. Show all posts

Monday, March 30, 2009

MySQL数据类型列表

名称
长度
用法
TINYINT(M)
BIT,BOOL,BOOLEAN
1
如果为无符号数,可以存储从0到255的数;
否则可以存储从-128到127的数。
SMALLINT(M)
2
如果为无符号数,可以存储从0到65535的数;
否则可以存储从-32768到32767的数。
MEDIUMINT(M)
3
如果为无符号数,可以存储从0到16777215的数;否则可以存储从-8388608到8388607的数
INT(M)
INTEGER(M)
4
如果为无符号数,可以存储从0到4294967295的数,否则可以存储从-2147483648到2147483647的数。
BIGINT(M)
8
如果为无符号数,可以存储从0到18446744073709551615的数,否则可以存储从-9223372036854775808到9223372036854775807的数。
FLOAT(precision)
4或8
这里的precision是可以直达53的整数。如果precision<=24则转换为FLOAT,如果precision>24并且precision<=53则转换为DOUBLE。
FLOAT(M,D)
4
单精度浮点数。
DOUBLE(M,D),
DOUBLE PRECISION,
REAL
8
双精度浮点。
DECIMAL(M,D),
DEC,NUMERIC,FIXED
M+1或M+2
未打包的浮点数。
DATE
3
以YYYY-MM-DD的格式显示。
DATETIME
HH:MM:SS
8
以YYYY-MM-DD的格式显示。
TIMESTAMP
4
以YYYY-MM-DD的格式显示。
TIME
3
以HH:MM:SS的格式显示。
YEAR
1
以YYYY的格式显示。
CHAR(M)
M
定长字符串。
VARCHAR(M)
最大M
变长字符串。M<=255.
TINYBLOB,
TINYTEXT
最大255
TINYBLOB为大小写敏感,而TINYTEXT不是大小写敏感的。
BLOB,
TEXT
最大64K
BLOB为大小敏感的,而TEXT不是大小写敏感的。
MEDIUMBLOB,
MEDIUMTEXT
最大16M
MEDIUMBLOB为大小写敏感的,而MEDIUMTEXT不是大小敏感的。
LONGBLOB,
LONGTEXT
最大4G
LONGBLOB为大小敏感的,而LONGTEXT不是大小敏感的。
ENUM(VALUE1,….)
1或2
最大可达65535个不同的值。
SET(VALUE1,….)
可达8
最大可达64个不同的值。

Saturday, March 28, 2009

MySQL常用命令

1、显示数据库列表。

show databases;

刚开始时才两个数据库:mysql和test。mysql库很重要它里面有MYSQL的系统信息,我们改密码和新增用户,实际上就是用这个库进行操作。

2、显示库中的数据表:

use mysql;

show tables;

3、显示数据表的结构:

describe 表名;

4、建库:

create database 库名;

5、建表:

use 库名;

create table 表名 (字段设定列表);

6、删库和删表:

drop database 库名;

drop table 表名;

7、将表中记录清空:

delete from 表名;

8、显示表中的记录:

select * from 表名;

Tuesday, March 24, 2009

使用Python操作MySQL之基础篇

使用Python操作MySQL
首先下载MySQLdb,地址:http://sourceforge.net/projects/mysql-python/

1. 使用
import MySQLdb
1.1. 连接
conn = MySQLdb.Connection(host, user, password, dbname)
1.2. 选择数据库,如果上面没有指定数据库,则使用此方法指定!
conn.select_db(’database name’)
1.3. 获得cursor
cur = conn.cursor()
1.4. cursor位置设定
cur.scroll(int, mode)
mode可为相对位置或者绝对位置,分别为relative和absolute。
1.5. select
cur.execute(‘select clause’)
例如
cur.execute(‘select * from mytable’)

row = cur.fetchall()
或者:
row1 = cur.fetchone()
1.6. insert
cur.execute(‘inset clause’)
例如
cur.execute("insert into user (Name, Password) values ('maggie','12345')")
conn.commit()

1.7. update
cur.execute(‘update clause’)
例如
cur.execute("update user set Name = 'eric chau' where id = 1")
conn.commit()

1.8. delete
cur.execute(‘delete clause’)
例如
cur.execute("delete from user where id = 1")
conn.commit()

完整代码:

from MySQLdb import Connect

def conn():
#conn = Connect('localhost','root','root')
#conn.select_db('eric')
conn = Connect('localhost','root','root','eric')
cur = conn.cursor()
cur.execute('select * from user')
cur.scroll(0)
row1 = cur.fetchone()
print 'id:', row1[0]
print 'name:', row1[1]
print 'password:', row1[2]
#cur.execute("insert into user (Name, Password) values ('maggie','12345')")
#cur.execute("update user set Name = 'eric chau' where id = 1")
cur.execute("delete from user where id = 11")
conn.commit()
if __name__=='__main__':
conn()

由于python的数据库模块有专门的数据库模块的规范,所以,其实不管使用哪种数据库的方法都大同小异的,这里就给出一段示范的代码:

#-*- encoding: gb2312 -*-
import os, sys, string
import MySQLdb

# 连接数据库 
try:
conn = MySQLdb.connect(host='localhost',user='root',passwd='xxxx',db='test1')
except Exception, e:
print e
sys.exit()

# 获取cursor对象来进行操作

cursor = conn.cursor()
# 创建表
sql = "create table if not exists test1(name varchar(128) primary key, age int(4))"
cursor.execute(sql)
# 插入数据
sql = "insert into test1(name, age) values ('%s', %d)" % ("zhaowei", 23)
try:
cursor.execute(sql)
except Exception, e:
print e

sql = "insert into test1(name, age) values ('%s', %d)" % ("张三", 21)
try:
cursor.execute(sql)
except Exception, e:
print e
# 插入多条

sql = "insert into test1(name, age) values (%s, %s)"
val = (("李四", 24), ("王五", 25), ("洪六", 26))
try:
cursor.executemany(sql, val)
except Exception, e:
print e

#查询出数据
sql = "select * from test1"
cursor.execute(sql)
alldata = cursor.fetchall()
# 如果有数据返回,就循环输出, alldata是有个二维的列表
if alldata:
for rec in alldata:
print rec[0], rec[1]

cursor.close()

conn.close()

转自:http://hi.baidu.com/cheneychen/blog/item/064f9d2b1b5d50fde6cd40dd.html