Flutter ios db.execute("Create Table) 在 onCreate 中不起作用

问题描述 投票:0回答:1

大家好,今天我发现 sqflite 的 db.execute 方法在 ios 模拟器上运行我的应用程序时不起作用,我不知道为什么? 它给了我一个错误“没有这样的表”,似乎代码没有在数据库中创建表,在 onCreate 方法中运行的代码对数据库没有影响,并且没有引发任何错误。 这是我的代码:

 Future<void> _initializeDatabase() async {
    if (Platform.isWindows || Platform.isLinux) {
      sqfliteFfiInit();
    }

    String documentFolder = (await getApplicationDocumentsDirectory()).path;
//
    String path = kDebugMode
        ? "E:\\Flutter-Projects\\online_order_project"
        : documentFolder;

    String fileDatabase = "$path\\online_order_db.db";

    if (Platform.isAndroid) {
      String p = (await getExternalStorageDirectory())!.path;

      fileDatabase = "$p\\online_order_db2.db";
    } else if (Platform.isIOS) {
      String p = (await getApplicationDocumentsDirectory()).path;

      fileDatabase = pathlib.join(p, "online_order_db-ios");
    }

    DataManager._sqliteDatabase = await databaseFactoryFfi.openDatabase(
        fileDatabase,
        options: OpenDatabaseOptions(
            onUpgrade: _onUpgrade,
            onConfigure: _onConfigure,
            onCreate: _onCreate,
            version: 4));

    DataManager._appDatabase = _sqliteDatabase;
  }
_onConfigure(Database db) async {
    await db.execute('PRAGMA foreign_keys = off');

    print("DATABASE VERSION${await db.getVersion()}");
  }

  _onCreate(Database db, int version) async {
    try {
      String sql =
          await rootBundle.loadString('assets/files/shopping_card_db.sql');
      await db.execute('PRAGMA foreign_keys = off');

      await db.execute(sql);//===this line does not work (does not alter the database)

      await db.execute('PRAGMA foreign_keys = on');
    } catch (ex) {
      print(ex.toString());
    }
  }

  _onUpgrade(Database db, int oldVer, int newVer) async {
    if (oldVer < newVer) {
      String sql =
          await rootBundle.loadString('assets/files/shopping_sql_ver3.sql');

      await db.execute(sql);
    }
  }

这是位于资产/文件上的shopping_card_db.sql:

--
-- File generated with SQLiteStudio v3.3.3 on Sun Aug 7 08:47:35 2022
--
-- Text encoding used: UTF-8
--
PRAGMA foreign_keys = off;




-- Table: tblorder
DROP TABLE IF EXISTS tblorder;

CREATE TABLE tblorder (
    id              INTEGER PRIMARY KEY AUTOINCREMENT,
    order_date      DATE,
    customer_id     INTEGER,
    user_id         INTEGER,
    status          BOOLEAN,
    total_amount    NUMERIC,
    order_number    INTEGER,
    discount_ratio  NUMERIC,
    discount_amount NUMERIC,
    net_total       NUMERIC,
    tax_total       NUMERIC,
    discount_total  NUMERIC,
    cashPaid        NUMERIC,
    invoice_number  VARCHAR (20),
    is_later        INTEGER (1)  DEFAULT (0) 
);


-- Table: category
DROP TABLE IF EXISTS category;

CREATE TABLE category (
    id                  INTEGER       PRIMARY KEY AUTOINCREMENT,
    name_english        VARCHAR (70),
    name_arabic         VARCHAR (70),
    notes               VARCHAR (300),
    image_url           TEXT,
    category_order      INTEGER (3),
    package_no_of_items INTEGER (3) 
);


-- Table: customer
DROP TABLE IF EXISTS customer;

CREATE TABLE customer (
    id          INTEGER       PRIMARY KEY AUTOINCREMENT,
    first_name  VARCHAR (50),
    last_name   VARCHAR (50),
    middle_name VARCHAR (50),
    email       VARCHAR (100),
    phone       VARCHAR (20),
    username    VARCHAR (20),
    password    VARCHAR (8),
    status      BOOLEAN
);


-- Table: order_detail
DROP TABLE IF EXISTS order_detail;

CREATE TABLE order_detail (
    id              INTEGER      PRIMARY KEY AUTOINCREMENT,
    order_id        INTEGER      REFERENCES tblorder (id) ON DELETE CASCADE,
    amount          NUMERIC,
    quantity        INTEGER (3),
    product_id      VARCHAR (20),
    discount_amount NUMERIC,
    discount_ratio  NUMERIC,
    total_amount    NUMERIC,
    net_total       NUMERIC
);


-- Table: order_payment
DROP TABLE IF EXISTS order_payment;

CREATE TABLE order_payment (
    id              INTEGER       PRIMARY KEY AUTOINCREMENT,
    order_id        INTEGER       REFERENCES tblorder (id),
    payment_type_id INTEGER,
    amount          REAL,
    payment_date    DATE,
    paid_by         VARCHAR (100),
    user_id         INTEGER
);


-- Table: payment_type
DROP TABLE IF EXISTS payment_type;

CREATE TABLE payment_type (
    id           INTEGER       PRIMARY KEY AUTOINCREMENT,
    name_english VARCHAR (100),
    name_arabic  VARCHAR (100),
    cache_type   INTEGER (1) 
);


-- Table: product
DROP TABLE IF EXISTS product;

CREATE TABLE product (
    id           VARCHAR (20)  PRIMARY KEY,
    name_english VARCHAR (150),
    category_id  INTEGER       REFERENCES category (id),
    image        BLOB,
    price        NUMERIC,
    image_url    TEXT,
    name_arabic  VARCHAR (150),
    unit_id      INTEGER,
    tax_ratio    INTEGER
);


-- Table: setting
DROP TABLE IF EXISTS setting;

CREATE TABLE setting (
    id           INTEGER       PRIMARY KEY AUTOINCREMENT,
    name_arabic  VARCHAR (255),
    name_english VARCHAR (255),
    ip           VARCHAR (20),
    port         INTEGER (3),
    path         VARCHAR (255),
    year         INTEGER (3),
    company      VARCHAR (20) 
);




-- Table: unit
DROP TABLE IF EXISTS unit;

CREATE TABLE unit (
    id           INTEGER       PRIMARY KEY AUTOINCREMENT,
    name_arabic  VARCHAR (150),
    name_english VARCHAR (150) 
);


-- Table: user
DROP TABLE IF EXISTS user;

CREATE TABLE user (
    id               INTEGER       PRIMARY KEY AUTOINCREMENT,
    username         VARCHAR (20),
    password         VARCHAR (20),
    phone            VARCHAR (20),
    image            BLOB,
    username_arabic  VARCHAR (120),
    username_english VARCHAR (120) 
);



PRAGMA foreign_keys = on;

所以当我运行“从设置中选择*”时,它给出错误: “没有这样的餐桌设置”

ios flutter sqlite dart sqflite
1个回答
0
投票

你有没有弄清楚这一点?我在 Android 模拟器中遇到了完全相同的问题。

© www.soinside.com 2019 - 2024. All rights reserved.