为mysql数据库创建连接sring

2024-09-30 16:33:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试了与这里找到的完全相同的代码。。。 https://github.com/TomMalkin/SimQLe

我不知道如何连接到mysql数据库

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)

获取错误:

UnknownConnectionError: Unknown connection my-database

这就是我如何从命令提示符连接到mysql数据库的方法

mysql -h 172.31.84.39 -udba -pXXXX -P 3392

如何写入连接字符串


Tags: 代码namehttpsgithubcom数据库sqlmy
1条回答
网友
1楼 · 发布于 2024-09-30 16:33:07

我通常使用sqlalchemy连接mysql数据库。我已经阅读了您正在使用的SimQLe文档。在SimQLe文档中

cm = ConnectionManager("connections.yaml")

是连接到数据库的方法,您应该将登录参数放在名为connections.yaml的yaml文件中

以下是简单的官方文件: 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

也许您应该在这里更改一些参数:

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

从文档中可以看出,SimQle是基于SQLAlchemy构建的

SimQLe is built on top of the fantastic SQLAlchemy library. 

也许您可以使用SQLAlchemy的登录参数来连接SimQLe中的数据库。例如:

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

改为:

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

官方文件:

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

相关问题 更多 >