ÓÃ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:Ò
Ïà¹ØÎĵµ£º
ʹÓÃmysqlʱ£¬Í¨³£±íÖлáÓÐÒ»¸ö×ÔÔöµÄid×ֶΣ¬µ«µ±ÎÒÃÇÏ뽫±íÖеÄÊý¾ÝÇå¿ÕÖØÐÂÌí¼ÓÊý¾Ýʱ£¬Ï£ÍûidÖØÐ´Ó1¿ªÊ¼¼ÆÊý£¬ÓÃÒÔÏÂÁ½ÖÖ·½·¨¾ù¿É£º
·½·¨Ò»£º
alter table tablename drop column id;
alter table tablename add id mediumint(8) not null primary key auto_increment first;
·½·¨¶þ£º
alter table tab ......
µØÖ·£º http://imysql.cn/taxonomy/term/1?page=1
[InnoDBϵÁÐ] -- innodb±íÈçºÎ¸ü¿ìµÃµ½count(*)½á¹û£ºhttp://imysql.cn/2008_06_24_speedup_innodb_count
[InnoDBϵÁÐϵÁÐ] -- ´óÊý¾ÝÁ¿µÄµ¼³öµ¼Èë·½·¨±È½Ï£º
http://imysql.cn/2007_10_15_large_innodb_table_export_import
[MySQLÓÅ»¯°¸Àý]ϵÁÐ -- DISABLE/ENABLE K ......
mysql ´´½¨±í£º
mysql> create table user(
-> userid int(4) primary key not null auto_increment,
-> username varchar(16) not null,
-> userpassword varchar(32) not null
-> );
create table log(
logid int(4) ......
½ØÈ¡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 ......