[英]Error 42703 when connecting to postgresql database with dart
为了测试与数据库的连接,我想简单地将一些硬编码数据添加到一个表中。 这是我的方法。 在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
方法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工具中,我可以看到此列。 怎么了
我认为您忘记了将值传递到conn.execute()中。
尝试更改此行:
conn.execute('insert into users values(@login, @password)')
对此:
conn.execute('insert into users values(@login, @password)', values)
^^^^^^
另外,我建议存储加盐的密码哈希值,而不要存储纯文本字符串。 这是一个很好的起点: 如何在postgresql中对密码进行哈希处理?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.