4

I am developing a Flutter app that uses SQLite database using Flutter SQFlite plugin. Issue is that when I try to insert some test data, app prints following in the console:

Unhandled Exception: DatabaseException(no such column: dummy_value (code 1): , while compiling: INSERT INTO DemoTable (name) VALUES(dummy_value)) sql 'INSERT INTO DemoTable (name) VALUES(dummy_value)' args []}

As far as I can understand from the log, issue is with the column 'dummy_value' but here lies the real problem, my database has column 'id' and 'name' whereas the 'dummy_value' is the value, sent to be inserted in column 'name'.

Following is my code for creating database:

class DatabaseCreator {
static const table = 'DemoTable';
static const id = 'id';
static const name = 'name';
static Database db;

initDatabase() async {
Directory documentsDirectory = await 
getApplicationDocumentsDirectory();
String path = join(documentsDirectory.path, 'Demo.db');
db = await openDatabase(path, version: 1, onCreate: _onCreate);
return db;
}

Future<void> _onCreate(Database db, int version) async {
await db.execute('''
      CREATE TABLE $table (
        $id INTEGER PRIMARY KEY AUTOINCREMENT,
        $name TEXT
      )
      ''');
  }
}

And following code for inserting data into database:

class DBOP {
static insertName(String name) async {
Database db = await DatabaseCreator().initDatabase();
final result = await db.rawInsert(
    'INSERT INTO ${DatabaseCreator.table} (${DatabaseCreator.name}) 
VALUES($name)');
print(result);
 }
}

And at last, following is my Stateful widget:

class StFull extends StatefulWidget {
@override
_StFullState createState() => _StFullState();
}

class _StFullState extends State<StFull> {
@override
Widget build(BuildContext context) {
return Scaffold(
  appBar: AppBar(
    title: Text('DB Demo'),
  ),
  body: Container(
    child: RaisedButton(
      onPressed: _insertData,
      child: Text('INSERT DATA'),
    ),
    ),
  );
}

_insertData() {
DBOP.insertName('dummy_value');
 }
}
5
  • I believe that you need single quote marks around $name in the insert statement such as "values('$name')" Commented Aug 29, 2019 at 16:37
  • Thanks @mlewis54, will try and let you know Commented Aug 29, 2019 at 16:39
  • Not working, even syntax error is being thrown :'( Commented Aug 29, 2019 at 16:40
  • 1
    That's probably because you are using single quotes for the string for the INSERT statement. Change those to double quotes: "INSERT ...values('$name')" Commented Aug 29, 2019 at 16:43
  • @mlewis54..you are a life savor, thanks Commented Aug 29, 2019 at 16:46

1 Answer 1

7

You should use paramaters for values (especially strings)

Instead of:

await db.rawInsert(
    'INSERT INTO ${DatabaseCreator.table} (${DatabaseCreator.name}) VALUES($name)');

Do something like:

await db.rawInsert(
    'INSERT INTO ${DatabaseCreator.table} (${DatabaseCreator.name}) VALUES(?)', [name]);

or even simpler:

await db.insert(DatabaseCreator.table, <String, dynamic>{DatabaseCreator.name: name});
Sign up to request clarification or add additional context in comments.

1 Comment

+1 ! This works for me ! The part await db.rawInsert( 'INSERT INTO ${DatabaseCreator.table} (${DatabaseCreator.name}) VALUES(?)', [name]);

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.