Giter Site home page Giter Site logo

mysql_manager's Introduction

MySQLManager is a dependency thought to be used as a controller for the connection to a MySQL or MariaDB database. It used the dependency mysql1 as the driver for the database connection. Connect to your database with only configuring a simple .env file!

Features

Connect to a MySQL database with a .env file and manage the connection easily. Also, you can pass the credentials as a Map, but is much easier to use the .env file.

Getting started

You only have to setup your .env file at the root of your app img

Once you have this configuration of above you can read Usage.

Usage

import 'package:mysql_manager/src/mysql_manager.dart';



// ignore: slash_for_doc_comments
/*********************************************
 *                                           *
 *  Visit the test folder at the github repo *
 *  in order to check if your .env file is   *
 *  well configured.                         *
 *  https://github.com/raul94a/mysql_manager *
 *                                           *
 ********************************************/

void main() async{

  //There's two ways to stablish a connection to MySQL using this dependency
  //1. With a .env file 
  //You're suposed to create a .env file at the root of your application
  //This file will have the following structure (adding more properties IS NOT problematic)

          //db=YOUR_DB_NAME
          //host=YOUR_HOST
          //user=YOUR_MYSQL_USER
          //password=YOUR_PASSWORD
          //port=PORT
//Once the .env file contains the credentials you can connect as easy as the following:

//The only way to instanciate MySQLManager is with the instance getter
final MySQLManager manager = MySQLManager.instance;
//initialize the connection. Init method will return a MySqlConnection object
final conn = await manager.init();
//you can pass sql to the query method
final results = await conn.query('select * from test');
//results will be a iterator,so is possible to loop over it
for (var r in results){
  //returns data in Map<String,dynamic> format
  print(r.fields);
}

await conn.close();
//also you can use await manager.close();

//2. Using the configuration map. This method is the same as the first with a little variation
//final conn = await manager.init(false, {'db'='YOUR_DB', 'host': 'YOUR_HOST', 'user':'YOUR_USER', 'password':'YOUR_PASSWORD', 'port': port}); //=> port is an integer, be careful

}
}

mysql_manager's People

Contributors

raul94a avatar

Watchers

 avatar

mysql_manager's Issues

Failed to run the package with flutter web

I'm getting an error, when I try and catch: ErrorUnsupported operation: _Namespace

my .env in root looks like this:

db=from_java
host=127.0.0.1
user=root
password=
port=3306

my connection file looks like:

import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:mysql_manager/src/mysql_manager.dart';

class ConnectMysql {
  Future<void> connectDB() async {
    try {
      final MySQLManager manager = MySQLManager.instance;
      //initialize the connection. Init method will return a MySqlConnection object
      final conn = await manager.init();
      //you can pass sql to the query method
      final results = await conn.execute('select * from employees');
      //results will be a iterator,so is possible to loop over it
      for (var r in results) {
        //returns data in Map<String,dynamic> format
        debugPrint(r.rows.toString());
      }
    } on Exception catch (e) {
      log("Exception $e");
    } on Error catch (e) {
      log("Error $e");
    }

    // await conn.close();
  }
}

I called the connection in main using a button like this:

Center(
          child: IconButton(
              color: Colors.blue,
              onPressed: () async {
                try {
                  await connectMysql.connectDB();
                } on Exception catch (e) {
                  log("Error $e");
                }
              },
              icon: const Icon(Icons.connect_without_contact)),
        ),

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.