PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, September 11, 2022

[FIXED] How do I print my inserted values in my console? (Flutter | sqflite)

 September 11, 2022     android-studio, cross-platform, flutter, mobile-application, sqflite     No comments   

Issue

I am currently successfully able to insert values into the system, however, I want to be able to display the inserted values in my console. How do I print the inserted values in my console? I tried doing so and I was only able to get the "id" of the inserted value. I'm not sure how to print the rest of the values in the table.

Here's my insert code:

  // Insert Operation: Insert a Note object to database
  Future<int> insertNote(Note note) async {
    //getting db reference
    Database db = await database;
    //running the insert command
    //our data from note is converted to map object
    var result = await db.insert(noteTable, note.toMap());
    if (kDebugMode) {
      print('Note data inserted successfully: $result');
    } 
    return result;
  }

Here's the code I used to convert the Note object into a Map object

  // Convert a Note object into a Map object
  Map<String, dynamic> toMap() {
    var map = <String,dynamic>{};
    if (id != null) {
      map['id'] = _id;
    }
    map['title'] = _title;
    map['description'] = _description;
    map['priority'] = _priority;
    map['color'] = _color;
    map['date'] = _date;

    return map;
  }

This is the result on my console:

console print result


Solution

When you insert row in database you will only get row id in return.

So if you want to print recent data then you have to make a query to your database with id or you can also print all the data available.

For table

db.query("table_name);

or

db.rawQuery('SELECT * FROM "table"');

For specific id

db.query("table_name",where: '$rowId = ?', whereArgs: [id]);

or

db.rawQuery('SELECT * FROM "table" WHERE $rowId = $id');

For more info check : https://pub.dev/packages/sqflite



Answered By - Diwyansh
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing