我想更新我的数据,我使用它,但是它不会更新我的数据,import java.util.Random;
import java.util.HashMap;
// A common interface for all players
interface Player
{
public void assignWeapon(String weapon);
public void mission();
}
// Terrorist must have weapon and mission
class Terrorist implements Player
{
// Intrinsic Attribute
private final String TASK;
// Extrinsic Attribute
private String weapon;
public Terrorist()
{
TASK = "PLANT A BOMB";
}
public void assignWeapon(String weapon)
{
// Assign a weapon
this.weapon = weapon;
}
public void mission()
{
//Work on the Mission
System.out.println("Terrorist with weapon "
+ weapon + "|" + " Task is " + TASK);
}
}
// CounterTerrorist must have weapon and mission
class CounterTerrorist implements Player
{
// Intrinsic Attribute
private final String TASK;
// Extrinsic Attribute
private String weapon;
public CounterTerrorist()
{
TASK = "DIFFUSE BOMB";
}
public void assignWeapon(String weapon)
{
this.weapon = weapon;
}
public void mission()
{
System.out.println("Counter Terrorist with weapon "
+ weapon + "|" + " Task is " + TASK);
}
}
// Claass used to get a playeer using HashMap (Returns
// an existing player if a player of given type exists.
// Else creates a new player and returns it.
class PlayerFactory
{
/* HashMap stores the reference to the object
of Terrorist(TS) or CounterTerrorist(CT). */
private static HashMap <String, Player> hm =
new HashMap<String, Player>();
// Method to get a player
public static Player getPlayer(String type)
{
Player p = null;
/* If an object for TS or CT has already been
created simply return its reference */
if (hm.containsKey(type))
p = hm.get(type);
else
{
/* create an object of TS/CT */
switch(type)
{
case "Terrorist":
System.out.println("Terrorist Created");
p = new Terrorist();
break;
case "CounterTerrorist":
System.out.println("Counter Terrorist Created");
p = new CounterTerrorist();
break;
default :
System.out.println("Unreachable code!");
}
// Once created insert it into the HashMap
hm.put(type, p);
}
return p;
}
}
// Driver class
public class CounterStrike
{
// All player types and weopons (used by getRandPlayerType()
// and getRandWeapon()
private static String[] playerType =
{"Terrorist", "CounterTerrorist"};
private static String[] weapons =
{"AK-47", "Maverick", "Gut Knife", "Desert Eagle"};
// Driver code
public static void main(String args[])
{
/* Assume that we have a total of 10 players
in the game. */
for (int i = 0; i < 10; i++)
{
/* getPlayer() is called simply using the class
name since the method is a static one */
Player p = PlayerFactory.getPlayer(getRandPlayerType());
/* Assign a weapon chosen randomly uniformly
from the weapon array */
p.assignWeapon(getRandWeapon());
// Send this player on a mission
p.mission();
}
}
// Utility methods to get a random player type and
// weapon
public static String getRandPlayerType()
{
Random r = new Random();
// Will return an integer between [0,2)
int randInt = r.nextInt(playerType.length);
// return the player stored at index 'randInt'
return playerType[randInt];
}
public static String getRandWeapon()
{
Random r = new Random();
// Will return an integer between [0,5)
int randInt = r.nextInt(weapons.length);
// Return the weapon stored at index 'randInt'
return weapons[randInt];
}
}
仍然为空。那么,如何同时执行多个SQL查询呢?
id
答案 0 :(得分:0)
您可以在一个电话中用分号分隔发送多个SQL查询
$sql = "UPDATE laporan_gini SET id_provinsi='1' WHERE nama_item_vertical_variabel= 'INDONESIA';";
$sql.= "UPDATE laporan_gini SET id_provinsi='61' WHERE nama_item_vertical_variabel= 'KALIMANTAN BARAT';";
if (!$mysqli->multi_query($sql)) {
echo "Multi query failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
答案 1 :(得分:0)
最好的方法是使用sql触发器或存储过程:
https://dev.mysql.com/doc/refman/8.0/en/trigger-syntax.html
https://dev.mysql.com/doc/connector-net/en/connector-net-tutorials-stored-procedures.html
以其他方式可以在代码中运行它:
<?php
$mysqli = new mysqli("example.com", "user", "password", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
if (!$mysqli->query("DROP TABLE IF EXISTS test") || !$mysqli->query("CREATE TABLE test(id INT)")) {
echo "Table creation failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
$sql = "SELECT COUNT(*) AS _num FROM test; ";
$sql.= "INSERT INTO test(id) VALUES (1); ";
$sql.= "SELECT COUNT(*) AS _num FROM test; ";
if (!$mysqli->multi_query($sql)) {
echo "Multi query failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
do {
if ($res = $mysqli->store_result()) {
var_dump($res->fetch_all(MYSQLI_ASSOC));
$res->free();
}
} while ($mysqli->more_results() && $mysqli->next_result());
?>
答案 2 :(得分:0)
你永远不应该使用 mysqli::multi_query()
来执行你的 SQL,因为这个函数不支持参数绑定。您必须使用准备好的语句分别执行这两个查询。如果您不打算将任何数据绑定到您的 SQL,那么您可以将其减少一行并改用 mysqli::query()
。
如果两个查询相互依赖,那么您可以将它们包装在一个事务中(假设您的表引擎支持事务)。
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli("a", "a", "a", "a");
$mysqli->set_charset('utf8mb4'); // always set the charset
// start transaction
$mysqli->begin_transaction();
$sql = "UPDATE laporan_gini SET id_provinsi='1' WHERE nama_item_vertical_variabel= 'INDONESIA'";
$stmt = $mysqli->prepare($sql);
$stmt->execute();
$sql = "UPDATE laporan_gini SET id_provinsi='61' WHERE nama_item_vertical_variabel= 'KALIMANTAN BARAT'";
$stmt = $mysqli->prepare($sql);
$stmt->execute();
// commit data and end transaction
$mysqli->commit();