用dart连接postgresql数据库报错42703
Error 42703 when connecting to postgresql database with dart
为了测试我与数据库的连接,我想简单地向一个 table 添加一些硬编码数据。
这是我的方法。在 main.dart
文件中只有 2 个问题相关的行
DatabaseUtility dbUtil = new DatabaseUtility('postgres', 'kewoziwa', 'auctionDB');
dbUtil.insertIntoUsers({'login': 'dartUser', 'password': 'passMePlease'});
在第一行中,我创建了一个助手实例 class。
在第二个中,我调用了此 class.
中定义的方法
main的全部代码:
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 class 构造函数中,我只为数据库连接创建了 uri。使用 pgAdmin III 在实例化中使用的相同密码登录没有任何问题。所有其他输入也与 pgAdmin III 中的相同。
定义的 insertIntoUsers
方法只是对用户 table 执行插入操作。我看不出那里有什么问题。插入后我关闭连接。
全帮手class:
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)
^^^^^^
此外,我建议存储加盐密码哈希而不是纯文本字符串。这是一个很好的起点:How can I hash passwords in postgresql?
为了测试我与数据库的连接,我想简单地向一个 table 添加一些硬编码数据。
这是我的方法。在 main.dart
文件中只有 2 个问题相关的行
DatabaseUtility dbUtil = new DatabaseUtility('postgres', 'kewoziwa', 'auctionDB');
dbUtil.insertIntoUsers({'login': 'dartUser', 'password': 'passMePlease'});
在第一行中,我创建了一个助手实例 class。 在第二个中,我调用了此 class.
中定义的方法main的全部代码:
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 class 构造函数中,我只为数据库连接创建了 uri。使用 pgAdmin III 在实例化中使用的相同密码登录没有任何问题。所有其他输入也与 pgAdmin III 中的相同。
定义的 insertIntoUsers
方法只是对用户 table 执行插入操作。我看不出那里有什么问题。插入后我关闭连接。
全帮手class:
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)
^^^^^^
此外,我建议存储加盐密码哈希而不是纯文本字符串。这是一个很好的起点:How can I hash passwords in postgresql?