Cannot convert values ​into mysql - c++

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();

Related

SQLite3 SQL DELETE syntax for two WHERE columns

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] + ";";

Need help to make a RegExp filter to replace redundant Parentheses

For the past few days (weeks, months, years maybe if you count my on-again off-again search and attempts) I've been trying to make or find a RegEx filter to help me remove all redundant parentheses found in my code.
A worst case scenario of what the regex filter will have to deal with is attached. As is a best case scenario
return ((((((((((((((((((((((((((getHumanReadableLine("avHardwareDisable") + getHumanReadableLine("hasAccessibility")) + getHumanReadableLine("hasAudio")) + getHumanReadableLine("hasAudioEncoder")) + getHumanReadableLine("hasEmbeddedVideo")) + getHumanReadableLine("hasIME")) + getHumanReadableLine("hasMP3")) + getHumanReadableLine("hasPrinting")) + getHumanReadableLine("hasScreenBroadcast")) + getHumanReadableLine("hasScreenPlayback")) + getHumanReadableLine("hasStreamingAudio")) + getHumanReadableLine("hasStreamingVideo")) + getHumanReadableLine("hasTLS")) + getHumanReadableLine("hasVideoEncoder")) + getHumanReadableLine("isDebugger")) + getHumanReadableLine("language")) + getHumanReadableLine("localFileReadDisable")) + getHumanReadableLine("manufacturer")) + getHumanReadableLine("os")) + getHumanReadableLine("pixelAspectRatio")) + getHumanReadableLine("playerType")) + getHumanReadableLine("screenColor")) + getHumanReadableLine("screenDPI")) + getHumanReadableLine("screenResolutionX")) + getHumanReadableLine("screenResolutionY")) + getHumanReadableLine("version")));
return ((((name + ": ") + Capabilities[name]) + "\n"));
As you can see there's... a few... redundant parentheses in my code. Been working actively with these for a very long time but have always tried to clean up what I come across and been trying to find a faster way to do it.
So one example of how the "clean" code would look, I'm hoping at least!
return (name + ": " + Capabilities[name] + "\n");
return name + ": " + Capabilities[name] + "\n";
Either one is acceptable to be completely honest as long as the code itself doesn't mock up and change how it works.
I greatly appreciate any answers anyone can give me. Please don't Mock what I do or am trying to achieve. I haven't worked much with regex or similar things before...
And just to humour you... Here's my "RegExp" for my "clean" example
(return) ({1,}((.[^)]{1,}))(.{1,}))(.{1,})){1,}
$1 $2 $3 $4 // output
oh... Forgot to mention
(!(testCrossZ()))
Might appear at times as well but those aren't as big of an issue to clean up manually if needed.
P.S... There is a "LOT" of occurances of the redundant parentheses... Like... Maybe thousands... Most likely thousands.
Not sure if it applies for actionscript, but for Java you can do: Main Menu | Analyze | Run Inspection by Name | type "parentheses" | select "Unnecessary parentheses" | run in the whole project and fix all problems
Result:
return getHumanReadableLine("avHardwareDisable") + getHumanReadableLine("hasAccessibility")
+ getHumanReadableLine("hasAudio") + getHumanReadableLine("hasAudioEncoder")
+ getHumanReadableLine("hasEmbeddedVideo") + getHumanReadableLine("hasIME")
+ getHumanReadableLine("hasMP3") + getHumanReadableLine("hasPrinting")
+ getHumanReadableLine("hasScreenBroadcast") + getHumanReadableLine("hasScreenPlayback")
+ getHumanReadableLine("hasStreamingAudio") + getHumanReadableLine("hasStreamingVideo")
+ getHumanReadableLine("hasTLS") + getHumanReadableLine("hasVideoEncoder")
+ getHumanReadableLine("isDebugger") + getHumanReadableLine("language")
+ getHumanReadableLine("localFileReadDisable") + getHumanReadableLine("manufacturer")
+ getHumanReadableLine("os") + getHumanReadableLine("pixelAspectRatio")
+ getHumanReadableLine("playerType") + getHumanReadableLine("screenColor")
+ getHumanReadableLine("screenDPI") + getHumanReadableLine("screenResolutionX")
+ getHumanReadableLine("screenResolutionY") + getHumanReadableLine("version");
I honestly haven't understood the exact form of the output format that you wanted but as per starters at least clearing off the unnecessary parenthesis can be done with pure JavaScript as follows.
var text = 'return ((((((((((((((((((((((((((getHumanReadableLine("avHardwareDisable") + getHumanReadableLine("hasAccessibility")) + getHumanReadableLine("hasAudio")) + getHumanReadableLine("hasAudioEncoder")) + getHumanReadableLine("hasEmbeddedVideo")) + getHumanReadableLine("hasIME")) + getHumanReadableLine("hasMP3")) + getHumanReadableLine("hasPrinting")) + getHumanReadableLine("hasScreenBroadcast")) + getHumanReadableLine("hasScreenPlayback")) + getHumanReadableLine("hasStreamingAudio")) + getHumanReadableLine("hasStreamingVideo")) + getHumanReadableLine("hasTLS")) + getHumanReadableLine("hasVideoEncoder")) + getHumanReadableLine("isDebugger")) + getHumanReadableLine("language")) + getHumanReadableLine("localFileReadDisable")) + getHumanReadableLine("manufacturer")) + getHumanReadableLine("os")) + getHumanReadableLine("pixelAspectRatio")) + getHumanReadableLine("playerType")) + getHumanReadableLine("screenColor")) + getHumanReadableLine("screenDPI")) + getHumanReadableLine("screenResolutionX")) + getHumanReadableLine("screenResolutionY")) + getHumanReadableLine("version")));',
r = /\(((getHumanReadableLine\("\w+"\)[\s\+]*)+)\)/g,
temp = "";
while (text != temp) {
temp = text;
text = text.replace(r,"$1");
}
document.write('<pre>' + text + '</pre>');
From this point on, it shouldn't be a big deal to convert the reduced text into the desired output format.

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

