String getting converted to number when inserting it in database through Perl's DBI $sth->execute() function - perl

I'm using Perl's DBI and SQLite database (I have DBD::SQLite installed). I have the following code:
my $dbh = DBI->connect("dbi:SQLite:dbname=$db", "", "", { RaiseError => 1, AutoCommit => 1 });
...
my $q = "INSERT OR IGNORE INTO books (identica, book_title) VALUES (?, ?)";
my $sth = $dbh->prepare($q);
$sth->execute($book_info->{identica}, $book_info->{book_title});
The problem I have is when $book_info->{identica} begins with 0's they get dropped and I get a number inserted in the database.
For example, identica of 00123 will get converted to 123.
I know SQLite doesn't have types, so how do I make DBI to insert the identica as string rather than number?
I tried quoting it as "$book_info->{identica}" when passing to $sth->execute but that didn't help.
EDIT
Even if I insert value directly in query it doesn't work:
my $i = $book_info->{identica};
my $q = "INSERT OR IGNORE INTO books (identica, book_title) VALUES ('$i', ?)";
my $sth = $dbh->prepare($q);
$sth->execute($book_info->{book_title});
This still coverts 00123 to 123, and 0000000009 to 9...
EDIT
Holy sh*t, I did this on the command line, and I got this:
sqlite> INSERT INTO books (identica, book_title) VALUES ('0439023521', 'a');
sqlite> select * from books where id=28;
28|439023521|a|
It was dropped by SQLite!
Here is how the schema looks:
CREATE TABLE books (
id INTEGER PRIMARY KEY AUTOINCREMENT,
identica STRING NOT NULL,
);
CREATE UNIQUE INDEX IDX_identica on books(identica);
CREATE INDEX IDX_book_title on books(book_title);
Any ideas what is going on?
SOLUTION
It's sqlite problem, see answer by in the comments by Jim. The STRING has to be TEXT in sqlite. Otherwise it treats it as number!
Changing schema to the following solved it:
CREATE TABLE books (
id INTEGER PRIMARY KEY AUTOINCREMENT,
identica TEXT NOT NULL,
);

Use bind params
my $sth = $dbh->prepare($q);
$sth->bind_param(1, 00123, { TYPE => SQL_VARCHAR });
$sth->bind_param(2, $book_info->{book_title});
$sth->execute();
UPDATE:
Read about type affinity in SQLite. Because your column type is STRING (technically unsupported), it defaults to INTEGER affinity. You need to create your column as TEXT instead.

According to the docs, if the column type (affinity) is TEXT it should store it as a string; otherwise it will be a number.

Related

perl dbi prepare with variable table column name

