shantanuo
shantanuo

Reputation: 32336

Creating connection sring for mysql DB

I tried the exact same code as found here... https://github.com/TomMalkin/SimQLe

I am not sure how to connect to mysql database.

from simqle import ConnectionManager
cm = ConnectionManager("connections.yaml")

sql = "SELECT name, age FROM people WHERE category = :category"
params = {"category": 5}

result = cm.recordset(con_name="my-database", sql=sql, params=params)

Getting an error:

UnknownConnectionError: Unknown connection my-database

This is how I can connect to mysql database from command prompt.

mysql -h 172.31.84.39 -udba -pXXXX -P 3392

How do I write the connection string?

Upvotes: 1

Views: 167

Answers (1)

shiratori3
shiratori3

Reputation: 146

I usually use sqlalchemy to connect mysql database. I have readed the document of SimQLe which you are using. In SimQLe document,

cm = ConnectionManager("connections.yaml")

is the way to connect to database and you should put your login param in this yaml file called connections.yaml.

Here is the offical document simple: https://github.com/TomMalkin/SimQLe#the-connectionsyaml-file

connections:
 
    # The name of the connection - this is what will be used in your project
    # to reference this connection.
  - name: my-sql-server-database
    driver: mssql+pyodbc:///?odbc_connect=
    connection: DRIVER={SQL Server};UID=<username>;PWD=<password>;SERVER=<my-server>
 
    # some odbc connections require urls to be escaped, this is managed by
    # setting url_escaped = true:
    url_escape: true

    # File based databases like sqlite are slightly different - the driver
    # is very simple.
  - name: my-sqlite-database
    driver: sqlite:///
    # put a leading '/' before the connection for an absolute path, or omit
    # if it's relative to the project path
    connection: databases/my-database.db
    #  This connection will be used if no name is given if the default 
    # parameter is used:
    default: true

Maybe you should change some params in here:

    driver: mssql+pyodbc:///?odbc_connect=
    connection: DRIVER={SQL Server};UID=<username>;PWD=<password>;SERVER=<my-server>

And from the document, it says that SimQle is built on SQLAlchemy,

SimQLe is built on top of the fantastic SQLAlchemy library. 

maybe you can use the SQLAlchemy's login params to connect the database in SimQLe. Such like this:

mysql+pymysql://<username>:<password>@<host>/<dbname>[?<options>]

Changed to:

    driver: mysql+pymysql://
    connection: <username>:<password>@<host>/<dbname>[?<options>]

Offical documents:

https://simqle.readthedocs.io/en/latest/ https://docs.sqlalchemy.org/en/14/dialects/mysql.html#module-sqlalchemy.dialects.mysql.pymysql https://docs.sqlalchemy.org/en/14/dialects/mssql.html#module-sqlalchemy.dialects.mssql.pyodbc

Upvotes: 2

Related Questions