ÓÃ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_real_connect( ..., CLIENT_MULTI_STATEMENTS );
//»òÕß
//ÖÐ;ָ¶¨
mysql_set_server_option( mysql, MYSQL_OPTION_MULTI_STATEMENTS_ON ); //mysqlÊÇÁ¬½ÓµÄÃû³Æ
µ±Ê¹ÓÃÖ´ÐжàÓï¾ä¹¦Äܺó£¬Ò»¶¨Òª¶ÁÍêÕû¸öresault¼¯£¬·ñÔò»á³öÏÖ´íÎó£ºCommands out of sync; you can't run this co ......
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 ......
Ð¶ÔØmysql
1¡¢²éÕÒÒÔǰÊÇ·ñ×°ÓÐmysql
ÃüÁrpm -qa|grep -i mysql
¿ÉÒÔ¿´µ½mysqlµÄÁ½¸ö°ü£º
mysql-4.1.12-3.RHEL4.1
mysqlclient10-3.23.58-4.RHEL4.1
2¡¢É¾³ýmysql
ɾ³ýÃüÁrpm -e --nodeps °üÃû
( rpm -ev mysql-4.1.12-3.RHEL4.1 )
3¡¢É¾³ýÀϰ汾mysqlµÄ¿ª·¢Í·ÎļþºÍ¿â
ÃüÁrm -fr /usr/lib/mysql
r ......
·Ç³£³£Óõģº¾µäµÄMYSQLÓï¾ä…… php¿ªÔ´
ÕâÕæÊÇÒ»¸öºÃ¶«Î÷£ºÑ§Ï°ºÃÁ˺ÜÊÜÓõÄŶ……
SQL·ÖÀࣺ
DDL—Êý¾Ý¶¨ÒåÓïÑÔ(Create£¬Alter£¬Drop£¬DECLARE)
DML—Êý¾Ý²Ù×ÝÓïÑÔ(Select£¬Delete£¬Update£¬Insert)
DCL—Êý¾Ý¿ØÖÆÓïÑÔ(GRANT£¬REVOKE£¬COMMIT£¬ROLLBACK)php¿ªÔ´
Ê×ÏÈ,¼òÒ ......