大虾米(dxm)的技术博客

导航

<2010年8月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
2930311234

统计

留言簿(0)

随笔分类

随笔档案

文章档案

相册

友情链接

搜索

最新评论

阅读排行榜

评论排行榜

mysql 应用两问题:最后插入的id,与转义

一,最后插入的id
     我们常常会将该表的主关键字设置为一个自增量,当我们使用insert 插入一条记录时,怎么获得刚刚插入的那条记录的id呢?
  方法1: select max(id) from tTable 
  
因为有并发的存在,这个方法是不可靠的.
     正确方法: Select last_insert_id() 这是可靠的.有兴趣的人可以看下面这篇文章
http://blog.163.com/javahou/blog/static/124403362200971995155763/
二,转义问题
        一个字段串,比如说 c:\doc
        那么写成C语言的话,因为\是用来转义的必须使用 c:\\doc 这个大家都知道了.
     但是如果你要写进mysql 的语句里,也许你得使用 c:\\\\doc 
        因为,C语言进行一次转义,你传递查询语句给mysql,mysql也会进行一次转义!
     也就是说,你得用4个 \ 来代表一个真正的 \
        不知道我说清楚没有.
  我在程序中是这么处理的 
  szTemp.Replace(L"\\",L"\\\\"); //这一行是特意处理字符串转义的
  wchar_t sCmd[256];
   wsprintf(sCmd,L"Select * from tdisk where upper(path)='%s'",szTemp);

  这样,才正确.
注:随笔发表后,有朋友指出 mysql_real_escape_string 确是正解.非常感谢.不过在某些场合,我这个方法也是可用的.
   

posted on 2010-01-05 21:05 大虾米(dxm)的技术博客 阅读(1736) 评论(9)  编辑 收藏

评论

# re: mysql 应用两问题:最后插入的id,与转义 2010-01-05 22:07 Diviner

还是用
http://dev.mysql.com/doc/refman/5.1/en/mysql-real-escape-string.html
这个函数吧。
unsigned long mysql_real_escape_string(MYSQL *mysql, char *to, const char *from, unsigned long length)

Note that mysql must be a valid, open connection. This is needed because the escaping depends on the character set in use by the server.

Description

This function is used to create a legal SQL string that you can use in an SQL statement. See Section 8.1.1, “Strings”.

The string in from is encoded to an escaped SQL string, taking into account the current character set of the connection. The result is placed in to and a terminating null byte is appended. Characters encoded are NUL (ASCII 0), “\n”, “\r”, “\”, “'”, “"”, and Control-Z (see Section 8.1, “Literal Values”). (Strictly speaking, MySQL requires only that backslash and the quote character used to quote the string in the query be escaped. This function quotes the other characters to make them easier to read in log files.)

The string pointed to by from must be length bytes long. You must allocate the to buffer to be at least length*2+1 bytes long. (In the worst case, each character may need to be encoded as using two bytes, and you need room for the terminating null byte.) When mysql_real_escape_string() returns, the contents of to is a null-terminated string. The return value is the length of the encoded string, not including the terminating null character.

If you need to change the character set of the connection, you should use the mysql_set_character_set() function rather than executing a SET NAMES (or SET CHARACTER SET) statement. mysql_set_character_set() works like SET NAMES but also affects the character set used by mysql_real_escape_string(), which SET NAMES does not.

Example
char query[1000],*end;

end = strmov(query,"INSERT INTO test_table values(");
*end++ = '\'';
end += mysql_real_escape_string(&mysql, end,"What is this",12);
*end++ = '\'';
*end++ = ',';
*end++ = '\'';
end += mysql_real_escape_string(&mysql, end,"binary data: \0\r\n",16);
*end++ = '\'';
*end++ = ')';

if (mysql_real_query(&mysql,query,(unsigned int) (end - query)))
{
fprintf(stderr, "Failed to insert row, Error: %s\n",
mysql_error(&mysql));
}

The strmov() function used in the example is included in the mysqlclient library and works like strcpy() but returns a pointer to the terminating null of the first parameter.

Return Values

The length of the value placed into to, not including the terminating null character.

Errors

None.

# 非常感谢,mysql_real_escape_string 确实是正解 2010-01-06 02:38 大虾米(dxm)的技术博客

谢谢

标题  
姓名  
主页
验证码 *
内容   
  登录  使用高级评论  Top
[使用Ctrl+Enter键可以直接提交]