dbi

What's the internals of a prepared statement like? [closed]

非 Y 不嫁゛ 提交于 2019-12-08 12:27:09
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 5 years ago . Here's how bind_params seem to be preparing sql statements: stmt = db.prepare( "select * from table where a=? and b=?" ) stmt.bind_params( 15, "hello" ) So in reality inside the stmt, we need to have map/array or something that will eventually map the arguments and create the right stmt. What's the

Insert into MySQL from R

喜夏-厌秋 提交于 2019-12-07 21:31:06
问题 I am using DBI package to insert data to MySQL. Here is the code: ch <- DBI::dbConnect(MySQL()) dbSendQuery(ch, 'set character set "utf8"') dbSendQuery(ch, 'SET NAMES utf8') for (i in 1:nrow(test)) { query <- paste0("INSERT INTO trade_data VALUES('0', '", test[i, 1], "', '", test[i, 2], "', ", test[i, 3], "')") dbSendQuery(ch, query) } The problem is in 3td column, which is numeric, but have NA values. When loop comes to row which has NA value it returns an error: Error in .local(conn,

Possible to use named placeholders in DBI's selectcol_arrayref & Co.?

孤人 提交于 2019-12-07 15:59:09
问题 Is it somehow possible to use named placeholders where DBI allows @bind_values? E. g., I would like to make statements like: my $s = $DB->selectcol_arrayref ("SELECT a FROM b WHERE c = ? OR d = ? OR e = ?;", {}, $par1, $par2, $par1) or die ($DB->errstr ()); less prone to mistakes. I'm using DBD::Pg and DBD::SQLite. 回答1: What sorts of placeholders (if any) are supported depends on the driver: Placeholders and Bind Values Some drivers support placeholders and bind values. [...] Some drivers

problems in using DBI

痞子三分冷 提交于 2019-12-07 15:25:28
I'm new to all this this stuff. I'm trying to execute the following code use DBI; my $dsn = 'DBI:mysql:db:localhost'; my $db_user_name = 'root'; my $db_password = '*******'; my $dbh = DBI->connect($dsn, $db_user_name, $db_password); my $sth = $dbh->prepare("select id from table where field = 'value'"); $sth->execute(); ($id) = $sth->fetchrow_array(); print "id is $id"; $sth->finish(); print outputs nothing. Can you tell me what am I doing wrong? Thank you in advance! You said in one of the comments that you had an @ in the value. If you're having a quoting issue, you should use a placeholder.

Accessing Microsoft SQL Server from Windows in PERL

五迷三道 提交于 2019-12-07 12:25:24
问题 I am using SQL Server driver. But this is the following error I get: DBI connect('Driver={SQL Server}:$database:$host','cartertest',...) failed: [Microsoft][ODBC Driver Manager] Invalid connection string attribute (SQL-01S00) at PERL_SQL_Connect.pl line 15 Can't call method "disconnect" on an undefined value at PERL_SQL_Connect.pl line 16 This is my code: use DBI; use DBD::ODBC; #my $dsn = "dbi:SQL Server:$database:$host"; my $dsn = 'DBI:ODBC:Driver={SQL Server}:$database:$host'; my $host =

Automatic character encoding handling in Perl / DBI / DBD::ODBC

谁都会走 提交于 2019-12-07 07:58:45
问题 I'm using Perl with DBI / DBD::ODBC to retrieve data from an SQL Server database, and have some issues with character encoding. The database has a default collation of SQL_Latin1_General_CP1_CI_AS , so data in varchar columns is encoded in Microsoft's version of Latin-1, AKA windows-1252. There doesn't seem to be a way to handle this transparently in DBI/DBD::ODBC. I get data back still encoded as windows-1252 , for instance, € “ ” are encoded as bytes 0x80, 0x93 and 0x94. When I write those

Perl - DBI and .pgpass

旧街凉风 提交于 2019-12-07 06:33:42
问题 I can successfully create a connection to a Postgres db using the following: my $settings = { host => 'myhost', db => 'mydb', user => 'myuser', passwd => 'mypasswd' }; my $connection = DBI->connect( 'DBI:Pg:dbname=' . $settings->{'db'} . ';host=' . $settings->{'host'}, $settings->{'user'}, $settings->{'passwd'}, { RaiseError => 1, ShowErrorStatement => 0, AutoCommit => 0 } ) or die DBI->errstr; But I'm left with valuable login credentials exposed (yes, I changed them) in my Perl module.

How do I get schemas from Perl's DBI?

折月煮酒 提交于 2019-12-07 03:19:20
问题 I am using Perl DBI. I know that $dbase->tables() will return all the tables in the corresponding database. Likewise, I want to know the schemas available in the database. Is there any function available for that? 回答1: What you're looking for is: DBI->table_info() Call it like this: my $sth = $dbh->table_info('', '%', ''); my $schemas = $dbh->selectcol_arrayref($sth, {Columns => [2]}); print "Schemas: ", join ', ', @$schemas; 回答2: This works. Create a database: echo 'create table foo (bar

How can I handle unicode with Perl's DBI?

北城以北 提交于 2019-12-06 17:07:40
问题 My delicious-to-wp perl script works but gives for all "weird" characters even weirder output. So I tried $description = decode_utf8( $description ); but that doesnt make a difference. I would like e.g. “go live” to become “go live” and not “go live†How can I handle unicode in Perl so that this works? UPDATE: I found the problem was to set utf of DBI I had to set in Perl: my $sql = qq{SET NAMES 'utf8';}; $dbh->do($sql); That was the part that I had to set, tricky. Thanks! 回答1: It may have

Insert into MySQL from R

对着背影说爱祢 提交于 2019-12-06 06:17:54
I am using DBI package to insert data to MySQL. Here is the code: ch <- DBI::dbConnect(MySQL()) dbSendQuery(ch, 'set character set "utf8"') dbSendQuery(ch, 'SET NAMES utf8') for (i in 1:nrow(test)) { query <- paste0("INSERT INTO trade_data VALUES('0', '", test[i, 1], "', '", test[i, 2], "', ", test[i, 3], "')") dbSendQuery(ch, query) } The problem is in 3td column, which is numeric, but have NA values. When loop comes to row which has NA value it returns an error: Error in .local(conn, statement, ...) : could not run statement: Unknown column 'NA' in 'field list' I tried to change NA to NaN,