0

用户 id 为空,该列是主键,它不应该是空的

我正在学习 Flutter 和 SQFlite,我正在按照 Udemey 课程中的示例进行操作,并且我正在按字母输入,但它对讲师有用,对我不起作用

我简化了一切,现在它是一个文件,仍然,ID 为空

主要.dart

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'package:sqflite/sqflite.dart';

void main() async {
//  debugPrint("Hello World");
  var db = dbhelp();
  User user1 = User("User1");
  int insertResult = await db.saveUser(user1);
  debugPrint("insert result is " + insertResult.toString());
  User searchResult = await db.retrieveUser(insertResult);
  debugPrint(searchResult.toString());
}

class dbhelp {
  static final dbhelp _instance = dbhelp.internal();
  dbhelp.internal();
  factory dbhelp() => _instance;
  static Database _db;
  void _onCreate(Database _db, int newVersion) async {
    await _db.execute(
        "CREATE TABLE MYTABLE(ID INTEGER PRIMARY KEY, userName TEXT NOT NULL)");
  }

  Future<Database> initDB() async {
    Directory documentDirectory = await getApplicationDocumentsDirectory();
    String path = join(documentDirectory.path, "appdb.db");
    Database newDB = await openDatabase(path, version: 1, onCreate: _onCreate);
    return newDB;
  }

  Future<Database> get db async {
    if (_db != null) {
      return _db;
    } else {
      _db = await initDB();
      return _db;
    }
  }

  Future<int> saveUser(User user) async {
    var dbClient = await db;
    int result;
    var userMap = user.toMap();
    result = await dbClient.insert("MYTABLE", userMap);
    return result;
  }

  Future<User> retrieveUser(int id) async {
    var dbClient = await db;
    if (id == null) {
      print("The ID is null, cannot find user with Id null");
      var nullResult =
          await dbClient.rawQuery("SELECT * FROM MYTABLE WHERE ID is null");
      return User.fromMap(nullResult.first);
    }
    String sql = "SELECT * FROM MYTABLE WHERE ID = $id";
    var result = await dbClient.rawQuery(sql);
    if (result.length != 0) {
      return User.fromMap(result.first);
    } else {
      return null;
    }
  }
}

class User {
  String _userName;
  int _id;
  String get userName => _userName;
  int get id => _id;
  User(this._userName, [this._id]);
  User.map(dynamic obj) {
    this._userName = obj['userName'];
    this._id = obj['id'];
  }
  User.fromMap(Map<String, dynamic> map) {
    this._userName = map["userName"];
    if (map["id"] != null) {
      this._id = map["id"];
    } else {
      print("in fromMap, Id is null");
    }
  }
  Map<String, dynamic> toMap() {
    Map map = Map<String, dynamic>();
    map["userName"] = this._userName;
    if (_id != null) {
      map["id"] = _id;
    } else {
      print("in toMap, id is null");
    }
    return map;
  }

  @override
  String toString() {
    return "ID is ${this._id} , Username is ${this._userName} }";
  }
}

当我打印 id 它应该打印一个不为空的数字

但它始终为空

4

1 回答 1

1

我疑惑了一会儿。选择后的简单打印显示问题:

String sql = "SELECT * FROM MYTABLE WHERE ID = $id";
var result = await dbClient.rawQuery(sql);
print(result);

输出:

[{ID: 1, userName: User1}]

列名区分大小写。有时id (在 toMap/fromMap 中)有时ID(在创建表中)使用。确保始终在任何地方使用相同的名称。

于 2019-02-04T08:02:07.373 回答