簡體   English   中英

使用Dart連接到Postgresql數據庫時出現錯誤42703

[英]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工具中,我可以看到此列。 帶有“缺少”列的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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM