1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Flutter] Flutter MySql select query not working with mysql1 package

Discussão em 'Mobile' iniciado por Stack, Outubro 9, 2024 às 12:52.

  1. Stack

    Stack Membro Participativo

    I am new to Flutter and mysql. I wanted to create an app where the user will be prompted questions that are stored in a database. I established a connection using the mysql1 package in flutter. I know the connection is valid because the "insert into ..." statements work properly. However I can't pull any data from the database.

    The following is the code block with the queries. The values inserted are dummy values just to test.

    String questionText = "";
    var db = MySql();

    void getData(String questionID) async {
    await db.getConnection().then((conn) async {
    await conn.query("insert into questions values ('a', 'a', 'a', 'a', 'a', 'a', '2')");
    String qry = "select * from questions";
    await conn.query(qry).then((results) {
    print(results);
    if (results.isNotEmpty) {
    for (var row in results) {
    setState(() {
    questionText = row[0];
    });
    }
    } else {
    print("no data");
    questionText = "no data";
    }

    });
    });
    }



    This function is called here:

    @override
    void initState() {
    super.initState();
    getData("1");
    }


    and the db creation is done here:

    class MySql {
    static String _host = "localhost",
    _user = "root",
    _password = "mypassword",
    _db = "mydb";
    static int _port = 3306;

    MySql();

    Future<MySqlConnection> getConnection() async {
    var settings = new ConnectionSettings(
    host:_host,
    port: _port,
    user: _user,
    password: _password,
    db: _db
    );

    return await MySqlConnection.connect(settings);
    }
    }


    As I said, the insert statement inserts rows successfully but I can't get any data with select.

    Continue reading...

Compartilhe esta Página