简体   繁体   中英

How Can I Solve This Problem in my dart code sqlite

I have a problem in my code that occurs error in lunching in database_helper.dart

database_helper.dart is one file in Sqlite storing data learning project and i faced this issue , i tried many solutions like uninstall app and install it again

can any one help me ?

======

  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) ;
  }

error log:

E/SQLiteLog(14607): (1) near "INTEGER": syntax error

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

E/flutter (14607): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: DatabaseException(near "INTEGER": syntax error (code 1): , while compiling: CREATE TABLE userT (id INTEGER PRIMARY KEY UNIQUE , username TEXT , password TEXT , city TEXT , age INTEGER)

Syntax error. you forgot to put ) at the end of line

$columnAge INTEGER )

You can check your sqllite code in https://sqliteonline.com/

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

after put ) . it works fine.

在此处输入图片说明

Edit : Second question of Insert Into statement you mentioned in comments

The syntax error of Insert Into statement is column "userCity" does not exist.
In the question you provided, when Create table you use the name columnCity = 'city'. so you sholud change the name to "city".

Try removing UNIQUE

They have not included that word in any of the samples, so maybe it has not been implemented.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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