首页 > 解决方案 > 如何在我的飞镖代码 sqlite 中解决这个问题

问题描述

我的代码中有一个问题,在 database_helper.dart 中午餐时出现错误

database_helper.dart 是 Sqlite 存储数据学习项目中的一个文件,我遇到了这个问题,我尝试了很多解决方案,比如卸载应用程序并重新安装

谁能帮我 ?

======

  class DataBaseHelper {
    static Database _db ;
    final String userT = 'userT' ;
    final String columnId = 'id' ;
    final String columnUserName = 'username' ;
    final String columnPassword = 'password' ;
    final String columnAge = 'age' ;
    final String columnCity = 'city' ;
=========

  Future<Database> get dbase async {
    if(_db != null) {
      return _db ;
    }
    _db = await intDB();
    return _db ;

  }
=================

  intDB() async {
    Directory docDirectory = await getApplicationDocumentsDirectory() ;
    String path = join(docDirectory.path , 'myDB.db') ;
    var myOwnDB = await openDatabase(path , version: 1 , onCreate: _onCreate);
    return myOwnDB ;
  }
=========================

  void _onCreate(Database db , int newVersion) async {
    var sql = 'CREATE TABLE $userT ($columnId INTEGER PRIMARY KEY UNIQUE ,'
        ' $columnUserName TEXT , $columnPassword TEXT , $columnCity TEXT , $columnAge INTEGER ' ;
    await db.execute(sql) ;
  }

错误日志:

E/SQLiteLog(14607): (1) "INTEGER" 附近: 语法错误

I/flutter (14607): error DatabaseException(near "INTEGER": syntax error (code 1): , while compile: CREATE TABLE userT (id INTEGER PRIMARY KEY UNIQUE , username TEXT , password TEXT , city TEXT , age INTEGER) during oenter代码herepen,关闭...

E/flutter (14607): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] 未处理的异常: DatabaseException(near "INTEGER": syntax error (code 1): , while compile: CREATE TABLE userT (id INTEGER) PRIMARY KEY UNIQUE , 用户名 TEXT , 密码 TEXT , 城市 TEXT , 年龄 INTEGER)

标签: sqliteintellij-ideaflutterdart

解决方案


语法错误。你忘了把 ) 放在行尾

$columnAge INTEGER )

您可以在https://sqliteonline.com/检查您的 sqllite 代码

CREATE TABLE userT (columnId INTEGER PRIMARY KEY UNIQUE ,
        columnUserName TEXT , columnPassword TEXT , columnCity TEXT , columnAge INTEGER)

放后)。它工作正常。

在此处输入图像描述

编辑:您在评论中提到的 Insert Into 语句的第二个问题

Insert Into 语句的语法错误是列“userCity”不存在。
在您提供的问题中,当创建表时,您使用名称 columnCity = 'city'。所以你应该把名字改成“城市”。


推荐阅读