SQLite3 SQL DELETE syntax for two WHERE columns - c++

This string works perfect for one column:
delSQL = "DELETE FROM user WHERE Name='" + datArray[0] + "';";
But what looks correct for two columns has error: expected ‘;’ before ‘AND’
delSQL = "DELETE FROM user WHERE Name='" + datArray[0] + "'" AND " Age=" + datArray[1] + ";";
Advice greatly appreciated.

Try this...
delSQL = "DELETE FROM user WHERE Name='" + datArray[0] + "' AND Age=" + datArray[1] + ";";

Related

Possible encoding issue between PS and C++

I have a C++ program written using Qt that I'm using as a front end to create AD accounts. Essentially I launch an elevated process that executes PowerShell commands within an elevated PowerShell session. I can create the accounts fine but when I attempt to pull membership from a pre-existing user to copy it over to the new one it fails. I need to understand why it's failing and resolve the issue, any help is greatly appreciated. It fails with the following error:
Get-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. Provide a valid value for the
argument, and then try running the command again.
At line:2 char:28
+ "}); $groups = (Get-ADUser $tmpusr -Properties MemberOf).MemberOf; $u ...
+ ~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-ADUser], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.GetADUser
The $tmpusr variable is just the value of duser.template_user which is pulled from a QComboBox and is deffinetly not null because it outputs correctly and shows the selected template account. In C++ it's just data added to a struct member:
duser.set_groups_command = "$tmpusr = (Get-ADUser -Filter {Name -like " "\"" + duser.template_user + "\"" "}); "
"$groups = (Get-ADUser $tmpusr -Properties MemberOf).MemberOf; "
"$usr = " "\"" + duser.sam_name + "\"" + "; "
"Foreach ($group in $groups) {Add-ADGroupMember -Identity (Get-ADGroup $group).name -Members $usr} ";
If I strip the C++ and run the same command within PowerShell it executes fine:
$tmpusr = (Get-ADUser -Filter {Name -like "Example User"}); $groups = (Get-ADUser $tmpusr -Properties MemberOf).MemberOf; $usr = "TestUser"; Foreach ($group in $groups) {Add-ADGroupMember -Identity (Get-ADGroup $group).name -Members $usr}
The purpose of the command is to determine which groups "Example User" belongs to and then to add "TestUser" to the same groups. Again, creating the user works fine. That is done with:
duser.complete_command = p + "New-ADUser -Name " + "\"" + duser.employe_name +"\"" + " -GivenName " + "\"" + duser.given_name + "\""
+ " -Surname " + "\"" + duser.surname + "\"" + " -AccountPassword $sec " + " -UserPrincipalName " + "\"" + duser.userpname + "\""
" -DisplayName " + "\"" + duser.display_name + "\"" + " -EmailAddress " + "\"" + duser.email_address + "\"" + " -SamAccountName " +
"\"" + duser.sam_name + "\"" + " -Enabled " + duser.is_enabled;
You'll note the existence of "p" which is another QString created earlier on to convert to a secure string. The only other component is the function that elevates and executes:
void MainWindow::elevate_and_execute(QString param)
{
QProcess *process = new QProcess();
QStringList params = QStringList();
params = QStringList({"-Command", QString("Start-Process -Verb runAs powershell; "), param});
process->startDetached("powershell", params);
process->waitForFinished();
process->terminate();
}
I was able to resolve the issue. I found that when I pulled the so called template user from the QComboBox it contained a carriage return. I had written the logs to a text file and found it was pushing it to the next line; so the $tmpusr was broken. I was able to resolve the issue by modifying the duser.template_user variable when it's initially filled by stripping that "\r" out with remove(QChar('\r'))

Cannot convert values ​into mysql

