简体   繁体   English

如何使用 flutter 更新 sqflite 中的表

[英]how to update table in sqflite with flutter

when I try to update a table with id nothing won't happen.当我尝试用 id 更新表时,什么都不会发生。 I got no error and I have no idea why this happens.我没有错误,我不知道为什么会发生这种情况。 at first, I try to get all data from fields, and then if all fields have value then make the User object and pass into the method for the update table.首先,我尝试从字段中获取所有数据,然后如果所有字段都有值,则创建用户 object 并传递给更新表的方法。 I got id from the previous screen and other data from fields.我从上一个屏幕中获得了 id,并从字段中获得了其他数据。 as I check I have all data inside the validate method in CompleteUserInformationScreen and I think error occur somewhere when I try to call update method with sqflite.当我检查我在 CompleteUserInformationScreen 的 validate 方法中拥有所有数据时,我认为当我尝试使用 sqflite 调用更新方法时发生错误。

this is my method for the update table:这是我更新表的方法:

 Future<User> update(User user) async {
    var dbClient = await db;
    // return await dbClient.update(USER_TABLE, user.toMap(),
    //     where: '$NATIONAL_ID = ?', whereArgs: [user.nationalId]);
    var res = await dbClient.rawQuery(
        'UPDATE Customer SET $NAME = ${user.name}, $FAMILY = ${user.family}, $BIRTHDAY = ${user.birthday}, $MOBILE = ${user.mobile}, $NATIONAL_ID = ${user.nationalId} WHERE id = ${user.id}');

    if (res.length > 0) {
      return new User.fromMap(res.first);
    }
    return null;
  }

and this is my screen:这是我的屏幕:

import 'package:flutter/material.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:atlas_gen_demo/Animation/FadeAnimation.dart';
import 'package:flutter/services.dart';
import '../models/user.dart';
import 'package:flushbar/flushbar.dart';
import 'package:atlas_gen_demo/data/storage/db_helper.dart';
import 'package:atlas_gen_demo/screens/users_list_screen.dart';

class CompleteUserInformationScreen extends StatelessWidget {
  static const routeName = '/complete-user-information';

  final nameController = TextEditingController();
  final familyController = TextEditingController();
  final nationalIdController = TextEditingController();
  final mobileController = TextEditingController();

  var id;
  var birthday;


  validate(BuildContext ctx) async {
    if (nameController.text != "" &&
        familyController.text != "" &&
        nationalIdController.text != "" &&
        mobileController.text != "") {
      final dbHelper = DBHelper();
      print("useriddd: $id   birthdayyyy: ${birthday.toString()}");
      User user = await dbHelper.update(User(
          id,
          nameController.text,
          familyController.text,
          null,
          null,
          birthday,
          mobileController.text,
          nationalIdController.text));
      if (user.username != "") {
        navigateToUsersList(ctx);
      }
    } else {
  //     showFlushBar(ctx, "fault", "fill all fields");
    }
  }

  void navigateToUsersList(BuildContext ctx) {
    Navigator.of(ctx).pushNamed(
      UsersListScreen.routeName,
    );
  }

  void showFlushBar(BuildContext context, String title, String text) {
    Flushbar(
      padding: EdgeInsets.all(10),
      borderRadius: 8,
      backgroundGradient: LinearGradient(
        colors: [Colors.purple.shade800, Colors.purpleAccent.shade700],
        stops: [0.6, 1],
      ),
      boxShadows: [
        BoxShadow(
          color: Colors.black,
          offset: Offset(3, 3),
          blurRadius: 3,
        )
      ],
      dismissDirection: FlushbarDismissDirection.HORIZONTAL,
      forwardAnimationCurve: Curves.fastLinearToSlowEaseIn,
      titleText: Text(
        title,
        style: TextStyle(fontFamily: 'mainBold', color: Colors.white),
      ),
      messageText: Text(
        text,
        style: TextStyle(fontFamily: 'mainMedium', color: Colors.white),
      ),
      duration: Duration(seconds: 3),
    ).show(context);
  }

