最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

I can't see if my python-mysql connection is active - Stack Overflow

programmeradmin0浏览0评论

I'm using python and I'm trying to make a connection to a MySQL db. The problem is that when I run this code:

import mysql.connector
print('Attempting connection to database...')

con = mysql.connector.connect(
    host="localhost",
    user="root",
    password="root"
)

print(con)

The terminal doesn't shows me the print. I've been debugging and I noticed that the program executes till the line 7 then jumps directly to the line 4 and finish the execution. I don't know if this behavior is normal but I'd like to know how to solve this, please :D

I'm also using MySQL Workbench to see if the connection is ok and it actually works.

I'm using python and I'm trying to make a connection to a MySQL db. The problem is that when I run this code:

import mysql.connector
print('Attempting connection to database...')

con = mysql.connector.connect(
    host="localhost",
    user="root",
    password="root"
)

print(con)

The terminal doesn't shows me the print. I've been debugging and I noticed that the program executes till the line 7 then jumps directly to the line 4 and finish the execution. I don't know if this behavior is normal but I'd like to know how to solve this, please :D

I'm also using MySQL Workbench to see if the connection is ok and it actually works.

Share Improve this question edited Feb 2 at 18:17 Shadow 34.3k10 gold badges65 silver badges75 bronze badges asked Feb 2 at 18:04 VictorM03VictorM03 33 bronze badges 4
  • your example works for me. the print statement should return something that looks like Attempting connection to database... <mysql.connector.connection_cext.CMySQLConnection object at 0x00000241E0DE6510>. – chickity china chinese chicken Commented Feb 2 at 19:32
  • You probably have a connection issue, I would suggest first ensuring that you can connect to your MySQL db from a simple command terminal to ensure your MySQL setup is working (e.g. cmd.exe, bash, etc. depending on your OS). If successful, then implement the connection in Python. – chickity china chinese chicken Commented Feb 2 at 19:33
  • Try getting a mysql connection from a terminal ... if your MySQL server is running, and your MySQL credentials for user is root, password is root, then C:\> mysql -u root -p should prompt you to enter your password, then drop you into a mysql> terminal with a successful connection. If not, then there are other issues to debug. – chickity china chinese chicken Commented Feb 2 at 19:36
  • I did everything you suggested me and the connection seems to be right. The problem was still there but I founded another method that is "mysql.connector.MySQLConnection" and it seems to solve the problem but I'm still intrigued :) – VictorM03 Commented Feb 3 at 0:30
Add a comment  | 

1 Answer 1

Reset to default 0

If you're new to database connections, perhaps my library might help you with some of this. https://github/hotnsoursoup/elixirdb

pip install elixirdb[mysql]

Then you can define a yaml config, or a dictionary.

from elixirdb import ElixirDB



config = {
    "dialect": "mysql",
    "url_params": {
        "host": "localhost",
        "port": 3306,
        "database": "elixirdb",
        "user": "test_user",
        "password": "StrongPassword!123",
    },
    "engine_options": {
        "pool_size": 20,
        "pool_recycle": 3600,
        "max_overflow": 10,
        "echo": False,
    },
}


db = ElixirDB(config)
db.connect()


if db.has_connection():
    print(db.connection)

Output

<sqlalchemy.engine.base.Connection object at 0x000001C7FFA64AA0>

print the dict for class Connection

from pprint import pprint

pprint(db.connection.__dict__)
{'_Connection__can_reconnect': True,
'_Connection__in_begin': False,
'_Connection__savepoint_seq': 0,
'_allow_autobegin': True,
'_dbapi_connection': <sqlalchemy.pool.base._ConnectionFairy object at 0x00000205EF2551F0>,
'_echo': False,
'_execution_options': immutabledict({}),
'_has_events': False,
'_nested_transaction': None,
'_transaction': None,
'dialect': <sqlalchemy.dialects.mysql.pymysql.MySQLDialect_pymysql object at 0x00000205EF175040>,
'dispatch': <sqlalchemy.event.base.JoinedConnectionEventsDispatch object at 0x00000205EF22B100>,
'engine': Engine(mysql+pymysql://test_user:***@localhost:3306/elixirdb)}

It's a new library, so any stars on it would be appreciated =)

发布评论

评论列表(0)

  1. 暂无评论