SPARQL query with filtering

I like to list all drugs that start with some letter to fill autocomplete text box.Here is the code
public string[] GetCompletionList(string prefixText)
{
string rdfDat = AppDomain.CurrentDomain.BaseDirectory + "DrugRDF.rdf";
List<string> list = new List<string>();
TripleStore store = new TripleStore();
Graph rdf = new Graph();
FileLoader.Load(rdf, rdfDat, new RdfXmlParser());
store.Add(rdf);
string tmp = "^" + prefixText;
string sparqlQuery = "PREFIX mojLek: <http://www.example.org/mojLek#>"
+ "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>"
+ "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>"
+ "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"
+ "SELECT ?x"
+ "WHERE {?h mojLek:ime ?x ."
+ "FILTER regex(str(?x),"+tmp+") }";
SparqlQueryParser sparqlParser = new SparqlQueryParser();
SparqlQuery query = sparqlParser.ParseFromString(sparqlQuery);
Object results = store.ExecuteQuery(query);
if (results is SparqlResultSet)
{
SparqlResultSet r = results as SparqlResultSet;
foreach (SparqlResult res in r)
{
list.Add(res["x"].ToString().ToLower());
}
}
return list.ToArray();
}
However if I try it with for example A there are already couples that starts with A I got this error
VDS.RDF.Parsing.RdfParseException: [Line 1 Column 263] The value 'A' is not valid as a QName
at VDS.RDF.Parsing.Tokens.SparqlTokeniser.TryGetQNameOrKeyword()
at VDS.RDF.Parsing.Tokens.SparqlTokeniser.GetNextToken()
at VDS.RDF.Parsing.Tokens.TokenQueue.InitialiseBuffer()
at VDS.RDF.Parsing.SparqlQueryParser.ParseInternal(SparqlQueryParserContext context)
at VDS.RDF.Parsing.SparqlQueryParser.ParseInternal(TextReader input)
at VDS.RDF.Parsing.SparqlQueryParser.ParseFromString(String queryString)
at SuggestWebService.GetCompletionList(String prefixText) in d:\Suggest\App_Code\SuggestWebService.cs:line 57
Put newlines in the query string to make the error messages better.
There are no SPARQL quotes at
regex(str(?x),"+tmp+")
Try:
regex(str(?x),'"+tmp+"')
which puts single quotes into the SPARQL. Be careful of any quotes in tmp.
I have changed my code in this way so it worked for me
string tmp="^"+prefixText;
var query = "PREFIX mojLek: <http://www.example.org/mojLek#>"
+ "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>"
+ "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>"
+ "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"
+ "PREFIX fn: <http://www.w3.org/2005/xpath-functions#>"
+ "SELECT ?x ?h"
+ "WHERE {?h mojLek:ime ?x ."
+ "FILTER regex(?x,\""+tmp+"\")"
+"}";

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.

Categories