  @override
  Widget build(BuildContext context) {
    final userId = ModalRoute.of(context).settings.arguments;
    return Scaffold(
      backgroundColor: Colors.white,
      body: SingleChildScrollView(
        child: Container(
          child: Column(
            children: <Widget>[
              Container(
                height: 250,
                margin: EdgeInsets.only(top: 50),
                decoration: BoxDecoration(
                  image: DecorationImage(
                    image: AssetImage('assets/images/complete_profile.png'),
                    fit: BoxFit.fill,
                  ),
                ),
              ),
              Positioned(
                child: FadeAnimation(
                    1.8,
                    InkWell(
                      child: Container(
                        margin: EdgeInsets.only(top: 10),
                        child: Center(
                          child: Text(
                            "Complete data",
                            textAlign: TextAlign.center,
                            style: TextStyle(
                              color: Color.fromRGBO(143, 148, 251, 1),
                              fontSize: 20,
                              fontWeight: FontWeight.bold,
                              fontFamily: 'persianBold',
                            ),
                          ),
                        ),
                      ),
                    )),
              ),
              Padding(
                padding: EdgeInsets.all(30.0),
                child: Column(
                  children: <Widget>[
                    Container(
                      padding: EdgeInsets.all(5),
                      decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(10),
                        boxShadow: [
                          BoxShadow(
                              color: Color.fromRGBO(143, 148, 251, .2),
                              blurRadius: 20.0,
                              offset: Offset(0, 10))
                        ],
                      ),
                      child: Column(
                        children: <Widget>[
                          Container(
                            padding: EdgeInsets.all(8.0),
                            decoration: BoxDecoration(
                                border: Border(
                              bottom: BorderSide(color: Colors.grey[100]),
                            )),
                            child: TextFormField(
                              controller: nameController,
                              textDirection: TextDirection.rtl,
                              textAlign: TextAlign.right,
                              decoration: InputDecoration(
                                border: InputBorder.none,
                                hintText: "name",
                                hintStyle: TextStyle(
                                  color: Colors.grey[400],
                                  fontFamily: 'persianMedium',
                                  fontSize: 14,
                                ),
                              ),
                            ),
                          ),
                          Container(
                            padding: EdgeInsets.all(8.0),
                            decoration: BoxDecoration(
                                border: Border(
                              bottom: BorderSide(color: Colors.grey[100]),
                            )),
                            child: TextFormField(
                              controller: familyController,
                              textAlign: TextAlign.right,
                              textDirection: TextDirection.rtl,
                              decoration: InputDecoration(
                                border: InputBorder.none,
                                hintText: "family",
                                hintStyle: TextStyle(
                                  color: Colors.grey[400],
                                  fontFamily: 'persianMedium',
                                  fontSize: 14,
                                ),
                              ),
                            ),
                          ),
                          Container(
                            padding: EdgeInsets.all(8.0),
                            decoration: BoxDecoration(
                                border: Border(
                              bottom: BorderSide(color: Colors.grey[100]),
                            )),
                            child: TextFormField(
                              controller: nationalIdController,
                              textAlign: TextAlign.right,
                              textDirection: TextDirection.rtl,
                              decoration: InputDecoration(
                                border: InputBorder.none,
                                hintText: "national code",
                                hintStyle: TextStyle(
                                  color: Colors.grey[400],
                                  fontFamily: 'persianMedium',
                                  fontSize: 14,
                                ),
                              ),
                            ),
                          ),
                          Container(
                            padding: EdgeInsets.all(8.0),
                            decoration: BoxDecoration(
                                border: Border(
                              bottom: BorderSide(color: Colors.grey[100]),
                            )),
                            child: TextFormField(
                              controller: mobileController,
                              textAlign: TextAlign.right,
                              inputFormatters: [
                                WhitelistingTextInputFormatter.digitsOnly
                              ],
                              textDirection: TextDirection.rtl,
                              decoration: InputDecoration(
                                border: InputBorder.none,
                                hintText: "09123456789",
                                hintStyle: TextStyle(
                                  color: Colors.grey[400],
                                  fontFamily: 'persianMedium',
                                  fontSize: 14,
                                ),
                              ),
                            ),
                          ),
                          FlatButton(
                              onPressed: () {
                                DatePicker.showDatePicker(context,
                                    showTitleActions: true,
                                    minTime: DateTime(1250, 1, 1),
                                    maxTime: DateTime(1450, 12, 31),
                                    onChanged: (date) {
                                  print('change $date');
                                }, onConfirm: (date) {
                                  birthday = date;
                                  print('confirm $date');
                                },
                                    currentTime: DateTime.now(),
                                    locale: LocaleType.en);
                              },
                              child: Text(
                                'birthday',
                              )),
                        ],
                      ),
                    ),
                    SizedBox(
                      height: 30,
                    ),
                    FadeAnimation(
                      2,
                      InkWell(
                        onTap: () => {
                          // print(
                          //     "useriddd: $userId   birthdayyyy: ${birthday.toString()}"),
                          id = userId,
                          validate(context),
                        },
                        child: Container(
                          height: 50,
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(10),
                            gradient: LinearGradient(
                              colors: [
                                Color.fromRGBO(143, 148, 251, .4),
                                Color.fromRGBO(143, 148, 251, .8),
                              ],
                            ),
                          ),
                          child: Center(
                            child: Text(
                              "Complete data",
                              style: TextStyle(
                                color: Colors.white,
                                fontWeight: FontWeight.bold,
                                fontFamily: 'persianBold',
                                fontSize: 18,
                              ),
                            ),
                          ),
                        ),
                      ),
                    ),
                    SizedBox(
                      height: 20,
                    ),
                  ],
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}

Edit:编辑:

after did some change in code I got this error:在对代码进行了一些更改后,我收到了这个错误:

Error Code: 1 (SQLITE_ERROR) E/flutter (18587): Caused By: SQL(query) error or missing database. E/flutter (18587): (near "00": syntax error (code 1): , while compiling: UPDATE User SET name = mehrdad, family = dolat, birthday = 1450-12-31 00:00:00.000, mobile = 0936, nationalId = 001 WHERE id = {id: 1}) E/flutter (18587): #################################################################) sql 'UPDATE User SET name = mehrdad, family = dolat, birthday = 1450-12-31 00:00:00.000, mobile = 0936, nationalId = 001 WHERE id = {id: 1}' args []}

updated code:更新代码:

  Future<User> update(String id, String name, String family, String birthday,
      String mobile, String nationalId) async {
    var dbClient = await db;
    var res = await dbClient.rawQuery(
        'UPDATE $USER_TABLE SET $NAME = $name, $FAMILY = $family, $BIRTHDAY = $birthday, $MOBILE = $mobile, $NATIONAL_ID = $nationalId WHERE id = $id');

    if (res.length > 0) {
      return new User.fromMap(res.first);
    }
    return null;
  }

and in my screen:在我的屏幕上:

  dbHelper.update(
          id.toString(),
          nameController.text,
          familyController.text,
          birthday.toString(),
          mobileController.text,
          nationalIdController.text);

      navigateToUsersList(ctx);

Edit Number 2:编辑编号 2:

I think we need just one more step to do this:我认为我们只需要多一步来做到这一点:

I do a new change on update method in database helper:我对数据库助手中的更新方法进行了新的更改:

   Future<int> update(String id, String name, String family, String birthday,
      String mobile, String nationalId) async {
    var dbClient = await db;
    return await dbClient.rawUpdate(
        'UPDATE $USER_TABLE SET $NAME = \'$name\', $FAMILY = \'$family\' ,$BIRTHDAY = \'$birthday\', $MOBILE = \'$mobile\' , $NATIONAL_ID = \'$nationalId\' WHERE $ID = ${int.parse(id)}');
  }

and when I try to add an Id for where section I got this error:当我尝试为 where 部分添加 Id 时,出现此错误:

[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: FormatException: Invalid radix-10 number (at character 1) E/flutter (21459): {id: 2}

but if I hardcoded id for example where $ID = 2 everything works fine.但是如果我硬编码 id 例如where $ID = 2一切正常。 how I can fix this issue?我该如何解决这个问题? why {} add beside id?为什么要在 id 旁边添加 {}? and how I must remove it以及我必须如何删除它

I used this article to implement my Database.我用这篇文章来实现我的数据库。 There you can find all things you need to interact with your database.在那里,您可以找到与数据库交互所需的所有内容。

this will work for you, you just need to implement the "toMap" Method in the User class:这对你有用,你只需要在用户 class 中实现“toMap”方法:

Future<void> update(User user) async {
    final dbClient = await database;
    var res = await db.update("Customer", user.toMap(),
        where: "id = ?", whereArgs: [user.id]);
    return res;
  }

after some search and trying I found the solution:经过一番搜索和尝试,我找到了解决方案:

update method in db_helper: db_helper 中的更新方法:

  Future<int> update(String id, String name, String family, String birthday,
      String mobile, String nationalId) async {
    var dbClient = await db;
    return await dbClient.rawUpdate(
        'UPDATE $USER_TABLE SET $NAME = \'$name\', $FAMILY = \'$family\' ,$BIRTHDAY = \'$birthday\', $MOBILE = \'$mobile\' , $NATIONAL_ID = \'$nationalId\' WHERE $ID = ${int.parse(id)}');
  }

and validate method in Screen:并在 Screen 中验证方法:

 validate(BuildContext ctx) async {
    if (nameController.text != "" &&
        familyController.text != "" &&
        nationalIdController.text != "" &&
        mobileController.text != "") {
      final dbHelper = DBHelper();

      dbHelper.update(
          id.toString(),
          nameController.text,
          familyController.text,
          birthday.toString(),
          mobileController.text,
          nationalIdController.text);

      navigateToUsersList(ctx);
    } else {
      showFlushBar(ctx, "error", "Please fill all fields");
    }
  }

A simple way to update the table in SQLite DB.一种更新 SQLite DB 中表的简单方法。

  • Here is my table这是我的桌子
id ID URL URL
  • SwiperList is my table name SwiperList是我的表名

  • Id is primary key with auto incrementation property Id是具有自动递增属性的主键

  • URL is simple text value URL是简单的文本值

     Future<int> update(String id, String url) async { final db = await database; return await db.rawUpdate( 'UPDATE SwiperList SET url = $url WHERE id = ${int.parse(id)}');

    } }

You can implement it according to your requirement.您可以根据您的要求实施它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM