dbi

mysql select query optimization and how limit works in mysql

我只是一个虾纸丫 提交于 2019-12-04 12:18:43
问题 I am using mysql database which has only one table "data" with 17,151257 rows.This table has a column string . i want to print all the rows in which string column contains a particular query string (stored in "entered_query" variable), so i used following: SELECT DISTINCT * from data WHERE string LIKE '%".$entered_query."%' limit 10 As obvious above query is taking too much time to execute. I have read that indexing can be used but how in this case ? I have also thought of dividing whole data

How do I connect with Perl to SQL Server?

好久不见. 提交于 2019-12-04 07:56:41
问题 I have a user id, password, database name and datasource details. I want to connect with Perl to a MSSQL server. I just used the following snippet, but I am getting an error. #!/usr/bin/perl -w use strict; use DBI; my $data_source = q/dbi:ODBC:192.168.3.137/; my $user = q/bharani/; my $password = q/123456/; # Connect to the data source and get a handle for that connection. my $dbh = DBI->connect($data_source, $user, $password) or die "Can't connect to $data_source: $DBI::errstr"; My error is:

Perl DBI fetchall_hashref

泪湿孤枕 提交于 2019-12-04 04:22:59
Consider the following table: mysql> select * from vCountryStatus; +-------------+------------+------+---------+--------+-----------------+ | CountryName | CountryISO | Code | Status | Symbol | CurrencyName | +-------------+------------+------+---------+--------+-----------------+ | Brazil | BR | 55 | LIVE | BRL | Brazilian Real | | France | FR | 33 | offline | EUR | Euro | | Philippines | PH | 63 | LIVE | PHP | Philippino Peso | +-------------+------------+------+---------+--------+-----------------+ 3 rows in set (0.00 sec) I am trying to construct a hash based on this table. For this I do

Perl DBI - Capturing errors

给你一囗甜甜゛ 提交于 2019-12-04 02:36:05
What's the best way of capturing any DBI errors in Perl? For example if an insert fails because there were illegal characters in the values being inserted, how can I not have the script fail, but capture the error and handle it appropriately. I don't want to do the "or die" because I don't want to stop execution of the script. Ether Use the RaiseError=>1 configuration in DBI->connect , and wrap your calls to the $dbh and $sth in a try block ( TryCatch and Try::Tiny are good implementations for try blocks). See the docs for more information on other connect variables available. for example: use

How can I get column names and row data in order with DBI in Perl?

岁酱吖の 提交于 2019-12-04 01:10:35
I'm using DBI to query a SQLite3 database. What I have works, but it doesn't return the columns in order. Example: Query: select col1, col2, col3, col4 from some_view; Output: col3, col2, col1, col4 3, 2, 1, 4 3, 2, 1, 4 3, 2, 1, 4 3, 2, 1, 4 ... (values and columns are just for illustration) I know this is happening because I'm using a hash , but how else do I get the column names back if I only use an array? All I want to do is get something like this for any arbitrary query: col1, col2, col3, col4 1, 2, 3, 4 1, 2, 3, 4 1, 2, 3, 4 1, 2, 3, 4 ... (That is, I need the output is in the right

JRruby, Sybase JDBC and DBI - fetching column name with the AS clause issue

久未见 提交于 2019-12-03 21:53:18
I have a ruby script which I run using the JRuby Interpreter. The script connects to a Sybase database using DBI and Sybase JDBC (jTDS3.jar and jconn3.jar) My problem is that I have a select query that alters the column names of table. For example: SELECT t.TRANSACTION as 'business_transaction', t.TRADE_CURRENCY as 'currency', t.CURRENCY as 'settlement_currency' ...etc... FROM TRADE t ...etc... My problem is when using the examples directly from the documentation sth = dbh.execute(stmt) printf "Number of rows: %d\n", rows.size printf "Number of columns: %d\n", sth.column_names.size sth.column

When to use $sth->fetchrow_hashref, $sth->fetchrow_arrayref and $sth->fetchrow_array?

半世苍凉 提交于 2019-12-03 17:15:19
问题 I know that: $sth->fetchrow_hashref returns a hashref of the fetched row from database, $sth->fetchrow_arrayref returns an arrayref of the fetched row from database, and $sth->fetchrow_array returns an array of the fetched row from database. But I want to know best practices about these. When should we use fetchrow_hashref and when should we use fetchrow_arrayref and when should we use fetchrow_array? 回答1: When I wrote YAORM for $work, I benchmarked all of these in our environment (MySQL) and

DBI::InterfaceError: Could not load driver (uninitialized constant MysqlError)

て烟熏妆下的殇ゞ 提交于 2019-12-03 16:15:01
I have included gems, dbd-mysql (0.4.4) dbi (0.4.5) mysql (2.8.1) on rails console when I run the following code, require 'rubygems' require "dbi" require 'dbd-mysql' dbh = DBI.connect("DBI:Mysql:TestDB:localhost","username", "pwd") 1.9.2-p180 :001 > require 'rubygems' => false 1.9.2-p180 :002 > require "dbi" => false 1.9.2p180 :003 > require 'dbd-mysql' LoadError: no such file to load -- dbd-mysql from /.rvm/gems/ruby-1.9.2-p180@rails3/gems/dbi-0.4.5/lib/dbi.rb:318:in `rescue in load_driver' from /.rvm/gems/ruby-1.9.2-p180@rails3/gems/dbi-0.4.5/lib/dbi.rb:242:in `load_driver' from /.rvm/gems

When to use $sth->fetchrow_hashref, $sth->fetchrow_arrayref and $sth->fetchrow_array?

折月煮酒 提交于 2019-12-03 07:11:59
I know that: $sth->fetchrow_hashref returns a hashref of the fetched row from database, $sth->fetchrow_arrayref returns an arrayref of the fetched row from database, and $sth->fetchrow_array returns an array of the fetched row from database. But I want to know best practices about these. When should we use fetchrow_hashref and when should we use fetchrow_arrayref and when should we use fetchrow_array? When I wrote YAORM for $work, I benchmarked all of these in our environment (MySQL) and found that arrayref performed the same as array, and hashref was much slower. So I agree, it is best to use

How can I fetch a single count value from a database with DBI?

六月ゝ 毕业季﹏ 提交于 2019-12-03 05:57:45
The following code seems to be just too much, for getting a single count value. Is there a better, recommended way to fetch a single COUNT value using plain DBI? sub get_count { my $sth = $dbh->prepare("SELECT COUNT(*) FROM table WHERE..."); $sth->execute( @params ); my $($count) = $sth->fetchrow_array; $sth->finish; return $count; } This is shorter, but I still have two statements. sub get_count_2 { my $ar = $dbh->selectall_arrayref("SELECT ...", undef, @params) return $ar->[0][0]; } Easy enough to do in one line with no extra variables: $count = $dbh->selectrow_array('SELECT count(*) FROM