Simple mysqli_query Insert into table (auto_increment) - mysqli

msa6#outlook.com
postArray ( [zip] => 81007 [st] => co [dist] => 13 [email] => msa6#outlook.com [pw] => msa12345 [sq] => hot [sa] => dog [comment] => 8/8 original )
INSERT INTO mbr (zip,st,dist,email,pw,sq,sa) VALUES (81007,co,13,msa6#outlook.com,msa12345,hot,dog)
Problem in recording your member data. Please try again later. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '#outlook.com,msa12345,hot,dog)' at line 1

varchars or characters should be enclosed in single quotes
Example:
'msa6#outlook.com'

Related

How do I use the Class::DBI->sequence() method to fill 'id' field automatically in perl?

I'm following the example Class::DBI.
I create the cd table like that in my MariaDB database:
CREATE TABLE cd (
cdid INTEGER PRIMARY KEY,
artist INTEGER, # references 'artist'
title VARCHAR(255),
year CHAR(4)
);
The primary key cdid is not set to auto-incremental. I want to use a sequence in MariaDB. So, I configured the sequence:
mysql> CREATE SEQUENCE cd_seq START WITH 100 INCREMENT BY 10;
Query OK, 0 rows affected (0.01 sec)
mysql> SELECT NEXTVAL(cd_seq);
+-----------------+
| NEXTVAL(cd_seq) |
+-----------------+
| 100 |
+-----------------+
1 row in set (0.00 sec)
And set-up the Music::CD class to use it:
Music::CD->columns(Primary => qw/cdid/);
Music::CD->sequence('cd_seq');
Music::CD->columns(Others => qw/artist title year/);
After that, I try this inserts:
# NORMAL INSERT
my $cd = Music::CD->insert({
cdid => 4,
artist => 2,
title => 'October',
year => 1980,
});
# SEQUENCE INSERT
my $cd = Music::CD->insert({
artist => 2,
title => 'October',
year => 1980,
});
The "normal insert" succeed, but the "sequence insert" give me this error:
DBD::mysql::st execute failed: You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax to use near ''cd_seq')' at line
1 [for Statement "SELECT NEXTVAL ('cd_seq')
"] at /usr/local/share/perl5/site_perl/DBIx/ContextualFetch.pm line 52.
I think the quotation marks ('') are provoking the error, because when I put the command "SELECT NEXTVAL (cd_seq)" (without quotations) in mysql client it works (see above). I proved all combinations (', ", `, no quotation), but still...
Any idea?
My versions: perl 5.30.3, 10.5.4-MariaDB
The documentation for sequence() says this:
If you are using a database with AUTO_INCREMENT (e.g. MySQL) then you do not need this, and any call to insert() without a primary key specified will fill this in automagically.
MariaDB is based on MySQL. Therefore you do not need the call to sequence(). Use the AUTO_INCREMENT keyword in your table definition instead.

Oracle dbms_scheduler error with BYTIME

I was trying to get a job run every business day (MON to SAT) at 6:30am which the Oracle scheduler refused with
ORA-27419 "unable to determine valid execution date from repeat
interval"
I started losing my mind when i discovered the following behaviour:
First, create a dummy job. Note that it has no schedule and is not enabled.
BEGIN
DBMS_SCHEDULER.CREATE_JOB (
job_name => '"TMP_DUMMY"',
job_type => 'PLSQL_BLOCK',
job_action => 'begin
dbms_lock.sleep(5);
end;',
number_of_arguments => 0,
start_date => NULL,
end_date => NULL,
enabled => FALSE,
auto_drop => FALSE,
comments => 'Test Job');
DBMS_SCHEDULER.SET_ATTRIBUTE(
name => '"TMP_DUMMY"', attribute => 'store_output', value => TRUE);
DBMS_SCHEDULER.SET_ATTRIBUTE( name => '"TMP_DUMMY"', attribute => 'logging_level', value => DBMS_SCHEDULER.LOGGING_OFF);
END;
/
Next step, set a repeat_interval using BYTIME with any execution time which is equal to or less than 02:55 (MI:SS) after the full hour. It does not matter whether this is done with or without the Hour part and for the former option the exact hour does not matter as well.
BEGIN
DBMS_SCHEDULER.set_attribute( name => '"TMP_DUMMY"', attribute => 'repeat_interval', value => 'FREQ=DAILY;BYTIME=010255');
DBMS_SCHEDULER.enable(name=>'"TMP_DUMMY"');
END;
/
This works perfectly fine for me.
Now i want to increase the BYTIME by 1 second to 02:56 (MI:SS)
BEGIN
DBMS_SCHEDULER.set_attribute( name => '"TMP_DUMMY"', attribute => 'repeat_interval', value => 'FREQ=DAILY;BYTIME=010256');
END;
/
Running this attribute change i get
ORA-27470: failed to re-enable "[schema]"."TMP_DUMMY" after making requested change
ORA-27419: unable to determined valid execution date from repeat interval
I have verified this behaviour for all MI:SS combinations:
set serveroutput on
DECLARE
l_rep_interval VARCHAR2(50 CHAR);
BEGIN
FOR mi IN 0..59
LOOP
FOR ss IN 0..59
LOOP
l_rep_interval := 'FREQ=DAILY;BYTIME='||lpad(to_char(mi*100+ss),4,'0');
DBMS_SCHEDULER.set_attribute( name => '"TMP_DUMMY"', attribute => 'repeat_interval', value => l_rep_interval);
DBMS_SCHEDULER.enable(name=>'"TMP_DUMMY"');
DBMS_OUTPUT.PUT_LINE(l_rep_interval);
END LOOP; --end ss
END LOOP; --end mi
EXCEPTION WHEN OTHERS THEN NULL;
END;
/
It is working properly from 00:00 until 02:55 and fails for all other times.
For me this looks like the MI:SS part is treated as a tinyint and higher values cause a type overflow.
Is this a Bug in the scheduler or am i missing something here?
Oracle version is 12c.
I just ran into this same issue. It looks like (from the documentation) that BYTIME is not recognised (even though SQLDeveloper uses it if you look at the SQL tab of the job GUI)
I found that the following works using BYMinute and ByHour
DBMS_SCHEDULER.set_attribute( name => '"TEST"."JOB"', attribute => 'repeat_interval', value => 'FREQ=DAILY;BYHOUR=9;BYMINUTE=30;BYDAY=MON,TUE,WED,THU,FRI,SAT,SUN');
Hope this helps someone.

Syntax error in where clause in postgresql

fl=s.executeUpdate("
insert into demi(rno,subcode,subname,intm,extm,crd,resultdate)
values(
'13JG1A05A0',
'RT22058',
' FREE OPEN SOURCE SOFTWARE(FOSS) LAB ',
'20',
'70',
'2',
'MAY 2015'
)
end where not exists(SELECT * FROM demi WHERE rn0 ='13JG105A0' AND subcode='RT22058')
");
I'm working in jsp with postgresql as backend, my IDE shows error in this statement.
i want to insert a record into db after checking and making sure that no such record already exists
Is this statement correct, or am I trying a garbage code?
Please help, thanks in advance
The reason for your error message is that an INSERT statement does not allow a WHERE clause.
You can only add a where clause to a SELECT statement (or a DELETE or UPDATE statement)
So you would need to get rid of the VALUES clause and use the insert into .. select ... syntax:
insert into demi(rno,subcode,subname,intm,extm,crd,resultdate)
select '13JG1A05A0',
'RT22058',
' FREE OPEN SOURCE SOFTWARE(FOSS) LAB ',
'20',
'70',
'2',
'MAY 2015'
where not exists (SELECT *
FROM demi
WHERE rn0='13JG105A0'
AND subcode='RT22058');
However for the intended use case:
I'm trying to insert a particular record into db if and only if there exists no other record with same rno and subject code columns
there is a better alternative if you have a unique constraint on (rno, subcode) (which you should) - use the on conflict clause:
insert into demi (rno, subcode, subname, intm, extm, crd, resultdate)
values
(
'13JG1A05A0',
'RT22058',
' FREE OPEN SOURCE SOFTWARE(FOSS) LAB ',
'20',
'70',
'2',
'MAY 2015'
)
on conflict (rno, subcode) do nothing;
Again: the on conflict will only work if you have a unique constraint (or index) on those two columns.
Unrelated, but:
you should specify each constant value with a literal matching the underlying data type. '20' is a character constant, 20 would be a number. Ìf intm, extm and crd re numbers, don't provide character values. Also if resultdate is a date column 'MAY 2015' won't work either.
Not sure how
end where not exists(SELECT * FROM demi WHERE rn0 ='13JG105A0' AND subcode='RT22058')
got into your code, but the whole thing is suspicious what do you even want that to do?

Interpreting Perl DBI MySQL column_info()

I'm trying to write Perl code that accepts bindings for a SQL INSERT statement, and identifies problems that might cause the INSERT to be rejected for bad data, and fixes them. To do this, I need to get and interpret column metadata.
The $dbh->column_info method returns the info in a coded form. I've pored through the official CPAN documentation but am still confused.
my $sth_column_info
= $dbh->column_info( $catalog, $schema, $table, undef );
my $columns_aoh_ref = $sth_column_info->fetchall_arrayref(
{ COLUMN_NAME => 1,
DATA_TYPE => 1,
NULLABLE => 1,
ORDINAL_POSITION => 1,
COLUMN_DEF => 1,
}
);
say $table;
for my $href (#$columns_aoh_ref) {
my #list;
while ( my ( $k, $v ) = each %$href ) {
push #list, "$k=" . ( $v // 'undef' );
}
say join '|', #list;
}
Output is:
dw_phone
NULLABLE=0|COLUMN_DEF=undef|DATA_TYPE=4|ORDINAL_POSITION=1|COLUMN_NAME=phone_id
NULLABLE=0|COLUMN_DEF=undef|DATA_TYPE=4|ORDINAL_POSITION=2|COLUMN_NAME=phone_no
NULLABLE=1|COLUMN_DEF=undef|DATA_TYPE=4|ORDINAL_POSITION=3|COLUMN_NAME=phone_ext
NULLABLE=0|COLUMN_DEF=undef|DATA_TYPE=1|ORDINAL_POSITION=4|COLUMN_NAME=phone_type
NULLABLE=0|COLUMN_DEF=undef|DATA_TYPE=1|ORDINAL_POSITION=5|COLUMN_NAME=phone_location
NULLABLE=1|COLUMN_DEF=undef|DATA_TYPE=1|ORDINAL_POSITION=6|COLUMN_NAME=phone_status
NULLABLE=0|COLUMN_DEF=undef|DATA_TYPE=11|ORDINAL_POSITION=7|COLUMN_NAME=insert_date
NULLABLE=0|COLUMN_DEF=undef|DATA_TYPE=11|ORDINAL_POSITION=8|COLUMN_NAME=update_date
Where - for example - does one find a mapping of data type codes to strings? Should I be using DATA_TYPE, TYPE_NAME, or SQL_DATA_TYPE? Should I be using NULLABLE or IS_NULLABLE, and why the two flavors?
I can appreciate the difficulty of documenting (let alone implementing) a universal interface for databases. But I wonder if anyone knows of a reference manual for using the DBI that's specific to MySQL?
UPDATE 1:
Tried to shed more light by retrieving all info using an array rather than a hash:
my $sth_column_info
= $dbh->column_info( $catalog, $schema, $table, undef );
my $aoa_ref = $sth_column_info->fetchall_arrayref; # <- chg. to arrayref, no parms
say $table;
for my $aref (#$aoa_ref) {
my #list = map $_ // 'undef', #$aref;
say join '|', #list;
}
Now I can see lots of potentially useful information mixed in there.
dw_contact_source
undef|dwcust1|dw_contact_source|contact_id|4|BIGINT|20|undef|undef|10|0|undef|undef|4|undef|undef|1|NO|undef|undef|undef|undef|undef|undef|undef|undef|undef|undef|undef|undef|undef|undef|undef|undef|undef|undef|1|bigint(20)|undef|0
undef|dwcust1|dw_contact_source|company_id|4|SMALLINT|6|undef|undef|10|0|undef|undef|4|undef|undef|2|NO|undef|undef|undef|undef|undef|undef|undef|undef|undef|undef|undef|undef|undef|undef|undef|undef|undef|undef|1|smallint(6)|undef|0
undef|dwcust1|dw_contact_source|contact_type_id|4|TINYINT|4|undef|undef|10|0|undef|undef|4|undef|undef|3|NO|undef|undef|undef|undef|undef|undef|undef|undef|undef|undef|undef|undef|undef|undef|undef|undef|undef|undef||tinyint(4)|undef|0
undef|dwcust1|dw_contact_source|insert_date|11|DATETIME|19|undef|0|undef|0|undef|undef|9|-79|undef|4|NO|undef|undef|undef|undef|undef|undef|undef|undef|undef|undef|undef|undef|undef|undef|undef|undef|undef|undef||datetime|undef|0
undef|dwcust1|dw_contact_source|update_date|11|DATETIME|19|undef|0|undef|0|undef|undef|9|-79|undef|5|NO|undef|undef|undef|undef|undef|undef|undef|undef|undef|undef|undef|undef|undef|undef|undef|undef|undef|undef||datetime|undef|0
So my question would be:
How do I get the corresponding names/descriptions of these metadata?
How do I fetchall_arrayref just what I need, using symbols rather than integers? (I tried fetchall_arrayref([qw/COLUMN_NAME DATA_TYPE/]) and got back all undefs; now I'm just flailing about guessing.)
UPDATE 2:
Now I'm digging around in DBD::mysql.pm and I found a very interesting array:
my #names = qw(
TABLE_CAT TABLE_SCHEM TABLE_NAME COLUMN_NAME
DATA_TYPE TYPE_NAME COLUMN_SIZE BUFFER_LENGTH DECIMAL_DIGITS
NUM_PREC_RADIX NULLABLE REMARKS COLUMN_DEF
SQL_DATA_TYPE SQL_DATETIME_SUB CHAR_OCTET_LENGTH
ORDINAL_POSITION IS_NULLABLE CHAR_SET_CAT
CHAR_SET_SCHEM CHAR_SET_NAME COLLATION_CAT COLLATION_SCHEM COLLATION_NAME
UDT_CAT UDT_SCHEM UDT_NAME DOMAIN_CAT DOMAIN_SCHEM DOMAIN_NAME
SCOPE_CAT SCOPE_SCHEM SCOPE_NAME MAX_CARDINALITY
DTD_IDENTIFIER IS_SELF_REF
mysql_is_pri_key mysql_type_name mysql_values
mysql_is_auto_increment
);
These correspond precisely to what is returned by fetchall_arrayref. Now I can see I have four choices for learning data type, so let's see if any of the codes are documented.
UPDATE 3:
DBI Recipes is a very nice adjunct to CPAN DBI documentation about retrieving info back into Perl (Especially the {select|fetch}{row|all}_{hash|array} methods.)
This will help you determine the values for the data_types. I normally use the data_type to determine how to handle a column based on its type.
You then need to look at the MySQL data type key below and get the hash value. Then look at the DBI table below and match up the data name to get the data type value. Example: a BIGINT is an INTEGER type which matches SQL_INTEGER so the DATA_TYPE value is 4,
DBD::MySQL
### ANSI datatype mapping to mSQL datatypes
%DBD::mysql::db::ANSI2db = ("CHAR" => "CHAR",
"VARCHAR" => "CHAR",
"LONGVARCHAR" => "CHAR",
"NUMERIC" => "INTEGER",
"DECIMAL" => "INTEGER",
"BIT" => "INTEGER",
"TINYINT" => "INTEGER",
"SMALLINT" => "INTEGER",
"INTEGER" => "INTEGER",
"BIGINT" => "INTEGER",
"REAL" => "REAL",
"FLOAT" => "REAL",
"DOUBLE" => "REAL",
"BINARY" => "CHAR",
"VARBINARY" => "CHAR",
"LONGVARBINARY" => "CHAR",
"DATE" => "CHAR",
"TIME" => "CHAR",
"TIMESTAMP" => "CHAR"
);
DBI.pm
TYPE
The TYPE attribute contains a reference to an array of integer values representing the international standard values for the respective datatypes. The array of integers has a length equal to the number of columns selected within the original statement, and can be referenced in a similar way to the NAME attribute example shown earlier.
The standard values for common types are:
SQL_CHAR 1
SQL_NUMERIC 2
SQL_DECIMAL 3
SQL_INTEGER 4
SQL_SMALLINT 5
SQL_FLOAT 6
SQL_REAL 7
SQL_DOUBLE 8
SQL_DATE 9
SQL_TIME 10
SQL_TIMESTAMP 11
SQL_VARCHAR 12
SQL_LONGVARCHAR -1
SQL_BINARY -2
SQL_VARBINARY -3
SQL_LONGVARBINARY -4
SQL_BIGINT -5
SQL_TINYINT -6
SQL_BIT -7
SQL_WCHAR -8
SQL_WVARCHAR -9
SQL_WLONGVARCHAR -10
While these numbers are fairly standard,[61] the way drivers map their native types to these standard types varies greatly. Native types that don't correspond well to one of these types may be mapped into the range officially reserved for use by the Perl DBI: -9999 to -9000.

How can I find a bounding box that encapsulates a specific point?

[uppercaseName] => ATLANTA, GA
[description] => Atlanta, GA
[name] => Atlanta, GA
[_id] => MongoId Object (
)
[addedOn] => MongoDate Object (
[sec] => 1318879015
[usec] => 517000
)
[excludePoints] => Array (
)
[boundingBox] => Array (
[0] => Array (
[lon] => -84.516
[lat] => 33.6747
)
[1] => Array (
[lon] => -84.516
[lat] => 33.8232
)
[2] => Array (
[lon] => -84.2599
[lat] => 33.8232
)
[3] => Array (
[lon] => -84.2599
[lat] => 33.6747
)
)
That's my document (in MongoDB). I have several such documents and I want to run a query to find all documents that have a bounding box that encapsulates that specific Long and Lat. How would I do this?
Unfortunately, this is not currently possible with MongoDB. MongoDB can index points and find all of the documents inside an area, but cannot index areas and query all of the documents containing an area that encloses a gives point.
There is a feature request for this: https://jira.mongodb.org/browse/SERVER-2874
Presently there is no scheduled date for this feature. Please vote for it!
For more information on Geospatial Indexing and what it is capable of, please see the MongoDB documentation:
http://www.mongodb.org/display/DOCS/Geospatial+Indexing