插入mysql

时间:2011-03-01 12:53:52

标签: mysql batch-file

我想将参数传递给批处理文件,然后将其插入到mysql数据库中。

lunch.bat name surname

echo off
mysql -uusername -ppassword -e "set @1:=name; set @2:=surname; source insert.sql;"

在insert.sql

insert into mytable(namecol,surnamecol) values(@1,@2);

有人可以帮我编写这两个脚本吗?

由于

2 个答案:

答案 0 :(得分:3)

添加单引号来处理字符串输入...如果参数

中提供了引号,则仍然不稳定
@echo off
mysql -uusername -ppassword -e "set @1:='%1'; set @2:='%2'; source insert.sql;"

完成!

答案 1 :(得分:0)

这是我正在处理的一段代码,尽管每次访问该页面时它都会发布到数据库中。我试图添加"唯一密钥"在数据库中使用rand()。但是代码仍然在页面上#34; rand();没有"提交"从形式。无论如何,这是我如何向Mysql插入数据。我的数据库创建时间和日期时间戳()&按顺序排列" id。"

    // Select from database working
    $statement = $database->prepare('SELECT * FROM pressroom ORDER BY id 
    DESC 
    LIMIT 0 , 30' );
    $statement->execute();
    $count = $statement->rowCount();
    if( $count > 0 ) {
    $R = $statement->fetchAll( PDO::FETCH_ASSOC );
    for( $x = 0; $x < count($R); $x++ ) {
    echo "<td><br>";
    echo "<center><b>" . $R[ $x ]['name'] . "</th>";
    echo ": <left></b>" . $R[ $x ]['comment'] . "</th>";
    echo "<center>" . $R[ $x ]['date'] . "<hr></td>";
    echo "</tr>";
     }
     }

     // Make sure that the 'values' are in the same order or as 
        your table

      $query = "INSERT INTO pressroom(first_name, last_name, Password )
      VALUES ('table1','table2','table3')";

    //connection prepare
    try {
    $database->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $results = $database->query($query); 

    //Prints results that are added to table
    print_r($results); 
    } catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
    }