lundi 28 janvier 2019

SQL If condition is true Update table

i am trying to write a one line sql statement that can check for a certain condition and if the condition is met I need to update a table with a new value. below is what I want to do, I just cant find out how to accomplish this. any help would be greatly appreciated.

if (select * from table
    where field1 = '0000000001' and field2 = 'XXX')
update table set field1 = '9876543210'
where field2 = 'XXX';

I am getting the following error:

An expression of non-Boolean type specified in a context where a condition is expected, near 'update'

Using a kotlin clause with enums instead of multiple if

I am working with the next code:

  override fun presentNativeItem(dcsItem: DCSItem?): Any {
        if (dcsItem?.type == "NavMenu") {
            return buildNavMenu(dcsItem)
        } else if (dcsItem?.type == "NavLink") {
            return buildNavLink(dcsItem)
        } else if (dcsItem?.type == "Image") {
            return buildImage(dcsItem)
        }
        else throw IllegalStateException("Unknown Type ${dcsItem?.type} of NavItem")
    }

But instead of using multiple if, I would like to use the next enum:

enum class DSCType {
    NAVMENU,
    NAVLINK,
    IMAGE;

    override fun toString(): String {
        return this.name.toLowerCase()
    }

    companion object {
        fun fromString(value: String?): DSCType? {
            return when (value?.toLowerCase()) {
                "NavMenu" -> NAVMENU
                "NavLink" -> NAVLINK
                "Image" -> IMAGE
                else -> null
            }
        }
    }
}

Any ideas of how can I achieve that in the kotlin way?

Thanks

Cooldown within an if statement

I'm coding a pretty simple Twitch Chatbot in Python and I have all my "commands" running in if statements (basically if the chatbot sees something in chat it activates).

However, I want to add a cooldown of about 3 seconds or really an amount of time per command (or if statement), so that I can customize it depending. To do that I tried this (pseudo code)

if command is seen in chat
    newtimestamp = create timestamp at now
    if (newtimestamp - previoustimestamp) > 30
        execute the command
        make (previoustimestamp)
    else
        return

but that didn't work because obviously it does not detect the (previoustimestamp) on the first run since it's not declared yet, but when you declare it, it declares it every time the command is run.

So is there a way to make a declaration only once at the beginning of an if statement and then ignore it subsequently every other time? Or any other ideas? I'm still a pretty novice programmer so I apologize.

Here's an example code that I would want the cooldown for, something quite simple

if "!redb match" in message:    
   x = str(randint(1, 100))
   realmessage = message.replace('!redb ship ', '')
   array = realmessage.split(' ')
   person1 = array[0]
   person2 = array[1]
   if ((9 - 2) > 5):
      sendMessage(s, person1 + " and " + person2 + "have a " + x + "% love compatibility <3")           
   else:
      break

How can I have my formula show a blank if searched cell is empty?

I have cell '$A' searching cell'$N'. Cell '$N' contains a VLOOKUP that pulls a few tags from another data sheet. '$A' searches for two words "CS" & "IG". I need my formula for '$A' to show a blank cell if '$N' has pulled no data.

My formula for '$A' =IFERROR(IF(SEARCH("CS",$N20),"CS"),"IG")

The A column should show CS, IG, or just be left blank.

Python : loop in if else statement in a loop

For each iteration in given script, i am importing a data and computing a variable. for simplicity, i am not including all that stuff and lets say i want to print the ID for each of 25 cases. In whatever way, i place the last statement, it always prints - either all iterations for last data or last iteration for all data.

for data in range(0,5):

if data==0:
    for iteration in range(0,5):
        # Import some data and compute some variables
        ID = (data,iteration)

elif data ==1:
    for iteration in range(0,5):
        # Import some data and compute some variables
        ID = (data,iteration)

elif data ==2:
    for iteration in range(0,5):
        # Import some data and compute some variables
        ID = (data,iteration)

elif data ==3:
    for iteration in range(0,5):
        # Import some data and compute some variables
        ID = (data,iteration)           

elif data ==4:
    for iteration in range(0,5):
        # Import some data and compute some variables
        ID = (data,iteration)


ComputedValue = data + iteration                                
print( 'ComputedValue = %.1d for ID = (%.1d,%.1d)' %(ComputedValue,data,iteration) )

Currently it prints only for 5 cases:

ComputedValue = 4 for ID = (0,4)
ComputedValue = 5 for ID = (1,4)
ComputedValue = 6 for ID = (2,4)
ComputedValue = 7 for ID = (3,4)
ComputedValue = 8 for ID = (4,4)

What changes/modifications to be done, so it prints for all 25 cases? Thanks!!

Rachit

C++: Is there a way to declare objects within a conditional statement?

flowchart

I'm having trouble figuring out how to properly create an object depending on the user's choice.

In the program, I ask the user which they class they want to be--Knight or Wizard. I take input '1' or '2' to represent Knight and Wizard.

I made a switch statement, and within case 1, I declared an object Knight, and the same for Wizard.

I need to use these objects outside of the switch statement, but I can't. I tried to make a 'default' object by making 'Player player;' but because the Player class has a pure virtual function, I can't do that either.

How do I do this effectively?

This is what I have so far:

int main()
{
std::string plyrName;
int input;
bool foo = false;

std::cout << "What is your name?\n";
std::cin >> plyrName;
std::cin.ignore(1000, '\n');

std::cout << "\nWelcome, " << plyrName << ". What class would you like to be?\n";
std::cout << "1. Knight.\n2. Wizard.\n";
std::cin >> input;

while (input != 1 && input != 2)
{
    if (foo == true)
        std::cout << "Please enter 1 for Knight and 2 for Wizard.\n";
    if (!(std::cin >> input))
    {
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        std::cout << "\n";
        std::cout << "Only integers are allowed.\n";
    }
    else
        std::cout << "\n";
    foo = true;
}

switch (input)
{
case 1:
{
    Wizard player;
    break;
}
case 2:
{
    Knight player;
    break;
}
}


std::cout << "\nHere is your player information summary.\n";
std::cout << player.classType();

system("pause");
return 0;
}

I need to access the player object after is has been created, because I want to output to the user which class they selected. Both Knight and Wizard classes have a function to output this.

Assert instead of if and for

This test verifies the correctness of the method that transport planes receive from the list of military planes. As in the test not to use "for" and "if", and to use an assertTrue. Also can not use FLAG and lambda. Next to all this should be one Assert.

@Test
public void testGetTransportMilitaryPlanes() {
    Airport airport = new Airport(planes);
    List<MilitaryPlane> transportMilitaryPlanes = airport.getTransportMilitaryPlanes();
    boolean flag = false;
    for (MilitaryPlane militaryPlane : transportMilitaryPlanes) {
        if ((militaryPlane.getType() == MilitaryType.TRANSPORT)) {
            flag = true;
            break;
        }
    }
    Assert.assertEquals(flag, true);
}

I did so:

@Test
public void testGetTransportMilitaryPlanes() {
    Airport airport = new Airport(planes);
    List<MilitaryPlane> transportMilitaryPlanes = airport.getTransportMilitaryPlanes();

    Assert.assertTrue(transportMilitaryPlanes.stream().anyMatch(p -> p.getMilitaryType() == MilitaryType.TRANSPORT));
}

But in the test can not use the lambda. How to do it without lambda?