| 您的当前位置:首页 --> MYSQL教程 |
| MySQL中insert语句的使用与优化教程 |
| 浏览次数:1030 关键词 ( ) |
|
MySQL 表中使用 INSERT INTO SQL语句来插入数据。
INSERT INTO table_name ( field1, field2,...fieldN )
VALUES
( value1, value2,...valueN );
如果数据是字符型,必须使用单引号或者双引号,如:"value"。
root@host# mysql -u root -p password;
Enter password:*******
mysql> use RUNOOB;
Database changed
mysql> INSERT INTO runoob_tbl
->(runoob_title, runoob_author, submission_date)
->VALUES
->("Learn PHP", "John Poul", NOW());
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO runoob_tbl
->(runoob_title, runoob_author, submission_date)
->VALUES
->("Learn MySQL", "Abdul S", NOW());
Query OK, 1 row affected (0.01 sec)
mysql> INSERT INTO runoob_tbl
->(runoob_title, runoob_author, submission_date)
->VALUES
->("JAVA Tutorial", "Sanjay", '2007-05-06');
Query OK, 1 row affected (0.01 sec)
mysql>
bool mysql_query( sql, connection ); 参数:
<html>
<head>
<title>向 MySQL 数据库添加数据</title>
</head>
<body>
<?php
if(isset($_POST['add']))
{
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
if(! get_magic_quotes_gpc() )
{
$runoob_title = addslashes ($_POST['runoob_title']);
$runoob_author = addslashes ($_POST['runoob_author']);
}
else
{
$runoob_title = $_POST['runoob_title'];
$runoob_author = $_POST['runoob_author'];
}
$submission_date = $_POST['submission_date'];
$sql = "INSERT INTO runoob_tbl ".
"(runoob_title,runoob_author, submission_date) ".
"VALUES ".
"('$runoob_title','$runoob_author','$submission_date')";
mysql_select_db('RUNOOB');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully\n";
mysql_close($conn);
}
else
{
?>
<form method="post" action="<?php $_PHP_SELF ?>">
<table width="600" border="0" cellspacing="1" cellpadding="2">
<tr>
<td width="250">Tutorial Title</td>
<td>
<input name="runoob_title" type="text" id="runoob_title">
</td>
</tr>
<tr>
<td width="250">Tutorial Author</td>
<td>
<input name="runoob_author" type="text" id="runoob_author">
</td>
</tr>
<tr>
<td width="250">Submission Date [ yyyy-mm-dd ]</td>
<td>
<input name="submission_date" type="text" id="submission_date">
</td>
</tr>
<tr>
<td width="250"> </td>
<td> </td>
</tr>
<tr>
<td width="250"> </td>
<td>
<input name="add" type="submit" id="add" value="Add Tutorial">
</td>
</tr>
</table>
</form>
<?php
}
?>
</body>
</html>
在我们接收用户提交的数据时,为了数据的安全性我们需要使用 get_magic_quotes_gpc() 函数来判断特殊字符的转义是否已经开启。如果这个选项为off(未开启),返回0,那么我们就必须调用addslashes 这个函数来为字符串增加转义。
insert into `table`(`field1`,`field2`) values('value1','value2');
提高insert 性能的方法
INSERT INTO `insert_table` (`uid`, `content`, `type`) VALUES ('userid_0', 'content_0', 0);
INSERT INTO `insert_table` (`uid`, `content`, `type`) VALUES ('userid_1', 'content_1', 1);
可以写成 复制代码 代码如下: INSERT INTO `insert_table` (`uid`, `content`, `type`) VALUES ('userid_0', 'content_0', 0), ('userid_1', 'content_1', 1); 2.使用事务
START TRANSACTION;
INSERT INTO `insert_table` (`uid`, `content`, `type`) VALUES ('userid_0', 'content_0', 0);
INSERT INTO `insert_table` (`uid`, `content`, `type`) VALUES ('userid_1', 'content_1', 1);
...
COMMIT;
注意 关于事务的配置项说明: 2.innodb_additional_pool_size 3.innodb_log_file_size 4.innodb_log_buffer_size 5.innodb_flush_log_at_trx_commit |
| 下载次数:8 |
| 下载地址:点击下载 |
| 本资源为程序自动采集,如有侵权请联系我们移除 admin#80vps.com 来信请将#替换为@ |
| 下一条 SQL计算timestamp的差值的方法 上一条 并发环境下mysql插入检查方案 |