全部博文(930)
分类: Mysql/postgreSQL
2010-05-09 16:37:45
LOCK TABLES和UNLOCK TABLES语法
LOCK TABLES
tbl_name [AS alias] {READ [LOCAL] | [LOW_PRIORITY] WRITE}
[, tbl_name [AS alias] {READ [LOCAL] | [LOW_PRIORITY] WRITE}] ...
UNLOCK TABLES
LOCK TABLES可以锁定用于当前线程的表。如果表被其它线程锁定,则造成堵塞,直到可以获取所有锁定为止。
UNLOCK TABLES可以释放被当前线程保持的任何锁定。当线程发布另一个LOCK TABLES时,或当与服务器的连接
被关闭时,所有由当前线程锁定的表被隐含地解锁。表锁定只用于防止其它客户端进行不正当地读取和写入。
保持锁定(即使是读取锁定)的客户端可以进行表层级的操作,比如DROP TABLE。
#include
#include
#include
#include
#include
#define BUFFER_SIZE 1000000
#define bool int
#define true 1
#define false 0
bool Connect_Server(MYSQL **sql,char *host,char *user,char *passwd,char *dbname)
{
my_bool b = 0;
if(!(*sql=mysql_init(NULL)))
{
printf("%s\n",mysql_error(*sql));
return 0;
}
mysql_options(*sql,MYSQL_REPORT_DATA_TRUNCATION,&b);
if(!mysql_real_connect(*sql,host,user,passwd,dbname,0,NULL,0))
{
fprintf(stderr,"%s\n",mysql_error(*sql));
return 0;
}
return 1;// true;
}
bool Execute(MYSQL *mysql,MYSQL_RES **res,char *query,int length)
{
if(length!=0)
{
if(mysql_real_query(mysql,query,length))
{
printf("query error!\n");
return false;
}
}
else
{
if(mysql_query(mysql,query))
{
printf("%s","query error!\n");
return false;
}
}
return true;
}
void Mysql_Close(MYSQL *sql)
{
mysql_close(sql);
}
int main(void)
{
char query[200];
//STEP 1:define varible
MYSQL *mysql;
MYSQL_RES *res;
/////////////////////////////////////////////////////////////////////////////////////
//STEP 2:connect to database
//"127.0.0.1", server ip;
//"root" and "123456",usename and passwd
//"test",which database you want to use
if(!Connect_Server(&mysql,"127.0.0.1", "root", "123456", "test"))
{
return 0;
}
//////////////////////////////////////////////////////////////////////////////////////
//STEP 3:query
////////query cmd.
strcpy(query,"lock table a write");
/////////execute query
if(!Execute(mysql,NULL,query,0))
{
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////
sleep(5);
strcpy(query,"unlock tables");
if(!Execute(mysql,NULL,query,0))
{
return 0;
}
Mysql_Close(mysql);
return 0;
}
结果
mysql> select * from a;
+------+--------+
| id | name |
+------+--------+
| 1 | jack |
| 2 | mick |
| 3 | ben |
| 4 | kenthy |
| 5 | jeremy |
+------+--------+
5 rows in set (3.25 sec)
read lock就是
LOCK TABLES tbl_name READ就可以了!
chinaunix网友2010-05-31 22:26:35
MBT is based on Africa's indigenous people to create innovative walking barefoot, and wear this shoe can stimulate the body to relax the muscles so that the muscles of heat to be released, consumed calories, thus to achieve weight loss results. MBT's pursuit of beauty in order to satisfy customers, design a variety of styles, such as Mbt Chapa shoe, MBT Lami Shoe