0

Currently using SQlite, practicing how to save text from textfield. I can do it with only 1 textfield, but when make it even as low as 1 more textfield, it fails me.

Exception has occurred. SqfliteDatabaseException (DatabaseException(table claims has no column named number (code 1 SQLITE_ERROR): , while compiling: INSERT INTO claims (id, name, number) VALUES (NULL, ?, ?)) sql 'INSERT INTO claims (id, name, number) VALUES (NULL, ?, ?)' args [tt, 605])

// ignore_for_file: prefer_const_constructors

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';

// https://www.youtube.com/watch?v=noi6aYsP7Go
// this is where im getting all these from

main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(SqliteApp());
}

class SqliteApp extends StatefulWidget {
  const SqliteApp({Key? key}) : super(key: key);

  @override
  _SqliteAppState createState() => _SqliteAppState();
}

class _SqliteAppState extends State<SqliteApp> {
  int? selectedId;
  final textController = TextEditingController();
  final textController2 = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('TEST TEST')),
        body: SingleChildScrollView(
          child: Padding(
            padding: const EdgeInsets.all(10.0),
            child: Center(
              child: Column(
                children: [
                  TextField(
                    controller: textController,
                  ),
                  TextField(
                    controller: textController2,
                    keyboardType: TextInputType.number,
                  ),
                  SizedBox(
                    height: 500,
                    child: FutureBuilder<List<Claim>>(
                      future: DatabaseHelper.instance.getClaim(),
                      builder: (BuildContext context,
                          AsyncSnapshot<List<Claim>> snapshot) {
                        if (!snapshot.hasData) {
                          return Center(
                            child: Text('Loading...'),
                          );
                        }
                        return snapshot.data!.isEmpty
                            ? Center(
                                child: Text('No Claims in List.'),
                              )
                            : ListView(
                                children: snapshot.data!.map((claim) {
                                  return Center(
                                    child: Card(
                                      color: selectedId == claim.id
                                          ? Colors.grey
                                          : Colors.white,
                                      child: ListTile(
                                        title: Text(claim.name),
                                        onTap: () {
                                          setState(() {
                                            if (selectedId == null) {
                                              textController.text = claim.name;
                                              textController2.text =
                                                  claim.number;
                                              selectedId = claim.id;
                                            } else {
                                              textController.text = '';
                                              textController2.text = '';
                                              selectedId = null;
                                            }
                                          });
                                        },
                                        onLongPress: () {
                                          setState(() {
                                            DatabaseHelper.instance
                                                .remove(claim.id!);
                                          });
                                        },
                                      ),
                                    ),
                                  );
                                }).toList(),
                              );
                      },
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
        floatingActionButton: FloatingActionButton(
          child: Icon(Icons.save),
          onPressed: () async {
            if (textController.text.isEmpty) {
              null;
            } else {
              selectedId != null
                  ? await DatabaseHelper.instance.update(
                      Claim(
                          id: selectedId,
                          name: textController.text,
                          number: textController2.text),
                    )
                  : await DatabaseHelper.instance.add(
                      Claim(
                          name: textController.text,
                          number: textController2.text),
                    );
              setState(() {
                textController.clear();
                textController2.clear();
              });
            }
          },
        ),
      ),
    );
  }
}

class Claim {
  final int? id;
  final String name;
  final String number;

  Claim({this.id, required this.name, required this.number});

  factory Claim.fromMap(Map<String, dynamic> json) =>
      new Claim(id: json['id'], name: json['name'], number: json['number']);

  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'name': name,
      'number': number,
    };
  }
}

class DatabaseHelper {
  DatabaseHelper._privateConstructor();
  static final DatabaseHelper instance = DatabaseHelper._privateConstructor();

  static Database?
      _database; // if the variable doesnt exist,,,,,,,, this (under) will initalise the database, and use set variable
  Future<Database> get database async => _database ??= await _initDatabase();

  Future<Database> _initDatabase() async {
    Directory documentsDirectory = await getApplicationDocumentsDirectory();
    String path = join(documentsDirectory.path, 'claims.db');
    return await openDatabase(
      path,
      version: 2,
      onCreate: _onCreate,
    );
  }

  Future _onCreate(Database db, int version) async {
    await db.execute('''
      CREATE TABLE claims(
        id INTEGER PRIMARY KEY,
        name TEXT
        number TEXT
      )
    ''');
  }

  Future<List<Claim>> getClaim() async {
    Database db = await instance.database;
    var claims = await db.query('claims', orderBy: 'id');
    List<Claim> claimList =
        claims.isNotEmpty ? claims.map((c) => Claim.fromMap(c)).toList() : [];
    return claimList;
  }

  Future<int> add(Claim claim) async {
    Database db = await instance.database;
    return await db.insert('claims', claim.toMap());
  }

  Future<int> remove(int id) async {
    Database db = await instance.database;
    return await db.delete('claims', where: 'id = ?', whereArgs: [id]);
  }

  Future<int> update(Claim claim) async {
    Database db = await instance.database;
    return await db.update('claims', claim.toMap(),
        where: 'id = ?', whereArgs: [claim.id]);
  }
}

With this code, I keep getting the code 1 SQLITE_ERROR, i tried tweaking it over and over again but alas, can't find the solution. Still quite an amateur in flutter so I don't have much experience

2
  • Please post the full error too. Commented Feb 28, 2022 at 3:14
  • ah will do! forgive me, still quite new to this Commented Feb 28, 2022 at 3:16

2 Answers 2

2

I think you missed a comma when creating the Claim table

Future _onCreate(Database db, int version) async {
    await db.execute('''
      CREATE TABLE claims(
        id INTEGER PRIMARY KEY,
        name TEXT,
        number TEXT
      )
    ''');
  }

I would like to propose a better way for readability

db.execute(
  "CREATE TABLE claims("
    "id INTEGER PRIMARY KEY, "
    "name TEXT, "
    "number TEXT)"
);
Sign up to request clarification or add additional context in comments.

3 Comments

dude i thought you got it istg if that was the problem id punch myself, but alas i tried it but still had the same error coming out :/ but thank you tho! ill update that
I updated my answer, how about my proposed way, maybe you also missing the space after comma
just tried it but still getting the same error. :/// been on this for 2 days and its just one small thing jesus.
0

The name of the database table is not comma (,) after the TEXT of the column, name TEXT,

Change the version : 1, uninstall the app from your physical device and reinstall.

Hopefully the problem will be solved.

Future _onCreate(Database db, int version) async {
        await db.execute('''
          CREATE TABLE claims(
            id INTEGER PRIMARY KEY,
            name TEXT, //You didn't give a comma here
            number TEXT
          )
        ''');
      }

Otherwise follow my code

import 'package:sqflite/sqflite.dart' as sqlite;

class SQLHelper {
  static Future<sqlite.Database> db() async {
    return sqlite.openDatabase("info.db", version: 1,
        onCreate: (sqlite.Database database, int version) {
      database.execute(
          "CREATE TABLE note (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, title TEXT, description TEXT)");
      database.execute(
          "CREATE TABLE userdata (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, name TEXT, phone TEXT, email TEXT, password TEXT)");
    });
  }

  static Future<int> insertData(String title, String description) async {
    final db = await SQLHelper.db();
    var values = {"title": title, "description": description};
    return db.insert("note", values);
  }

  static Future<List<Map<String, dynamic>>> getAllData() async {
    final db = await SQLHelper.db();
    return db.query("note", orderBy: "id");
  }

  static Future<int> updateData(
      int id, String title, String description) async {
    final db = await SQLHelper.db();
    var values = {"title": title, "description": description};
    return db.update("note", values, where: "id = ?", whereArgs: [id]);
  }

  static Future<int> deleteData(int id) async {
    final db = await SQLHelper.db();
    return db.delete("note", where: "id = ?", whereArgs: [id]);
  }
}

4 Comments

I dont really understand what you mean, but im assuming it has to do with the comma (.) after 'name TEXT'. but i tried reinstalling it and check for updated version but still doesnt work
Future _onCreate(Database db, int version) async { await db.execute(''' CREATE TABLE claims( id INTEGER PRIMARY KEY, name TEXT, // You didn't give a comma here number TEXT ) '''); } contact me facebook.com/nezam.cst
ohh understood, if you saw the answer before this, they proposed this already, and ive tried it. but it still does not work
You see, I have given some more code...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.