3

Possible Duplicate:
How do I connect to a MySQL Database in Python?

There is a way to connect python with a mysql db? I found something, but seems to is only for windows and I'm working with linux.

Community
  • 1
  • 1
pythonFoo
  • 2,194
  • 3
  • 15
  • 17

2 Answers2

4

Use MySQL-Python.

I also strongly advise having a look at SQLAlchemy, an ORM that allows you to write once for all major sql's including MySQL. It's a bit more complex, but the flexibility is a serious advantage.

moinudin
  • 134,091
  • 45
  • 190
  • 216
  • Can't use SQL Alchemy if you can't connect. – duffymo Dec 26 '10 at 22:00
  • @duffymo Not true. SQLAlchemy handles the connection for you. It's not as if OP was unable to connect to the database at all. – moinudin Dec 26 '10 at 22:05
  • "There is a way to connect python with a mysql db?" - no, s/he couldn't connect. And SQLAlchemy might be doing it for you, but it still has to be done. That's beyond dispute. – duffymo Dec 26 '10 at 22:14
  • @duffymo So what is your point then? OP's question is to connect to MySQL from Python. SQLAlchemy is one way of connecting to MySQL from Python. Where's the dispute? The question wasn't "Cannot connect to mysql *at all*. How do I save my db using Python?" – moinudin Dec 26 '10 at 22:21
  • No, I'm saying that the question was "how do I connect to MySQL DB?" I quoted it from the original question. I think the right answer was your first bit about MySQL Python; SQLAlchemy is over and above. Personally, I wouldn't recommend such a thing to somebody who hasn't connected successfully at all. – duffymo Dec 26 '10 at 23:22
  • 1
    @duffymo The comment about SQLAlchemy was meant an aside once he has something working. Hence the "use mysql-python" vs "have a look at sqlalchemy". – moinudin Dec 26 '10 at 23:26
  • I'll buy that...point still stands. SQLAlchemy might be fine if it solves the connection problem, but not until then. – duffymo Dec 26 '10 at 23:33
3

You can use the MySQL-Python package. Search your disto's package database first before installing from source (you probably already have it there). Be aware that all database packages do (our should) adhere to the Python DB API. This provides a standardized interface to all databases.

If you are comfortable with writing SQL, then that should suffice. If you prefer to query and define data in pure python I would also recommend SQLAlchemy.

Keith
  • 42,110
  • 11
  • 57
  • 76