I am making a program in pascal in which I am asked to read from a file then put the names in alphabetical order. This is the code i have so far.
for counter := 1 to 6 do
begin
readln(infile, name[counter]);
if names[counter] > names[counter+1] then
high:=names[counter];
if names[counter] < names[counter-1] then
low:=names[counter];
end;
for counter:= 1 to 6 do
begin
writeln(names[counter]);
end;
close(infile);
readln()
end.
This probably is a range-check error. I guess you have something like name,names:array[1..6] of string; and you are accessing indexes 0 throgh 7. RTFM: http://www.freepascal.org/docs-html/user/userap4.html
Related
I have mysql table and DBAdvGrid,
decimal numbers of mysql column are shown like 950, 450, 555.45
I would like to be always shown 2 digits after point. like 950.00
I tried event of dataset 'AfterOpen'
TFloatField(MyDs.FieldByName('Price')).DisplayFormat := '0.00';
but did not helps
any help would appreciate.
you can try (use the Dataset direct)
works with mysql and Zeos
(MyDs.FieldByName('Price') as TFloatField).DisplayFormat := '#####0.00';
to get what you want.
More information look here.
Delphi's Database Architecture
UPDATE
About the test
procedure TForm1.Button1Click(Sender: TObject);
begin
ZQuery1.Open;
end;
procedure TForm1.ZQuery1AfterOpen(DataSet: TDataSet);
begin
if CheckBox1.Checked then
(ZQuery1.FieldByName('gebuehr') as
TFloatField).DisplayFormat := '#####0.00';
end;
Field gebuehr Transl.(gebuehr == fee)
Without AfterOpen
With AfterOpen
I'm trying to create a simple function in oracle for my lesson plan and cannot see what I'm doing wrong.
CREATE OR REPLACE FUNCTION ten_pct(num1 IN NUMBER)
RETURN NUMBER
IS
BEGIN
RETURN (num1 * 0.1);
END;
I've tried declaring a variable and setting it equal to the equation and returning it and get the same error.
Error(2,14): PLS-00103: Encountered the symbol "" when expecting one of the following: . # % ; is authid as cluster order using external character deterministic parallel_enable pipelined aggregate result_cache The symbol "" was ignored.
I'm hoping someone sees my mistake.
That code works on my machine
SQL> CREATE OR REPLACE FUNCTION ten_pct(num1 IN NUMBER)
2 RETURN NUMBER
3 IS
4 BEGIN
5 RETURN (num1 * 0.1);
6 END;
7 /
Function created.
Realistically, there is some difference between the code that you posted here and the code that you're actually trying to run.
I'm starting to work with PL/SQL and learning how to write procedures and exceptions but I cant seem to grasp how to show an error. The procedure is a simple one, its only supposed to do simple math as you can see below:
create or replace
procedure get_simple_math
(n_num1 in number,
n_num2 in number,
n_answer out number,
n_err_code out number,
n_err_msg out varchar2)
as
begin
n_answer := (n_num1 / n_num2);
dbms_output.put_line('Answer is '||n_answer);
n_err_code := 0;
exception
when others
then
n_err_code := SQLCODE;
n_err_msg := 'Error in get_simple_math '||SQLERRM;
raise_application_error (-20002,n_err_msg);
end get_simple_math;
As you can see its nothing fancy, but I cant for the life of me get the block to run:
set serveroutput on
declare
n_answer number := 0;
n_err_code number;
n_err_msg varchar2;
begin
get_simple_math(10,5,n_answer);
end;
I'm assuming I need to declare the variables that are set to out, which is why they are there. I also tried adding them to the procedure execute like:
get_simple_math(10,5,n_answer,n_err_code,n_err_msg);
But that didn't seem to do the trick, so I am just looking for some help in solving this problem. Thanks in advance.
Run this and you'll get your answer:
declare
n_answer number := 0;
n_err_code number;
--> n_err_msg varchar2; -- Incorrect
n_err_msg varchar2(300); -- Correct
begin
--> get_simple_math(10,5,n_answer); -- Incorrect call to proc...
get_simple_math(10,5,n_answer, n_err_code, n_err_msg); -- add out parameters
end;
/
General suggestion for the procedure part: use substr - optional:
n_err_msg := 'Error in get_simple_math '||substr(SQLERRM, 1, 250);
I keep getting a error when i run this code, What wrong with the code?
create or replace function f_vars(line varchar2,delimit varchar2 default ',')
return line_type is type line_type is varray(1000) of varchar2(3000);
sline varchar2 (3000);
line_var line_type;
pos number;
begin
sline := line;
for i in 1 .. lenght(sline)
loop
pos := instr(sline,delimit,1,1);
if pos =0 then
line_var(i):=sline;
exit;
endif;
string:=substr(sline,1,pos-1);
line_var(i):=string;
sline := substr(sline,pos+1,length(sline));
end loop;
return line_var;
end;
LINE/COL ERROR
20/5 PLS-00103: Encountered the symbol "LOOP" when expecting one of
the following:
if
22/4 PLS-00103: Encountered the symbol "end-of-file" when expecting
one of the following:
end not pragma final instantiable order overriding static
member constructor map
Stack Overflow isn't really a de-bugging service.
However, I'm feeling generous.
You have spelt length incorrectly; correcting this should fix your first error. Your second is caused by endif;, no space, which means that the if statement has no terminator.
This will not correct all your errors. For instance, you're assigning something to the undefined (and unnecessary) variable string.
I do have more to say though...
I cannot over-emphasise the importance of code-style and whitespace. Your code is fairly unreadable. While this may not matter to you now it will matter to someone else coming to the code in 6 months time. It will probably matter to you in 6 months time when you're trying to work out what you wrote.
Secondly, I cannot over-emphasise the importance of comments. For exactly the same reasons as whitespace, comments are a very important part of understanding how something works.
Thirdly, always explicitly name your function when ending it. It makes things a lot clearer in packages so it's a good habit to have and in functions it'll help with matching up the end problem that caused your second error.
Lastly, if you want to return the user-defined type line_type you need to declare this _outside your function. Something like the following:
create or replace object t_line_type as object ( a varchar2(3000));
create or replace type line_type as varray(1000) of t_line_type;
Adding whitespace your function might look something like the following. This is my coding style and I'm definitely not suggesting that you should slavishly follow it but it helps to have some standardisation.
create or replace function f_vars ( PLine in varchar2
, PDelimiter in varchar2 default ','
) return line_type is
/* This function takes in a line and a delimiter, splits
it on the delimiter and returns it in a varray.
*/
-- local variables are l_
l_line varchar2 (3000) := PLine;
l_pos number;
-- user defined types are t_
-- This is a varray.
t_line line_type;
begin
for i in 1 .. length(l_line) loop
-- Get the position of the first delimiter.
l_pos := instr(l_line, PDelimiter, 1, 1);
-- Exit when we have run out of delimiters.
if l_pos = 0 then
t_line_var(i) := l_line;
exit;
end if;
-- Fill in the varray and take the part of a string
-- between our previous delimiter and the next.
t_line_var(i) := substr(l_line, 1, l_pos - 1);
l_line := substr(l_line, l_pos + 1, length(l_line));
end loop;
return t_line;
end f_vars;
/
I'm currently using this JSON escaping function in PostgreSQL as a stand in for future native JSON support. While it works, it's also limiting our systems performance. How can I go about optimizing it? Maybe some kind of lookup array?
CREATE OR REPLACE FUNCTION escape_json(i_text TEXT)
RETURNS TEXT AS
$body$
DECLARE
idx INTEGER;
text_len INTEGER;
cur_char_unicode INTEGER;
rtn_value TEXT := i_text;
BEGIN
-- $Rev: $ --
text_len = LENGTH(rtn_value);
idx = 1;
WHILE (idx <= text_len) LOOP
cur_char_unicode = ASCII(SUBSTR(rtn_value, idx, 1));
IF cur_char_unicode > 255 THEN
rtn_value = OVERLAY(rtn_value PLACING (E'\\u' || LPAD(UPPER(TO_HEX(cur_char_unicode)),4,'0')) FROM idx FOR 1);
idx = idx + 5;
text_len = text_len + 5;
ELSE
/* is the current character one of the following: " \ / bs ff nl cr tab */
IF cur_char_unicode IN (34, 92, 47, 8, 12, 10, 13, 9) THEN
rtn_value = OVERLAY(rtn_value PLACING (E'\\' || (CASE cur_char_unicode
WHEN 34 THEN '"'
WHEN 92 THEN E'\\'
WHEN 47 THEN '/'
WHEN 8 THEN 'b'
WHEN 12 THEN 'f'
WHEN 10 THEN 'n'
WHEN 13 THEN 'r'
WHEN 9 THEN 't'
END)
)
FROM idx FOR 1);
idx = idx + 1;
text_len = text_len + 1;
END IF;
END IF;
idx = idx + 1;
END LOOP;
RETURN rtn_value;
END;
$body$
LANGUAGE plpgsql;
Confession: I am the Google Summer of Code 2010 student who was going to try to bring JSON support to PostgreSQL 9.1. Although my code was fairly feature-complete , it wasn't completely ready for upstream, and the PostgreSQL development community was looking at some alternative implementations. However, with spring break coming up, I'm hoping to finish my rewrite and give it a final push this week.
In the mean time, you can download and install the work-in-progress JSON data type module, which should work on PostgreSQL 8.4.0 and up. It is a PGXS module, so you can compile and install it without having to compile all of PostgreSQL. However, you will need the PostgreSQL server development headers.
Installation goes something like this:
git clone git://git.postgresql.org/git/json-datatype.git
cd json-datatype/
USE_PGXS=1 make
sudo USE_PGXS=1 make install
psql -f json.sql <DBNAME1> # requires database superuser privileges
Although the build and install only needs to be done once, json.sql needs to be run on every database you plan to use the JSON data type on.
With that installed, you can now run:
=> SELECT to_json(E'"quotes and \n newlines"\n'::TEXT);
to_json
--------------------------------
"\"quotes and \n newlines\"\n"
(1 row)
Note that this does not escape non-ASCII characters.
All my approaches boil down to "do it some other way":
Write it in some other language, e.g. use pl/perl, pl/python, pl/ruby
Write a wrapper round some external JSON library written in C
Do the JSON escaping in the client rather than in the query (assuming your client has some good JSON escaping support)
In my experience pl/pgsql isn't fast at this sort of thing- its strength is in its integral support for exchanging data with the database, not as a general-purpose programming language.
Example:
create or replace function escape_json_perl(text) returns text
strict immutable
language plperlu as $$
use JSON;
return JSON->new->allow_nonref->encode($_[0]);
$$;
A quick test suggests this is on the order of 15x faster than the plpgsql function (although it returns quotes around the value which you probably want to strip off)
I have found a PostgreSQL function implemented in C here : http://code.google.com/p/pg-to-json-serializer/
I have not compared it with your PLSQL method but it should be faster than any interpreted language.
Another one : http://miketeo.net/wp/index.php/projects/json-functions-for-postgresql