I want to enter the value of a variable into a MySql database, but it is giving me this error:
I use code like this:
ADOQuery1->SQL->Clear();
ADOQuery1->SQL->Add("insert into data_nasabah(nama_nasabah,tanggal,debit/kredit,saldo,no_rekening)values('"+ns[0].nama+"','"+Date()+"','"+ns[0].dk[0]+"','"+ns[0].saldo[0]+"','"+rekening[n]+"')");
ADOQuery1->ExecSQL();
Column names that have special characters in them need to be quoted with `, or " in ANSI_QUOTES mode. See 9.2 Schema Object Names in MySQL's documentation.
Try this:
ADOQuery1->SQL->Text = "insert into data_nasabah (nama_nasabah, tanggal, `debit/kredit`, saldo, no_rekening) values ('" + ns[0].nama + "','" + Date() + "','" + ns[0].dk[0] + "','" + ns[0].saldo[0] + "','" + rekening[n] + "')";
However, don't format SQL statement parameters manually like you are! This is subject to SQL Injection attacks. You can use Sysutils::QuotedStr() (or Sysutils::AnsiQuotedStr()) to avoid this, eg:
ADOQuery1->SQL->Text = "insert into data_nasabah (nama_nasabah, tanggal, `debit/kredit`, saldo, no_rekening) values (" + QuotedStr(ns[0].nama) + "," + QuotedStr(Date()) + "," + QuotedStr(ns[0].dk[0]) + "," + QuotedStr(ns[0].saldo[0]) + "," + QuotedStr(rekening[n]) + ")";
But, you really should be using a parameterized query instead, let the database engine handle the quoting for you, eg:
// make sure to set ADOQuery1->ParamCheck=true beforehand!
ADOQuery1->SQL->Text = "insert into data_nasabah (nama_nasabah, tanggal, `debit/kredit`, saldo, no_rekening) values (:PNamaNasabah, :PTanggal, :PDebitKredit, :PSaldo, :PNoRekening)";
ADOQuery1->Parameters->ParamByName("PNamaNasabah")->Value = ns[0].nama;
ADOQuery1->Parameters->ParamByName("PTanggal")->Value = Date();
ADOQuery1->Parameters->ParamByName("PDebitKredit")->Value = ns[0].dk[0];
ADOQuery1->Parameters->ParamByName("PSaldo")->Value = ns[0].saldo[0];
ADOQuery1->Parameters->ParamByName("PNoRekening")->Value = rekening[n];
ADOQuery1->ExecSQL();

Accessing data required out of for loop in python and store the data at specific location

I am using a for loop for getting data from the user in command prompt using python 2.7. Then storing the data in a text file in certain format. I am looking for a method to get the data from the user and store it in a list and use it where required.
for Input_Number in range(Number_Of_Inputs):
Input_Number = Input_Number+1
GUI_Parameter = str(raw_input("Please enter input parameter " + str(Input_Number) + " :"))
GUI_Parameter_Name = str(raw_input("Enter the GUI name for the parameter " + str(Input_Number) + " :"))
Store_GUI_Parameter(Opened_File, GUI_Parameter, GUI_Parameter_Name)
I would like to use this data to store it in a specific location in a text file according to required syntax. The above code stores the data in the text file. But the problem is it doesn't store it at the required place.
def Store_GUI_Parameter(Opened_File, GUI_Parameter, GUI_Parameter_Name):
GUI_Description = "| " + '"'+ GUI_Parameter_Name + '"' + " |$" + GUI_Parameter.title() + " |"
Write_Data(Opened_File, GUI_Description)
print "GUI parameters written to NDF file"
return
The data storage is done using the above function...
I tried this, but unfortunately this also is not working
GUI_Parameter= []
GUI_Parameter_Name = []
for Input_Number in range(Number_Of_Inputs):
Input_Number = Input_Number+1
GUI_Parameter[Input_Number] = str(raw_input("Please enter input parameter " + str(Input_Number) + " :"))
GUI_Parameter_Name[Input_Number] = str(raw_input("Enter the GUI name for the parameter " + str(Input_Number) + " :"))
Using it outside the loop in the same function...
GUI_Description(Opened_File, GUI_Parameter_Name[Input_Number], GUI_Parameter[Input_Number])
The function implementation:
def GUI_Description(Opened_File, GUI_Parameter_Name[Input_Number], GUI_Parameter[Input_Number]):
Iteration = 0
while Iteration < Input_Number:
Iteration += 1
GUI_Description = "| " + '"'+ GUI_Parameter_Name[Input_Number] + '"' + " |$" + GUI_Parameter[Input_Number].title() + " |"
Write_Data(Opened_File, GUI_Description)
print "GUI parameters written to NDF file"
return
But it shows syntax error at the def GUI_Description
C:\Users\padmanab\Desktop>python CtoN.py File "CtoN.py", line 173
def GUI_Description(Opened_File, GUI_Parameter_Name[Input_Number], GUI_Parameter[Input_Number]):
^ SyntaxError: invalid syntax
The syntax error in the function GUI_Description is caused by your input arguments. 'GUI_Parameter_Name[Input_Number]' is not a valid input argument. Since your function requires both 'GUI_Parameter_Name' and 'Input_Number' they should be separate input arguments. The code snippet below would solve this syntax error:
def GUI_Description(Opened_File, Input_Number, GUI_Parameter_Name, GUI_Parameter):
...
The code below will give an 'index out of range' error since the lists 'GUI_Parameter' and 'GUI_Parameter_Name' have zero length.
GUI_Parameter= []
GUI_Parameter_Name = []
Number_Of_Inputs = 1
for Input_Number in range(Number_Of_Inputs):
Input_Number = Input_Number+1
GUI_Parameter[Input_Number] = str(raw_input("Please enter input parameter " + str(Input_Number) + " :"))
GUI_Parameter_Name[Input_Number] = str(raw_input("Enter the GUI name for the parameter " + str(Input_Number) + " :"))
If you want to add items to the arrays you should append them:
GUI_Parameter.append(raw_input())

django make log that works for all models

I am trying to make my own log that makes a string of changed data between object (my old object and my new object) However i keep getting back empty string,
My code:
def log_fields(old_obj, new_obj):
fields = new_obj.__class__._meta.fields
changed_fields = ""
old_data = ""
new_data = ""
# get all changed data
for field in fields:
old_field_data = old_obj.__getattribute__(field.name)
new_field_data = new_obj.__getattribute__(field.name)
if old_field_data != new_field_data:
count =+ 1
# convert changed data to strings
# number + space + data + 5 spaces for next string
changed_fields.join(str(count)).join(" ").join(str(field)).join(" ")
old_data.join(str(count)).join(" ").join(str(old_field_data)).join(" ")
new_data.join(str(count)).join(" ").join(str(new_field_data)).join(" ")
print changed_fields
print old_data
print new_data
I got a feeling something with the string .join combination something is going wrong, cause trying this manually in shell seems to work up to the comparison. Not sure tho hos i should change the string
changed_fields = changed_fields + str(count) + "." + str(field.name) + " "
old_data = old_data + str(count) + "." + str(old_field_data) + " "
new_data = new_data + str(count) + "." + str(new_field_data) + " "
Seems to do the job, so for now, ill keep it at this

Regular expression for a language tag (as defined by BCP47)

I need a regular expression for a language tag as defined by BCP 47.
I know that the full BNF syntax is available at http://www.rfc-editor.org/rfc/bcp/bcp47.txt and that I could use it to write my own, but hopefully there is one already out there.
Looks like this:
^((?<grandfathered>(en-GB-oed|i-ami|i-bnn|i-default|i-enochian|i-hak|i-klingon|i-lux|
i-mingo|i-navajo|i-pwn|i-tao|i-tay|i-tsu|sgn-BE-FR|sgn-BE-NL|sgn-CH-DE)|(art-lojban|
cel-gaulish|no-bok|no-nyn|zh-guoyu|zh-hakka|zh-min|zh-min-nan|zh-xiang))|((?<language>
([A-Za-z]{2,3}(-(?<extlang>[A-Za-z]{3}(-[A-Za-z]{3}){0,2}))?)|[A-Za-z]{4}|[A-Za-z]{5,8})
(-(?<script>[A-Za-z]{4}))?(-(?<region>[A-Za-z]{2}|[0-9]{3}))?(-(?<variant>[A-Za-z0-9]{5,8}
|[0-9][A-Za-z0-9]{3}))*(-(?<extension>[0-9A-WY-Za-wy-z](-[A-Za-z0-9]{2,8})+))*
(-(?<privateUse>x(-[A-Za-z0-9]{1,8})+))?)|(?<privateUse>x(-[A-Za-z0-9]{1,8})+))$
Here is the code to generate it (in C#):
var regular = "(art-lojban|cel-gaulish|no-bok|no-nyn|zh-guoyu|zh-hakka|zh-min|zh-min-nan|zh-xiang)";
var irregular = "(en-GB-oed|i-ami|i-bnn|i-default|i-enochian|i-hak|i-klingon|i-lux|i-mingo|i-navajo|i-pwn|i-tao|i-tay|i-tsu|sgn-BE-FR|sgn-BE-NL|sgn-CH-DE)";
var grandfathered = "(?<grandfathered>" + irregular + "|" + regular + ")";
var privateUse = "(?<privateUse>x(-[A-Za-z0-9]{1,8})+)";
var singleton = "[0-9A-WY-Za-wy-z]";
var extension = "(?<extension>" + singleton + "(-[A-Za-z0-9]{2,8})+)";
var variant = "(?<variant>[A-Za-z0-9]{5,8}|[0-9][A-Za-z0-9]{3})";
var region = "(?<region>[A-Za-z]{2}|[0-9]{3})";
var script = "(?<script>[A-Za-z]{4})";
var extlang = "(?<extlang>[A-Za-z]{3}(-[A-Za-z]{3}){0,2})";
var language = "(?<language>([A-Za-z]{2,3}(-" + extlang + ")?)|[A-Za-z]{4}|[A-Za-z]{5,8})";
var langtag = "(" + language + "(-" + script + ")?" + "(-" + region + ")?" + "(-" + variant + ")*" + "(-" + extension + ")*" + "(-" + privateUse + ")?" + ")";
var languageTag = #"^(" + grandfathered + "|" + langtag + "|" + privateUse + ")$";
Console.WriteLine(languageTag);
I cannot guarantee its correctness (I may have made typos), but it works fine on the examples in Appendix A.
Depending on your environment, you might need to remove the named capturing groups "?<...>".
An optimized version that works in PHP.
/^(?<grandfathered>(?:en-GB-oed|i-(?:ami|bnn|default|enochian|hak|klingon|lux|mingo|navajo|pwn|t(?:a[oy]|su))|sgn-(?:BE-(?:FR|NL)|CH-DE))|(?:art-lojban|cel-gaulish|no-(?:bok|nyn)|zh-(?:guoyu|hakka|min(?:-nan)?|xiang)))|(?:(?<language>(?:[A-Za-z]{2,3}(?:-(?<extlang>[A-Za-z]{3}(?:-[A-Za-z]{3}){0,2}))?)|[A-Za-z]{4}|[A-Za-z]{5,8})(?:-(?<script>[A-Za-z]{4}))?(?:-(?<region>[A-Za-z]{2}|[0-9]{3}))?(?:-(?<variant>[A-Za-z0-9]{5,8}|[0-9][A-Za-z0-9]{3}))*(?:-(?<extension>[0-9A-WY-Za-wy-z](?:-[A-Za-z0-9]{2,8})+))*)(?:-(?<privateUse>x(?:-[A-Za-z0-9]{1,8})+))?$/Di
Javascript polices duplicate named capture groups so you have to change the 2nd use of ?<privateUse> to e.g. ?<privateUse1>. Compiles to:
/^((?<grandfathered>(en-GB-oed|i-ami|i-bnn|i-default|i-enochian|i-hak|i-klingon|i-lux|i-mingo|i-navajo|i-pwn|i-tao|i-tay|i-tsu|sgn-BE-FR|sgn-BE-NL|sgn-CH-DE)|(art-lojban|cel-gaulish|no-bok|no-nyn|zh-guoyu|zh-hakka|zh-min|zh-min-nan|zh-xiang))|((?<language>([A-Za-z]{2,3}(-(?<extlang>[A-Za-z]{3}(-[A-Za-z]{3}){0,2}))?)|[A-Za-z]{4}|[A-Za-z]{5,8})(-(?<script>[A-Za-z]{4}))?(-(?<region>[A-Za-z]{2}|[0-9]{3}))?(-(?<variant>[A-Za-z0-9]{5,8}|[0-9][A-Za-z0-9]{3}))*(-(?<extension>[0-9A-WY-Za-wy-z](-[A-Za-z0-9]{2,8})+))*(-(?<privateUse>x(-[A-Za-z0-9]{1,8})+))?)|(?<privateUse1>x(-[A-Za-z0-9]{1,8})+))$/
Here's a way to construct it:
let privateUseUsed = 0
const privateUse = () => "(?<privateUse" + (privateUseUsed++) + ">x(-[A-Za-z0-9]{1,8})+)"
const grandfathered = "(?<grandfathered>" +
/* irregular */ (
"en-GB-oed" +
"|" + "i-(?:ami|bnn|default|enochian|hak|klingon|lux|mingo|navajo|pwn|tao|tay|tsu)" +
"|" + "sgn-(?:BE-FR|BE-NL|CH-DE)"
) +
"|" + /* regular */ (
"art-lojban|cel-gaulish|no-bok|no-nyn|zh-guoyu|zh-hakka|zh-min|zh-min-nan|zh-xiang"
) +
")"
const langtag = "(" +
"(?<language>" + (
"([A-Za-z]{2,3}(-" +
"(?<extlang>[A-Za-z]{3}(-[A-Za-z]{3}){0,2})" +
")?)|[A-Za-z]{4,8})"
) +
"(-" + "(?<script>[A-Za-z]{4})" + ")?" +
"(-" + "(?<region>[A-Za-z]{2}|[0-9]{3})" + ")?" +
"(-" + "(?<variant>[A-Za-z0-9]{5,8}|[0-9][A-Za-z0-9]{3})" + ")*" +
"(-" + "(?<extension>" + (
/* singleton */ "[0-9A-WY-Za-wy-z]" +
"(-[A-Za-z0-9]{2,8})+)"
) +
")*" +
"(-" + privateUse() + ")?" +
")"
const languageTagReStr = "^(" + grandfathered + "|" + langtag + "|" + privateUse() + ")$";
Edit: turns out ff doens't support named capture groups so you have to strip them out with .replace(/\?<a-zA-Z>/g, '') or jest leave them out in the first place:
const grandfathered = "(" +
/* irregular */ "(en-GB-oed|i-ami|i-bnn|i-default|i-enochian|i-hak|i-klingon|i-lux|i-mingo|i-navajo|i-pwn|i-tao|i-tay|i-tsu|sgn-BE-FR|sgn-BE-NL|sgn-CH-DE)" +
"|" +
/* regular */ "(art-lojban|cel-gaulish|no-bok|no-nyn|zh-guoyu|zh-hakka|zh-min|zh-min-nan|zh-xiang)" +
")";
const langtag = "(" +
"(" + (
"([A-Za-z]{2,3}(-" +
"([A-Za-z]{3}(-[A-Za-z]{3}){0,2})" +
")?)|[A-Za-z]{4}|[A-Za-z]{5,8})"
) +
"(-" + "([A-Za-z]{4})" + ")?" +
"(-" + "([A-Za-z]{2}|[0-9]{3})" + ")?" +
"(-" + "([A-Za-z0-9]{5,8}|[0-9][A-Za-z0-9]{3})" + ")*" +
"(-" + "(" + (
/* singleton */ "[0-9A-WY-Za-wy-z]" +
"(-[A-Za-z0-9]{2,8})+)"
) +
")*" +
"(-" + "(x(-[A-Za-z0-9]{1,8})+)" + ")?" +
")";
const languageTag = RegExp("^(" + grandfathered + "|" + langtag + "|" + "(x(-[A-Za-z0-9]{1,8})+)" + ")$");
Test with languageTag.test('en-us')
If using a CLDR-based function set, like PHP's intl extension, you can check if a locale exists in the intl database using a function like:
<?php
function is_locale($locale=''){
// STANDARDISE INPUT
$locale=locale_canonicalize($locale);
// LOAD ARRAY WITH LOCALES
$locales=resourcebundle_locales(NULL);
// RETURN WHETHER FOUND
return (array_search($locale,$locales)!==F);
}
?>
It takes about half a millisecond to load and search the data, so it won't be too much of a performance hit.
Of course, it will only find those in the database of the CLDR version supplied with the PHP version used, but will be updated with each subsequent PHP release.