为了测试我与数据库的连接,我想简单地将一些硬编码数据添加到一个表中。
这是我的方法。在main.dart
文件中,只有2个与问题相关的行
DatabaseUtility dbUtil = new DatabaseUtility('postgres', 'kewoziwa', 'auctionDB');
dbUtil.insertIntoUsers({'login': 'dartUser', 'password': 'passMePlease'});
在第一行中,我创建了一个辅助类的实例。 在第二个中,我调用了这个类中定义的方法。
主要的整个代码:
import 'dart:io';
import 'package:http_server/http_server.dart' show VirtualDirectory;
import 'database_utility.dart';
VirtualDirectory virDir; // not related to this problem
main() {
DatabaseUtility dbUtil = new DatabaseUtility('postgres', 'kewoziwa', 'auctionDB');
dbUtil.insertIntoUsers({'login': 'dartUser', 'password': 'passMePlease'});
initVirDir(); // not related to this problem
runServer(); // not related to this problem
}
在DatabaseUtility类构造函数中,我只为数据库连接创建uri。实例化中使用的相同密码使用pgAdmin III登录时没有任何问题。所有其他输入也与pgAdmin III中的输入相同。
定义的insertIntoUsers
方法只是对users表执行插入操作。我不知道那里可能出现什么问题。插入后我关闭连接。
整个助手类:
import 'package:postgresql/postgresql.dart';
class DatabaseUtility {
//Connection conn;
String username;
String passwd;
String database;
var uri;
DatabaseUtility(this.username, this.passwd, this.database) {
uri ='postgres://$username:$passwd@localhost:5432/$database';
print(uri);
}
insertIntoUsers(Map values){
connect(uri)
.then((conn){
conn.execute('insert into users values(@login, @password)')
.then((_) => conn.close());
})
.catchError(print);
}
}
不幸的是,当我运行这个时,我得到错误:42703
错误讯息:
Unhandled exception:
Uncaught Error: BŁĄD 42703 kolumna "login" nie istnieje
// Trnaslation Error: 42703 the column "login" does not exist
#0 _rootHandleUncaughtError.<anonymous closure> (dart:async/zone.dart:886)
#1 _asyncRunCallbackLoop (dart:async/schedule_microtask.dart:41)
#2 _asyncRunCallback (dart:async/schedule_microtask.dart:48)
#3 _runPendingImmediateCallback (dart:isolate-patch/isolate_patch.dart:96)
#4 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:143)
在pgAdminIII工具中,我可以看到此专栏。
这里有什么问题?
答案 0 :(得分:1)
我认为您忘记将值传递给conn.execute()。
尝试更改此行:
conn.execute('insert into users values(@login, @password)', values)
^^^^^^
到此:
Me.XpProgressBar1 = New Framework.Controls.XpProgressBar
另外,我建议存储盐渍密码哈希值而不是明文字符串。这是一个很好的起点:How can I hash passwords in postgresql?