ÓÃpythonÁ¬½ÓmysqlÊý¾Ý¿â
#
-*- 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()
0.ÒýÈëMySQLdb¿â
import MySQLdb
1.ºÍÊý¾Ý¿â½¨Á¢Á¬½Ó
conn=MySQLdb.connect(host="localhost",user="root",passwd="sa",db="mytable")
ÌṩµÄconnect·½·¨ÓÃÀ´ºÍÊý¾Ý¿â½¨Á¢Á¬½Ó,½ÓÊÕÊý¸ö²ÎÊý,·µ»ØÁ¬½Ó¶ÔÏó.
±È½Ï³£ÓõIJÎÊý°üÀ¨
host:Êý¾Ý¿âÖ÷»úÃû.ĬÈÏÊÇÓñ¾µØÖ÷»ú.
user:Êý¾Ý¿âµÇ½Ãû.ĬÈÏÊǵ±Ç°Óû§.
passwd:Êý¾Ý¿âµÇ½µÄÃØÃÜ.ĬÈÏΪ¿Õ.
db:Ò
Ïà¹ØÎĵµ£º
½ØÈ¡province×Ö·û´®ÖеÚÒ»¸ö<br>Ç°µÄ×Ö·û´®¡«£¡
update lcjd
set `province` = substring_index( `province` , '<br>', '1' );
ÔÚÐèÒªÌí¼Ó‘0’µÄλÖÃÌí¼ÓÒ»¸ö‘0’
update lcjd
set lc_name2 = concat('0', lc_name2)
WHERE length(lc_name2) = 3
http://www.sqlstudy.com/s ......
ÔÚÉè¼Æ¹ØϵÊý¾Ý¿âʱÊÇÒª×ñÊØÒ»¶¨µÄ¹æÔòµÄ¡£ÓÈÆäÊÇÊý¾Ý¿âÉè¼Æ·¶Ê½ÏÖ¼òµ¥½éÉÜ1NF£¨µÚÒ»·¶Ê½£©£¬2NF£¨µÚ¶þ·¶Ê½£©£¬3NF£¨µÚÈý·¶Ê½£©ºÍBCNF£¬ÁíÓеÚËÄ·¶Ê½ºÍµÚÎ巶ʽÁôµ½ÒÔºóÔÙ½éÉÜ¡£ÔÚÄãÉè¼ÆÊý¾Ý¿â֮ʱ£¬ÈôÄÜ·ûºÏÕ⼸¸ö·¶Ê½£¬Äã¾ÍÊÇÊý¾Ý¿âÉè¼ÆµÄ¸ßÊÖ¡£
µÚÒ»·¶Ê½£¨1NF£©£ºÔÚ¹ØϵģʽRÖеÄÿһ¸ö¾ßÌå¹ØϵrÖУ¬Èç¹ûÿ¸öÊôÐÔÖµ¶¼Ê ......
//Ö÷¼ü
alter table tabelname add new_field_id int(5) unsigned default 0 not null auto_increment ,add primary key (new_field_id);
//Ôö¼ÓÒ»¸öÐÂÁÐ
alter table t2 add d timestamp;
alter table infos add ex tinyint not null default '0';
//ɾ³ýÁÐ
alter table t2 drop column c;
//ÖØÃüÃûÁÐ
......
MySQL5.X¶¼ÒѾ·¢²¼ºÃ¾ÃÁË£¬µ«ÊÇ»¹ÓкܶàÈËÈÏΪMySQLÊDz»Ö§³ÖÊÂÎñ´¦ÀíµÄ£¬Õâ²»µÃ²»¹ÖËûÃÇÊǹª¹ÑÎŵģ¬Æäʵ£¬Ö»ÒªÄãµÄMySQL°æ±¾
Ö§³ÖBDB»òInnoDB±íÀàÐÍ£¬ÄÇôÄãµÄMySQL¾Í¾ßÓÐÊÂÎñ´¦ÀíµÄÄÜÁ¦¡£ÕâÀïÃ棬ÓÖÒÔInnoDB±íÀàÐÍÓõÄ×î¶à£¬ËäÈ»ºóÀ´·¢ÉúÁËÖîÈçOracleÊÕ
¹ºInnoDBµÈÁîMySQL²»Ë¬µÄÊÂÇ飬µ«ÄÇЩÉÌÒµÉϵĶ·ÕùÓë¼¼ÊõÎÞ¹ ......