I have used the following code many times before when inserting values into database tables using perl
my $SRV='xxx';
my $DB='dbname';
my $db = DBI->connect("dbi:Oracle:$SRV/$DB", "user", "pword" ) or die "impossible de se connecter à $SRV / $DB";
my $insert_T1 = "INSERT INTO tablename (ColA, ColB) VALUES ( ?, ?) " ;
my $insert_T1_sth = $db->prepare($insert_T1) ;
Later in the code I can then call the following to do the insertion
$insert_T1_sth->execute('val1','val2');
$insert_T1_sth->execute('val3','val4');
So basically when I use the prepare function above I can replace the entries I want to insert by question marks and then put the values of these question marks in the execute statements later on.
So to my question: Can I use question marks in place of column names in the prepare statement? I'm thinking no because when I try the following I get a runtime error on the line where the execute statement(s) are.
my $SRV='xxx';
my $DB='dbname';
my $db = DBI->connect("dbi:Oracle:$SRV/$DB", "user", "pword" ) or die "impossible de se connecter à $SRV / $DB";
$db->{AutoCommit} = 0 ;
my $insert_T1 = "INSERT INTO tablename (ColA, ?) VALUES ( ?, ?) " ;
my $insert_T1_sth = $db->prepare($insert_T1) ;
Then later, as before, use
$insert_T1_sth->execute('colname1','val1','val2');
$insert_T1_sth->execute('colname2','val3','val4');
You can't use dynamic column names with prepare like you are trying to do.
Your column names shouldn't be known to the user, and therefore don't really need to be part of the parameters, since they are not sensitive (and don't need to be protected against SQL injection). Preparing is still useful for performances though.
What I'd suggest is to do a prepare for each of you column name, and store those in a hash:
my #col_names = qw(colname1 colname2);
my %inserts;
for my $col (#col_names) {
$inserts{$col} = $db->prepare("INSERT INTO tablename (ColA, $col) VALUES (?, ?)");
}
...
$inserts{colname1}->execute('val1', 'val2');

How can I combine these two statements?

I'm currently trying to insert data into a database from a text boxes, $enter / $enter2 being where the text is being written.
The database consists of three columns ID, name and nametwo
ID is auto incrementing and works fine
Both statements work fine on their own, but because they are being issued separately the first leaves nametwo blank and the second leaves name blank.
I've tried combining both but haven't had much luck, hope someone can help.
$dbh->do("INSERT INTO $table(name) VALUES ('".$enter."')");
$dbh->do("INSERT INTO $table(nametwo) VALUES ('".$enter2."')");
To paraphrase what others have said:
my $sth = $dbh->prepare("INSERT INTO $table(name,nametwo) values (?,?)");
$sth->execute($enter, $enter2);
So you don't have to worry about quoting.
You should read database manual.
The query should be:
$dbh->do("INSERT INTO $table(name,nametwo) VALUES ('".$enter."', '".$enter2."')");
The SQL syntax is
INSERT INTO MyTable (
name_one,
name_two
) VALUES (
"value_one",
"value_two"
)
Your way of generating SQL statements is very fragile. For example, it will fail if the table name is Values or the value is Jester's.
Solution 1:
$dbh->do("
INSERT INTO ".$dbh->quote_identifier($table_name)."
name_one,
name_two
) VALUES (
".$dbh->quote($value_one).",
".$dbh->quote($value_two)."
)
");
Solution 2: Placeholders
$dbh->do(
" INSERT INTO ".$dbh->quote_identifier($table_name)."
name_one,
name_two
) VALUES (
?, ?
)
",
undef,
$value_one,
$value_two,
);

why autoincrement primary key column not autoincrementing in sqlite3

