I have a perl script. The simplified version looks like this:
my $sel = "SELECT DBMS_METADATA.GET_DDL($type, '$name', '$owner') FROM DUAL";
my $sth = $dbh->prepare( $sel ) or die "Can't prepare statement: $DBI::errstr";
my $rc = $sth->execute or die "Can't execute statement: $DBI::errstr";
my $code = $sth->fetchrow();
$sql = "INSERT INTO MY_TABLE (CODE) VALUES (?)";
$stmt = $dbh->prepare($sql);
$stmt->execute($code);
So, the script simpy get a CLOB text, save it in a $code variable and insert it to a table.
The command SELECT * FROM NLS_DATABASE_PARAMETERS WHERE PARAMETER = 'NLS_CHARACTERSET'; returns WE8MSWIN1252.
My problem is, that in the $code variable occur German characters. After executing the INSERT to DB the encoding is wrong. The geman special characters(umlauts) are displayed not correctly. guess_encoding($code); returns Encode::XS=SCALAR(0x...).
How can I decode and encode the $code variable in a proper way? How can I check what the guess_encoding returns? As already mentioned, the 'CODE' field is of type CLOB.
Related
I have a perl code which queries the Oracle 11G database to pull the count of the 'values' from a table.
$sqlStatement="SELECT count(values) FROM Table WHERE values IN ('value1','value2','value3',.... 'valuen')"
How can I pass the values in query through a subroutine's argument?
For example:
$sqlStatement="SELECT count(values) FROM Table WHERE values IN ($values)
subroutine($values)
Wouldn't it make more sense to pass in the values using an array rather than a scalar?
count_values(#values);
Then the subroutine could start like this:
sub count_values {
my #values = #_;
my $sql = 'select count(values) from table where values in (';
$sql .= join ',', ('?') x #values;
$sql .= ')';
my $sth = $dbh->prepare($sql);
$sth->execute(#values);
my $count = ($sth->fetchrow_array)[0];
}
How to use like clause in code iginiter query() WITH BIND PARAMS?
Eg:
When I use
$query = 'SELECT mycol FROM mytable WHERE name LIKE %?';
$name = 'foo';
$db->this->query($query,array($name));
//the clause generated
//SELECT mycol FROM mytable WHERE id LIKE '%'foo'%'
//I expected this
//SELECT mycol FROM mytable WHERE id LIKE '%foo'
I don't to put param values inside query and use like below:
$query = 'SELECT mycol FROM mytable WHERE name LIKE '%foo';
Also I can not use $this->db->like() function as my query consists of:
INSERT IGNORE
and
INSERT INTO table SELECT col FROM table2;
Please suggests?
Thanks,
just use CodeIgniter Query Builder
$name = 'foo';
$query = $this
->db
->select('mycol')
->like('name', $name)
->get('mytable');
$query = 'SELECT mycol FROM mytable WHERE name LIKE ?';
$name = '%foo';
$this->db->query($query,array($name));
codeigniter will replace ? with 'params' value.
if you write this
$query = 'SELECT mycol FROM mytable WHERE name LIKE %?';
$name = 'foo';
//$db->this->query($query,array($name)); //you wrote this line wrong.
//it should be like this
$this->db->query($query,array($name));
it will produce
SELECT mycol FROM mytable WHERE name LIKE %'foo' //inverse comma after % ,actually before and after foo.
So your right way will be
$query = 'SELECT mycol FROM mytable WHERE name LIKE ?';
$name = '%foo';
$this->db->query($query,array($name));
It will produce
SELECT mycol FROM mytable WHERE name LIKE '%foo'
NOTE
You wrote this which is wrong
$db->this->query($query,array($name));
Right way
$this->db->query($query,array($name));
$code = Array(1,2,3,4)
$sql = "SELECT * FROM Table1 WHERE Field1 IN (?)";
$query = $this->db->query($sql, array($code));
$this->db->last_query() will show
"SELECT * FROM Table1 WHERE Field1 IN ('1,2,3,4')"
How can I remove the single quote in the IN condition?
Even if the $code is array of strings, example
$code = Array('This one', 'Next code', 'And this')
the statement will be:
"SELECT * FROM Table1 WHERE Field1 IN ('This one, Next Code, And This')"
Am I missing something ?
TIA.
You can use this simple and alternate way
$this->db->where_in('Field1',$code);
$this->db->get('Table1');
From codeigniter active record manual
$this->db->where() accepts an optional third parameter. If you set it
to FALSE, CodeIgniter will not try to protect your field or table
names with backticks.
$this->db->where('MATCH (field) AGAINST ("value")', NULL, FALSE);
So, put a 3rd parameter on a where clause with FALSE
Then your query should be
$this->db->select('*')
->where('Field1 IN('.$code.'),NULL,FALSE)
->get('Table1');
I have getting data from a database and using
$query = $this->db->query("SELECT * FROM mytable");
echo $this->dbutil->csv_from_result($query);
however I want to change the name of my headers for the CSV for first_name is First Name...how would I go about doing that?
Thanks,
J
Try renaming the columns returned by the sql query:
$query = $this->db->query("
SELECT
first_name as 'First Name', last_name as 'Last Name'
FROM mytable
");
The downside is that you will have to list the columns instead of a simple *.
I have a an sqlite database with the table test. Several processes are accessing this database from bash. The table has the following fields:
CREATE TABLE mytable (id NUMERIC,
start JULIAN,
finish JULIAN)
I obtain an unique id by:
id=$(sqlite test.db <<EOF
BEGIN EXCLUSIVE;
SELECT id FROM mytable WHERE start IS NULL ORDER BY RANDOM() LIMIT 1;
COMMIT;
EOF
)
My question is, how can update the field start with:
UPDATE mytable set start=julianday('now') where id="SELECTED ID FROM ABOVE";
In the same statement?
Based on the comments that you supplied above, my solution would look something like follows (in perl with a raw DBI connection, also i didn't do a lot of error checking or anything either, something that you should probably do):
my $dbh = DBI->connect(...);
$dbh->do("BEGIN EXCLUSIVE");
my $stm = $dbh->prepare("SELECT id FROM mytable WHERE start IS NULL ORDER BY RANDOM() LIMIT 1");
$stm->execute();
my $row = $stm->fetchrow_hashref();
my $id = undef;
if ( $row ) {
$id = $row->{ID};
my $ustm = $dbh->prepare("UPDATE mytable set start=julianday('now') where id=?");
$ustm->execute($id);
}
$dbh->do("COMMIT");
# Still have the id at this point.