简体   繁体   English

Sqflite 数据库异常在 flutter 中没有这样的表

[英]Sqflite Database exception no such table in flutter

I have loaded json data in a list of objects.我已经在对象列表中加载了 json 数据。 Whenever I try to insert data into a table my console says "Unhandled Exception: DatabaseException(no such table: Album (code 1 SQLITE_ERROR): , while compiling: INSERT OR REPLACE INTO Album....". I know before inserting data I have to create database and table. I have done that in my code. But for some unknown reason the table doesn't get build. Here is my database helper class -每当我尝试将数据插入表中时,我的控制台都会显示“未处理的异常:DatabaseException(没有这样的表:专辑(代码 1 SQLITE_ERROR):,同时编译:插入或替换为专辑......”。我知道在插入数据之前我必须创建数据库和表。我已经在我的代码中做到了。但由于某些未知原因,表没有得到构建。这是我的数据库助手 class -

class DatabaseHelper {
  static final _dbName = "myDatabase.db";
  static final _dbVersion = 1;

  static final _tableAlbum = "Album";
  static final columnAlbum = '_fetched_album';



  DatabaseHelper._privateConstructor();

  static final DatabaseHelper instance = DatabaseHelper._privateConstructor();

  static Database? _database;

  Future<Database?> get database async {
    if (_database == null) {
    print("instance.db null");
    _database ??= await _initiateDatabase();
    return _database;
    }else{
    print("instance.db not null");
    return _database; }


     } 

    Future<Database> _initiateDatabase() async {
    String directory = await getDatabasesPath();
    String path = join(directory, _dbName);

    return await openDatabase(
    path,
    version: _dbVersion,
    onCreate:(Database db,int version) async {

    await db.execute('''
    CREATE TABLE $_tableAlbum (
    $columnAlbum TEXT
    )''');


    },
    );


    }




    Future<int?> insertAlbum(ModelAlbum list) async {
    Database? db = await instance.database;
    return await db?.insert(
    _tableAlbum,list.toMap(),conflictAlgorithm: ConflictAlgorithm.replace);
    }
    Future<int?> insertPhoto(ModelPhoto list) async {
    Database? db = await instance.database;
    return await db?.insert(
    _tablePhoto,list.toMap(),conflictAlgorithm: ConflictAlgorithm.replace);
    }


    Future<List<Map<String, dynamic>>> queryAlbum() async {
    Database? db = await instance.database;
    return await db!.query(_tableAlbum);
    }
    Future<List<Map<String, dynamic>>> queryPhoto() async {
    Database? db = await instance.database;
    return await db!.query(_tableAlbum);
    }

    Future<List<ModelAlbum>> retrieveAlbum() async {
    final Database? db = await database;
    final List<Map> maps = await db!.query(_tableAlbum);
    return List.generate(maps.length, (i) {
    return ModelAlbum(
    id: maps[i]['id'],
    title: maps[i]['title'],
    userId: maps[i]['userId'],
    );
    });
    }
    Future<List<ModelPhoto>> retrievePhoto() async {
    final Database? db = await database;
    final List<Map> maps = await db!.query(_tablePhoto);
    return List.generate(maps.length, (i) {
    return ModelPhoto(
    albumId: maps[i]['albumId'],
    thumbnailUrl: maps[i]['thumbnailUrl'],
    url: maps[i]['url'],
    id: maps[i]['id'],
    title: maps[i]['title'],
    );
    });
    } 
    }

Whenever Database.instance.insert(object);每当 Database.instance.insert(object); gets called the database and table should be automatically created.被调用的数据库和表应该被自动创建。 But it doesn't.但事实并非如此。

You are referring to static fields from instance methods.您指的是实例方法中的static字段。 this may a problem try removing static keyword from the fields as,这可能是一个问题尝试从字段中删除 static 关键字,

final _dbName = "myDatabase.db";
final _dbVersion = 1;

final _tableAlbum = "Album";
final columnAlbum = '_fetched_album';

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

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