I have an sqlite3 db for an address book, and I created the address table with
my $dbcreate = qq(CREATE TABLE IF NOT EXISTS address
(ID INTEGER PRIMARY KEY AUTOINCREMENT,
LASTNAME TEXT NOT NULL,
FIRSTNAME TEXT NOT NULL,
COMPANY TEXT,
STREET TEXT,
CITY TEXT,
STATE TEXT,
COUNTRY TEXT,
ZIPCODE TEXT,
MOBILE TEXT,
OTHERPHONE TEXT,
FAX TEXT,
EMAIL TEXT,
EMAIL2 TEXT,
WEBSITE TEXT,
WEBSITE2 TEXT,
DOB DATE,
NOTES TEXT,
TAGS TEXT););
So the first column SHOULD, as I understand it, be an autoincrementing primary ID, only, it's not autoincrementing. My understanding from reading sqlite tutorials is that I should only have to enter the other 18 values, and that column should autoincrement, but if I insert 18 values, I'm told I'm missing one.
I've only been able to insert rows if I manually set the ROWID.
What gives?
Here's how I'm adding rows (unsuccessfully)
#!/usr/bin/perl -w
# script to enter a new addressbook entry
use DBI;
use strict;
use warnings;
print "Please enter a DB username? \n";
my $userid=<STDIN>;
chomp($userid);
print "Please enter a DB password? \n";
my $password=<STDIN>;
chomp($password);
my $driver = "SQLite";
my $database = "myaddress.db";
my $dsn = "DBI:$driver:dbname=$database";
my $dbh = DBI->connect($dsn, $userid, $password, { RaiseError => 1 })
or die $DBI::errstr;
print "Successfull DB connection\n";
print "Creating new address book entry\n";
my $arval = 0;
my #dbits = ('LASTNAME','FIRSTNAME','COMPANY','STREET','CITY','STATE','COUNTRY','ZIPCODE','MOBILE','OTHERPHONE','FAX','EMAIL','EMAIL2','WEBSITE','WEBSITE2','DOB','NOTES','TAGS');
# print "#dbits";
# for $b (0 .. 17) {
# print "\'$dbits[$b]\', ";
# }
print "-----\n";
my #nubits = ();
foreach my $databit(#dbits) {
print "Enter $databit: \n";
my $nubit = <STDIN>;
chomp($nubit);
print "$databit = $nubit\n\n";
push(#nubits,$nubit);
print "$databit = $nubits[$arval]\n";
my $arval = (++$arval);
print "$arval\n";
}
# print "new entry will be:\n";
# for $b (0 .. 17) {
# print "$nubits[$b] | ";
# }
my $entry = qq(INSERT INTO address VALUES('$nubits[0]','$nubits[1]','$nubits[2]','$nubits[3]','$nubits[4]','$nubits[5]','$nubits[6]','$nubits[7]','$nubits[8]','$nubits[9]','$nubits[10]','$nubits[11]','$nubits[12]','$nubits[13]','$nubits[14]','$nubits[15]','$nubits[16]','$nubits[17]'););
my $retval = $dbh->do($entry);
if($retval < 0){
print $DBI::errstr;
} else {
print "entry created successfully\n";
}
my $query = qq(select * from address);
print $query;
$dbh->disconnect();
I can manually use sqlite3 from the cli to enter rows, but, as mentioned, I have to manually set the ROWID/Primary ID.
In the past, I've only used sqlite with tcl/tk, never perl, but even though I'm a complete perl n00b, I don't think perl is my problem. sqlite3 is not behaving as expected (unless I've completely misread about a dozen tutorials that state that a primary key id set to autoincrement should, well, autoincrement).
It's not auto incrementing because you're already giving it an id, $nubits[0].
With no column list, INSERT INTO address VALUES (...) inserts all columns in the table. You could use NULL as #ReenactorRob suggests, but that just hides another problem with that query.
INSERT INTO address VALUES (...) requires knowledge of the order in which address was created to know that element 5 is street. If anything changes the ordering in the table, your INSERT breaks. If a column is added, your query breaks. If you put a value in the wrong slot (as you did) its difficult to tell that. You're much better off using an explicit column list.
INSERT INTO address
(lastname, firstname, company, street, ...)
VALUES
(...)
Now your ID will increment and you're protected from future table changes. If that seems like a lot of work, make it a function that takes a hash of values to build a query. It'll be much more readable than remembering what $nubits[12] is. But before you do that, look at DBIx::Class which has already done this for you.
I would be remiss if I didn't mention bind parameters. These are faster, allowing you to use prepared statements, and they protect you against SQL injection attacks.
Try using null in the insert like this:
my $entry = qq(INSERT INTO address VALUES(null,'$nubits[0]','$nubits[1]',...
As has been explained, the problem is that the items in the VALUES section of the INSERT are assigned one-for-one to the table's columns in the order they were first created. If you need to provide values for only a subset of the columns then you also need to provide a list of the column names that should receive the values.
Without the column names the values will be assigned to columns in the order that they were created, so it is the columns at the end that will not receive a value. In this case the database will allocate the value for the LASTNAME column to ID, and it is the TAGS element at the end that doesn't have a value.
It is also much better to use placeholders for the values in your SQL statement, and use prepare and execute instead of just do. That way the DBI module will quote and escape the actual values appropriately according to their data type.
This program shows how to do that
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
use DBD::SQLite;
STDOUT->autoflush;
my ($driver, $dbname) = qw/ SQLite myaddress.db /;
my $dsn = "DBI:$driver:dbname=$dbname";
print 'Please enter a DB username: ';
my $username = <STDIN>;
chomp($username);
print 'Please enter a DB password: ';
my $password = <STDIN>;
chomp($password);
my $dbh = DBI->connect($dsn, $username, $password, { RaiseError => 1 });
print "Successful DB connection\n";
print "Creating new address book entry...\n";
print "-----\n";
my #dbits = qw/
LASTNAME FIRSTNAME COMPANY STREET
CITY STATE COUNTRY ZIPCODE
MOBILE OTHERPHONE FAX EMAIL
EMAIL2 WEBSITE WEBSITE2 DOB
NOTES TAGS
/;
my #nubits;
for my $databit (#dbits) {
print "Enter $databit: ";
my $nubit = <STDIN>;
chomp($nubit);
push #nubits, $nubit;
print "$databit = $nubits[-1]\n";
print scalar #nubits, "\n";
}
my $columns = join ', ', #dbits;
my $placeholders = join ', ', ('?') x #dbits;
my $insert = <<"__END__SQL__";
INSERT INTO address (
$columns
)
VALUES (
$placeholders
)
__END__SQL__
$insert = $dbh->prepare($insert);
$insert->execute(#nubits);
print "Entry created successfully\n";
$dbh->disconnect;
This creates an SQL statement in $insert that looks like this
INSERT INTO address (
LASTNAME, FIRSTNAME, COMPANY, STREET, CITY, STATE, COUNTRY, ZIPCODE, MOBILE, OTHERPHONE, FAX, EMAIL, EMAIL2, WEBSITE, WEBSITE2, DOB, NOTES, TAGS
)
VALUES (
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
)

DBIx::class find function returns hash when value expected

Database schema:
create table requests(
rid integer primary key autoincrement,
oid integer references orders (oid),
command varchar(5),
account varchar(50),
txn_id varchar(12),
md5 varchar(30),
txn_date varchar(14),
sum float,
regdt timestamp default current_timestamp,
constraint u1 unique (command,txn_id)
);
create table orders (
oid integer primary key autoincrement,
cid integer references customers (cid),
pid integer references providers (pid),
account varchar(50),
amount float
);
Mapped to code by DBIx::Class::Schema::Loader.
My controller code :
my $req= $schema->resultset('Request');
my $order= $schema->resultset('Order');
my $r= $req->find(
{ command => 'check',
txn_id => $txn_id,
},
{ key => 'command_txn_id_unique' }
);
my $oid=$r->oid;
$req->create(
{ command => $command,
account => $account,
txn_id => $txn_id,
md5 => $md5,
txn_date => $txn_date,
sum => $sum,
oid => $oid
}
);
my $o = $order->find($oid);
$o->sum($sum);
$o->update;
My tracert sqls with DBIC_TRACE=1
SELECT me.rid, me.oid, me.command, me.account, me.txn_id, me.md5, me.txn_date, me.sum, me.regdt FROM requests me
WHERE ( ( me.command = ? AND me.txn_id = ? ) ): 'check', '1358505665'
SELECT me.oid, me.cid, me.pid, me.account, me.amount FROM orders me
WHERE ( me.oid = ? ): '18'
INSERT INTO requests ( account, command, md5, oid, sum, txn_date, txn_id) VALUES ( ?, ?, ?, ?, ?, ?, ? ): '1', 'pay', '44F4BC73D17E3FA906F658BB5916B7DC', '18', '500', '20130118104122', '1358505665'
DBIx::Class::Storage::DBI::SQLite::_dbi_attrs_for_bind(): Non-integer value supplied for column 'me.oid' despite the integer datatype at /home/.../lib/TestPrj/Test.pm line 128
SELECT me.oid, me.cid, me.pid, me.account, me.amount FROM orders me WHERE ( me.oid = ? ): 'TestPrj::Model::Result::Order=HASH(0x3dea448)'
datatype mismatch: bind param (0) =HASH(0x3dea448) as integer at /usr/share/perl5/DBIx/Class/Storage/DBI.pm line 1765.
I don't understand :
Why in first select query $oid = 18 and its ok. Its really 18.
But in second select query this $oid is a HASH ? Its not redefined anywhere in my code.
UPD:
Using Data::Dumper by advice #akawhy I saw that $oid is a blessed HASH.
So, looks like there is different context
scalar in { ... oid => $oid .. .}
and hash in find(...).
I don't know why it not a hash in first case.
But, when I changed to $oid=$r->get_column($oid) all works fine.
$resultset->find returns a Result object.
You didn't paste your ResultSource classes but as you wrote that you generated them with Schema::Loader I assume the 'oid' column accessor method is the same as the 'oid' relationship accessor. In that case it returns different things depending on scalar vs. list context.
I suggest to rename your relationship so you have two different accessors for the column value and its relationship.
I prefix all relationships with 'rel_' but you might prefer a different naming standard.
I think my $oid=$r->oid; the $oid is a ref. you can use ref or Data::Dumper to see its type.
Maybe you should use $oid->oid
And pay attention to this error
DBIx::Class::Storage::DBI::SQLite::_dbi_attrs_for_bind(): Non-......

RYO blog engine - showing tags for several posts

I am writing yet another blog engine for practice, using SQLite and Perl Dancer framework.
The tables go like this:
CREATE TABLE posts (
p_id INTEGER PRIMARY KEY,
p_url VARCHAR(255),
p_title VARCHAR(255),
p_text TEXT,
p_date DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE tags (
t_id INTEGER PRIMARY KEY,
t_tag VARCHAR(255),
t_url VARCHAR(255)
);
CREATE TABLE tags_posts_junction (
tp_tag INTEGER NOT NULL,
tp_post INTEGER NOT NULL,
FOREIGN KEY(tp_tag) REFERENCES tags.t_id,
FOREIGN KEY(tp_post) REFERENCES tags.p_id
);
All the big guys like Wordpress (or stackoverflow) can show tags right on the main page, after each question, and I'd like to implement it too. The question is how do I do it.
So far the posts are stored in the database, and when I need to render a page showing latest 20 posts I pass a hash refernece (fetchall_hashref from DBI) to the template. So how do I add tags there? Of course I can do something like
my $dbh = database->prepare('SELECT * FROM posts ORDER BY p_date DESC
LIMIT 20 OFFSET 0');
$dbh->execute;
my $posts = $dbh->fetchall_hashref('p_date');
foreach my $key (keys $post) {
my $dbh = database->prepare('SELECT * FROM tags WHERE t_id IN (
SELECT tp_tag FROM tags_posts_junction WHERE tp_post = ?)');
$dbh->execute($post->{"$key"}->{"p_id"});
my $tags = $dbh->fetchall_hashref(t_id);
$post->{"$key"}->{"$tag_hash"} = $tags;
};
But that's ugly and that's 20 more queries per page, isn't it too much? I think there should be a better way.
So the question is how do I get tags for 20 posts the least redundant way?
I think you could combine your first / outer query before
my $posts = $dbh->fetchall_hashref('p_date');
with your inner query and then you will be hitting the database once instead of 20 times.
You could also simplify your code by use of DBIx::Simple - https://metacpan.org/module/DBIx::Simple.
Putting this together would give something like:
my $sql = 'SELECT t.*, p.*
FROM tags t
JOIN tags_posts_junction tpj ON t.t_tag = tpj.t_tag
JOIN posts p ON p.p_id = tpj.tp_post
WHERE tpj.tp_post IN (
SELECT p_id FROM posts ORDER BY p_date DESC
LIMIT 20 OFFSET 0
)';
my $db = DBIx::Simple->connect($dbh);
my $posts = $db->query($sql)->hashes;
Collect all the p_ids into an array and construct your query using IN instead of =, something like this, presuming #pids is your array:
my $dbh = database->prepare('SELECT * FROM tags WHERE t_id IN (
SELECT tp_tag FROM tags_posts_junction WHERE tp_post IN (' .
join(', ', ('?')x#pids).') )');
$dbh->execute(#pids);
Though you should really look to JOINs to replace your sub-queries.