creating a new database with mysqldb

Lou Losee llosee at gmail.com
Wed May 17 16:53:32 EDT 2006


On 5/17/06, Philippe Martin <pmartin at snakecard.com> wrote:
>
> John Salerno wrote:
>
> > Since the connect method of mysqldb requires a database name, it seems
> > like you can't use it without having a database already created. So is
> > there a way to connect to your mysql server (without a specified
> > database) in order to create a new database (i.e., the CREATE DATABASE
> > query)?
> >
> > Thanks.
>
> I'm no expert but: can't you spawn mysql with a script/scheme ?
>
> Philippe
>
> MySQLdb.connect does not require a database name. ie.:

>>> import MySqlDB
>>> db = MySQLdb.connect(host='localhost', user='root', passwd='xxxx')
>>> csr = db.cursor()
>>> csr.execute('''show databases''')
6L
>>> for d in csr.fetchall():
    print d

('cc',)
('cc_41',)
('mysql',)
('purchaseorder',)
('test',)
('xsldb',)
>>> csr.execute('''create database newdb''')
1L
>>> csr.execute('''show databases''')
7L
>>> for d in csr.fetchall():
    print d

('cc',)
('cc_41',)
('mysql',)
('newdb',)
('purchaseorder',)
('test',)
('xsldb',)
>>>

Later,
Lou
-- 
Artificial Intelligence is no match for Natural Stupidity
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20060517/a81358e7/attachment.html>


More information about the Python-list mailing list