sqlite - 如何在我的飞镖代码 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)
解决方案
语法错误。你忘了把 ) 放在行尾
$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'。所以你应该把名字改成“城市”。
推荐阅读
- android - React Native 55.4:contextMenuHidden 在 Android 上不隐藏菜单 - 禁用 React-Native 文本输入上的选项
- c++ - 如何正确检查 C++ 中是否存在注册表项?
- quill - 使用 StimulusJS 和 Quill 编辑器的陈旧数据
- sql - 错误消息:“PostgreSQL 说:无法写入临时文件的块 119518:设备上没有剩余空间”PostgreSQL
- sql - 数据库我需要对相关字段进行反规范化吗?
- css - 使用 CSS 的 @media 打印查询使我的打印机崩溃
- javascript - 如何将 File 对象呈现为 HTML 元素(使用 Vue.js)
- c++ - unordered_set 是存储向量的合适数据结构吗
元素?如果是这样,我将如何实现散列函数? - javascript - 使用基于 Web 的 javascript 编辑器自动完成主机对象
- android - Android:保护付费应用免遭盗版?