gpt4 book ai didi

mysql - Perl DBI mysql更新

转载 作者:行者123 更新时间:2023-12-01 00:54:29 26 4
gpt4 key购买 nike

名称键值 . . . . . .待更新的用户名

#!/usr/bin/perl -w
# Use the DBI module

use strict;
use warnings;
use DBI;

# CONFIG VARIABLES
my $platform = "mysql";
my $database = "prod";
my $host = "localhost";
my $username = "root";
my $password = "admin";

# DATA SOURCE NAME
my $dsn = "dbi:$platform:$database:$host";

# PERL DBI CONNECT
my $connect = DBI->connect($dsn, $username, $password);

# VARS for Examples
my $query;
my $query_handle;
my $id;
my $demo;

# Example 2 using do() UPDATE

# SAMPLE VARIABLE AND VALUES TO PASS INTO SQL STATEMENT
$id = "username";
$name = "Arty";

# do() THE UPDATE
$query = "UPDATE jos_config SET values = '$name' WHERE namekey = $id;";
$query_handle = $connect->prepare($query);

# EXECUTE THE UPDATE
$query_handle = $connect->do($query);

print STDERR "ERROR: $DBI::errstr";
print STDERR "INFO: $query_handle rows updated";

undef $query;

错误信息:

DBD::mysql::db do failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'values = 'Arty' WHERE namekey = smtp_username' at line 1 at /home/arty/Documents/SmtpDbchange2.pl line 40.
ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'values = 'Arty' WHERE namekey = smtp_username' at line 1Use of uninitialized value $query_handle in concatenation (.) or string at /home/arty/Documents/SmtpDbchange2.pl line 43.

请帮忙

最佳答案

您的语法错误来自于“值”是 SQL 中的保留字这一事实。当它用作标识符时,您需要引用该 token :

UPDATE jos_config SET `values` = ...

通常,MySQL 会在查询摘录的开头向您显示语法错误的位置,就像它为您所做的那样。

您的下一个问题是您没有正确引用与 namekey 比较的字面值,并且该字面值对于 MySQL 来说看起来更像是标识符而不是字符串。这里的解决方案是忘记插值变量并使用参数化查询:

$connect->do('UPDATE jos_config SET `values`=? WHERE namekey=?', undef, $name, $id);

关于mysql - Perl DBI mysql更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12380528/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com