标签云

Python和MySQL

I can get Python to work with Postgresql but I cannot get it to work with MySQL. The main problem is that on the shared hosting account I have I do not have the ability to install things such as Django or PySQL, I generally fail when installing them on my computer so maybe it's good I can't install on the host.

I found bpgsql really good because it does not require an install, it's a single file that I can look at, read and then call the functions of. Does anybody know of something like this for MySQL?

2016年12月11日45分05秒

MySQLdb is what I have used before.

If you host is using Python version 2.5 or higher, support for sqlite3 databases is built in (sqlite allows you to have a relational database that is simply a file in your filesystem). But buyer beware, sqlite is not suited for production, so it may depend what you are trying to do with it.

Another option may be to call your host and complain, or change hosts. Honestly these days, any self respecting web host that supports python and mysql ought to have MySQLdb pre installed.

2016年12月11日45分05秒

I don't have any experience with http://www.SiteGround.com as a web host personally.

This is just a guess, but it's common for a shared host to support Python and MySQL with the MySQLdb module (e.g., GoDaddy does this). Try the following CGI script to see if MySQLdb is installed.

#!/usr/bin/python

module_name = 'MySQLdb'
head = '''Content-Type: text/html

%s is ''' % module_name

try:
    __import__(module_name)
    print head + 'installed'
except ImportError:
    print head + 'not installed'

2016年12月11日45分05秒

I uploaded it and got an internal error

Premature end of script headers

After much playing around, I found that if I had

import cgi
import cgitb; cgitb.enable()
import MySQLdb

It would give me a much more useful answer and say that it was not installed, you can see it yourself -> http://woarl.com/db.py

Oddly enough, this would produce an error

import MySQLdb
import cgi
import cgitb; cgitb.enable()

I looked at some of the other files I had up there and it seems that library was one of the ones I had already tried.

2016年12月11日45分05秒

You could try setting up your own python installation using Virtual Python. Check out how to setup Django using it here. That was written a long time ago, but it shows how I got MySQLdb setup without having root access or anything like it. Once you've got the basics going, you can install any python library you want.

2016年12月11日45分05秒

You really want MySQLdb for any MySQL + Python code. However, you shouldn't need root access or anything to use it. You can build/install it in a user directory (~/lib/python2.x/site-packages), and just add that to your PYTHON_PATH env variable. This should work for just about any python library.

Give it a shot, there really isn't a good alternative.

2016年12月11日45分05秒

Take a pick at

https://docs.djangoproject.com/en/1.8/ref/databases/

MySQLdb is mostly used driver, but if you are using python3 and django 1.8.x that will not work, then you should use mysqlclient that is a folk of MySQLdb on the following link

https://pypi.python.org/pypi/mysqlclient

2016年12月11日45分05秒