我正在使用SELECT|INSERT|UPDATE
某个表的简单MySQLi函数,但我无法使其工作。这是函数的代码:
function syncReps($fileName, $server, $user, $pass, $db)
{
$conn = new mysqli($server, $user, $pass, $db);
if ($conn->connect_error) {
trigger_error('Database connection failed: '.$conn->connect_error, E_USER_ERROR);
}
$data = convertCsvToArray($fileName);
echo "DEBUG count(data): ", count($data), "\n";
// Processing on each row of data
foreach ($data as $row) {
$sql = 'SELECT id,lastSyncAt FROM reps WHERE veeva_rep_id = '.$row['Id'];
echo "DEBUG: ", $sql, "\n";
$rs = $conn->query($sql);
if ($rs === false) {
trigger_error('Wrong SQL: '.$sql.' Error: '.$conn->error, E_USER_ERROR);
} else {
$rows_returned = $rs->num_rows;
$veeva_rep_id = "'".$conn->real_escape_string($row['Id'])."'";
$first = "'".$conn->real_escape_string(ucfirst(strtolower($row['FirstName'])))."'";
$last = "'".$conn->real_escape_string(ucfirst(strtolower($row['LastName'])))."'";
$email = "'".$conn->real_escape_string($row['Email'])."'";
$username = "'".$conn->real_escape_string($row['Username'])."'";
$display_name = "'".$conn->real_escape_string(
ucfirst(strtolower($row['FirstName'])).' '.ucfirst(strtolower($row['LastName']))
)."'";
if ($rows_returned === 0) {
$sql = "INSERT INTO reps(veeva_rep_id,first,last,email,username,lastLoginAt,lastSyncAt,display_name,rep_type,avatar_url) VALUES($veeva_rep_id,$first,$last,$email,$username,NOW(),NOW(),$display_name,'VEEVA','default_avatar.png')";
echo "DEBUG: ", $sql, "\n";
$conn->query($sql);
} else {
}
}
}
}
当我从CLI以php script.php
执行脚本时,我得到了这个:
DEBUG count(data): 1454
DEBUG: SELECT id,lastSyncAt FROM reps WHERE veeva_rep_id = 00580000008ReolAAC
它首先在SELECT
查询停止,为什么?如果$data
计数为1454,则SELECT
和INSERT
$num_rows === 0
不应该执行相同的function syncTerritories($fileName, $server, $user, $pass, $db)
{
$conn = new mysqli($server, $user, $pass, $db);
if ($conn->connect_error) {
trigger_error('Database connection failed: '.$conn->connect_error, E_USER_ERROR);
}
$data = convertCsvToArray($fileName);
echo "DEBUG count(data): ", count($data), "\n";
// Processing on each row of data
foreach ($data as $row) {
$sql = "SELECT id FROM territories WHERE veeva_territory_id='{$row['Id']}'";
echo "DEBUG: ", $sql, "\n";
$rs = $conn->query($sql);
if ($rs === false) {
echo 'Wrong SQL: '.$sql.' Error: '.$conn->error, E_USER_ERROR;
} else {
$rows_returned = $rs->num_rows;
$veeva_territory_id = "'".$conn->real_escape_string($row['Id'])."'";
$name = "'".$conn->real_escape_string($row['Name'])."'";
if ($rows_returned === 0) {
$sql = "INSERT INTO territories(veeva_territory_id,territory_name,createdAt,updatedAt) VALUES($veeva_territory_id,$name,NOW(),NOW())";
echo "DEBUG: ", $sql, "\n";
$rs = $conn->query($sql);
if ($rs === false) {
echo 'Wrong SQL: '.$sql.' Error: '.$conn->error, E_USER_ERROR;
}
$rs->free();
} else {
// UPDATE
}
}
}
}
和DEBUG count(data): 6911
DEBUG: SELECT id FROM territories WHERE veeva_territory_id='04T800000008zy9EAA'
DEBUG: SELECT id FROM territories WHERE veeva_territory_id='04T80000000903eEAA'
DEBUG: SELECT id FROM territories WHERE veeva_territory_id='04T80000000TOTIEA4'
DEBUG: INSERT INTO territories(veeva_territory_id,territory_name,createdAt,updatedAt) VALUES('04T80000000TOTIEA4','AKORN_101TM',NOW(),NOW())
?我在这里缺少什么?
修改
我仍然遇到与其他功能类似的问题,请参阅下文:
buildozer --verbose android debug deploy run
输出如下:
# Install distribute
# Run 'curl http://python-distribute.org/distribute_setup.py | venv/bin/python'
# Cwd /home/yerman/PycharmProjects/genius/.buildozer
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- 0:00:16 --:--:-- 0
# Install requirement jnius in virtualenv
# Run 'pip install --download-cache=/home/yerman/.buildozer/cache --target=/home/yerman/PycharmProjects/genius/.buildozer/applibs jnius'
# Cwd /home/yerman/PycharmProjects/genius/.buildozer
DEPRECATION: --download-cache has been deprecated and will be removed in the future. Pip now automatically uses and configures its cache.
Collecting jnius
Using cached jnius-1.0.2.tar.gz
Complete output from command python setup.py egg_info:
You need Cython to compile Pyjnius.
Traceback (most recent call last):
File "<string>", line 20, in <module>
File "/tmp/pip-build-_tfhLW/jnius/setup.py", line 32, in <module>
from Cython.Distutils import build_ext
ImportError: No module named Cython.Distutils
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-_tfhLW/jnius
# Command failed: pip install --download-cache=/home/yerman/.buildozer/cache --target=/home/yerman/PycharmProjects/genius/.buildozer/applibs jnius
#
# Buildozer failed to execute the last command
# The error might be hidden in the log above this error
# Please read the full log, and search for it before
# raising an issue with buildozer itself.
# In case of a bug report, please add a full log with log_level = 2
现在有什么问题?应该从这种方式转变为准备好的陈述吗?如果是这样的话可以根据我的例子给我一些疑问吗?
答案 0 :(得分:1)
这是一个无效的SQL字符串要执行。您需要将veeva_rep_id
值包装在单引号中:
$sql = "SELECT id,lastSyncAt FROM reps WHERE veeva_rep_id='{$row['Id']}'";