skip to Main Content

I have recently started developing a flutter/dart project getting some basic pages set up and navigation.

I have a MySQL database that I would like to incorporate and I found the mysql1 package to help me do this. I’ve imported the package and used the examples found here.

The problem I’m having is that I can’t seem to see any of the data in the db.

in my Main.dart I have this:

 import 'dart:async';
 import 'package:mysql1/mysql1.dart';

 Future main() async {
   // Open a connection (testdb should already exist)
   final connection = await MySqlConnection.connect(new ConnectionSettings(
     host: '10.0.2.2',
     port: 3306,
     user: 'root',
     password: 'gDrP^6W42f9Z',
     db: 'EntomologyApp',
   ));

   var results = await connection.query('select * from Account'); // expected results
   print(results);

   // Finally, close the connection
   await connection.close();
 }

After trying so many different approaches and not reaching any real solution I don’t know what to try next.

Here, I expect a connection to made to MySQL DB and the data to be accessed through the connection.query select statement. I then expect it to print out the results just so I can clarify there is results.

When I run the exact same query in SQL workbench, I get results of dummy data I have inserted for testing but here all I get in the console is ‘()’ which I would assume is just an empty list of potential records.

Now, I know it may seem like there’s a connection issue, but if I put this line of code in just above the other query code:

   await connection.query(
       'CREATE TABLE users (id int NOT NULL AUTO_INCREMENT PRIMARY KEY, name varchar(255), email varchar(255), age int)');

it works and creates a ‘users’ table in workbench, then any time I run after, it will say a table already exists called ‘users’. I have this code here just to test that table creation works. I want to access the data in the ‘Account’ table however, of which there are plenty of records now.

EDIT:
I have now tested inserting a single record with:
await connection.query('insert into Account (email, password) values ("[email protected]", "password123")');

and it successfully creates a record in the Account table, so creation statements seem to work but select doesn’t.

2

Answers


  1. Chosen as BEST ANSWER

    I do not have an answer or solution to this problem, during the troubleshooting of this post I played around with using the http package and followed a very insightful straight forward tutorial series by the CodingCafe on YouTube which has helped me now create a reliable connection with my Flutter Dart project and my backend SQL database which I've now migrated to a XAMPP server.

    Anyone struggling to work with mysql1, I would recommend http as it does seem more flexible to use when working with json payloads.

    Thank you for the help


  2. Obviously you would have to change the ConnectionSettings and table name to fit yours, but this code works for me:

    import 'package:mysql1/mysql1.dart';
    
    Future main(List<String> arguments) async {
      MySqlConnection? con;
      try {
        con = await MySqlConnection.connect(ConnectionSettings(
          host: "localhost",
          port: 3306,
          db: "world",
          user: "root",
          password: null,
          characterSet: CharacterSet.UTF8MB4,
        ));
    
        Results results = await con.query("SELECT * FROM categories");
        print(results);
      } catch (e) {
        print(e);
      } finally {
        if (con != null) await con.close();
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search