脚本的目的:我希望能够使用它将费用插入到SQLite数据库中,然后在以后制作自定义报告以提取信息,以便更好地预算开支。
我完全不理解这个错误代码。
perl6 budgetpro.p6苹果 一天一苹果,不用请医生 你好,我很高兴见到你,艾格曼 无法解析调用方sqlite3_bind(DBDish::SQLite::本机::STMT、Int、Date);这些签名都不匹配:(DBDish::SQLite::STMT $stmt,Int $n,Blob:D $b) (DBDish::SQLite::STMT $stmt,Int $n,Real:D $d) (DBDish::SQLite::STMT $stmt,Int $n,Int:D $i) (DBDish::SQLite::STMT $stmt,Int $n,Any:U) (DBDish::SQLite::本机:STMT $stmt,Int $n,/home/jon/opt/rakudo-star/share/perl6/site/sources/2D749062AA6D5641452CDA214FC7C566E7E3E2A1 (DBDish::SQLite::StatementHandle)方法中的第38行(方法插入budgetpro.p6,第54行,块在budgetpro.p6第86行)
2
3 use v6;
4 use DBIish;
5
6 constant DB = 'budgetpro.sqlite3';
7 my $dbh = DBIish.connect('SQLite', database => DB);
8
9 #$dbh.do('drop table if exists Essential, Savings, Personal');
10
11 sub create-schema {
12 $dbh.do(qq:to/SCHEMA/);
13 create table if not exists Essential(
14 id integer primary key not null,
15 name varchar not null,
16 quantity integer not null,
17 price numeric(5,2) not null,
18 description varchar not null,
19 date timestamp not null default (datetime('now'))
20 );
21 create table is not exists Savings(
22 id integer primary key not null,
23 name varchar not null,
24 quantity integer not null,
25 price numeric(5,2) not null,
26 description varchar not null,
27 date timestamp not null default (datetime('now'))
28 );
29 create table if not exists Personal(
30 id integer primary key not null,
31 name varchar not null,
32 quantity integer not null,
33 price numeric(5,2) not null,
34 description varchar not null,
35 date timestamp not null default (datetime('now'))
36 );
37 SCHEMA
38 }
39
40 create-schema;
41
42 class Item {
43 has $!table = 'Essential';
44 has $.name;
45 has $.quantity;
46 has $.price;
47 has $.description is rw;
48 has $.timestamp;
49 has Str $.notes is rw;
50 method notes() { "$!notes\n" };
51
52 method insert($name, $quantity?, $price?, $description?, $timestamp?) {
53 my $sth = $dbh.prepare("insert into Essential(name,quantity,price,description,date) values (?,?,?,?,?)");
54 $sth.execute($name, $quantity, $price, $description, $timestamp);
55
56 say "Inserted $name, $quantity, $price, $description, $timestamp";
57 }
58 }
59
60 class Essential is Item {
61 method greet($me: $person) {
62 say "Hi, I am $me.^name(), nice to meet you, $person";
63 }
64 }
65
66 class Savings is Item {
67 }
68
69 class Personal is Item {
70 }
71
72 my $food = Essential.new(
73 name => 'Apple',
74 price => .99,
75 quantity => 2,
76 notes => 'An apple a day keeps the doctor away'
77 );
78
79 say $food.name;
80 say $food.notes;
81 Essential.new.greet('Eggman');
82 say '';
83
84 my $test = Item.new();
85
86 $test.insert("Cheese", 2, 1.99, 'Block of cheddar', Date.new(now));
87
88 say $test.name;发布于 2018-03-27 22:06:30
问题是你打电话给:
$test.insert("Cheese", 2, 1.99, 'Block of cheddar', Date.new(now));这最终调用了sqlite3_bind,但是没有一个候选人接受Date的实例作为第三个参数。
它将接受Blob、Int、Real (Numeric而不是Complex)或Str的实例。它还将接受一个未定义的值(Any:U)。
在我看来,它不接受DateTime,甚至不接受Instant对象,这是数据库驱动程序中的一个bug或缺少的特性,并自动将其转换为SQLite所期望的。
我还想指出的是Date.today和DateTime.now。后者将跟踪当前的时区信息,而DateTime.new(now)则不会。
(这是有意的,因为无法知道Instant来自何处。)
发布于 2018-03-28 09:23:20
@布拉德·吉尔伯特是对的。语句只接受5种数据结构,其中没有一种是Date。 are standard data types in SQL,但就目前而言,他们似乎没有在DBIish中得到支持。在这种情况下,只需将第85行更改为
$test.insert("Cheese", 2, 1.99, 'Block of cheddar', Date.new(now).Str);让您的程序处理与日期的转换,您还必须更改Essential表的定义。还可以让数据库处理默认值。您必须删除“null",因为它与default语句冲突。
还有一些小错误。这可以简化为最小表达式,可以正确地将默认时间戳插入数据库(通过使用sqlite3命令行检查):
use v6;
use DBIish;
constant DB = 'budgetpro.sqlite3';
my $dbh = DBIish.connect('SQLite', database => DB);
#$dbh.do('drop table if exists Essential, Savings, Personal');
sub create-schema {
$dbh.do(qq:to/SCHEMA/);
create table if not exists Essential(
id integer primary key not null,
name varchar not null,
quantity integer not null,
price numeric(5,2) not null,
description varchar not null,
date timestamp default (datetime('now'))
);
SCHEMA
}
create-schema;
class Item {
has $!table = 'Essential';
has $.name;
has $.quantity;
has $.price;
has $.description is rw;
has $.timestamp;
has Str $.notes is rw;
method notes() { "$!notes\n" };
method insert($name, $quantity?, $price?, $description?, $timestamp?) {
my $sth = $dbh.prepare("insert into Essential(name,quantity,price,description) values (?,?,?,?)");
$sth.execute($name, $quantity, $price, $description);
say "Inserted $name, $quantity, $price, $description";
}
}
Item.new().insert("Cheese", 2, 1.99, 'Block of cheddar');请注意,您的Item类实际上并不是通过运行insert语句来实例化的,因此,最后一行中的$test.name将返回一个空对象,该对象的默认值分配给实例变量。您可能希望将insert方法更改为实例化项并将其插入数据库的子方法。
https://stackoverflow.com/questions/49522489
复制相似问题